1 安装 tomcat/apache
2 下载 jk.so, 具体版本请根据需要,不再多说
3 关键是配置
httpd.conf
查看复制到剪切板打印
# 去掉这个前面的#,启用它
LoadModule rewrite_module modules/mod_rewrite.so
# 启用jk,同时配置参数
LoadModule jk_module modules/mod_jk.so
JkWorkersFile D:/Apache2.2/conf/workers.properties
JkLogFile d:/Apache2.2/logs/mod_jk.log
JkLogLevel info
#JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /servlet/* myloadbalancer
JkMount /*.jsp myloadbalancer
# 虚拟主机,这里模拟了 a.test.com 和 b.test.com 以及 a.tst.com/blog的调用
<VirtualHost _default_:80>
ServerAdmin
[email protected]
DocumentRoot D:\Apache2.2\htdocs
ServerName all-sites
ErrorLog logs/all-sites-error.log
CustomLog logs/all-sites-access.log common
RewriteEngine On
# 下面三行实现动态解析
RewriteCond %{HTTP_HOST} ^[a-z0-9\-]+\.test\.com$
RewriteRule ^/(.*)$ /%{HTTP_HOST}/$1
RewriteRule ^/([a-z0-9\-]+)\.test\.com/?$ /index.jsp?u=$1 [L,PT]
RewriteRule ^/([a-z0-9\-]+)\.test\.com/blog(/(.*))?$ /blog.jsp?u=$1&$3 [L,PT]
<Directory "D:\Apache2.2\htdocs">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
# 去掉这个前面的#,启用它
LoadModule rewrite_module modules/mod_rewrite.so
# 启用jk,同时配置参数
LoadModule jk_module modules/mod_jk.so
JkWorkersFile D:/Apache2.2/conf/workers.properties
JkLogFile d:/Apache2.2/logs/mod_jk.log
JkLogLevel info
#JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /servlet/* myloadbalancer
JkMount /*.jsp myloadbalancer
# 虚拟主机,这里模拟了 a.test.com 和 b.test.com 以及 a.tst.com/blog的调用
<VirtualHost _default_:80>
ServerAdmin
[email protected]
DocumentRoot D:\Apache2.2\htdocs
ServerName all-sites
ErrorLog logs/all-sites-error.log
CustomLog logs/all-sites-access.log common
RewriteEngine On
# 下面三行实现动态解析
RewriteCond %{HTTP_HOST} ^[a-z0-9\-]+\.test\.com$
RewriteRule ^/(.*)$ /%{HTTP_HOST}/$1
RewriteRule ^/([a-z0-9\-]+)\.test\.com/?$ /index.jsp?u=$1 [L,PT]
RewriteRule ^/([a-z0-9\-]+)\.test\.com/blog(/(.*))?$ /blog.jsp?u=$1&$3 [L,PT]
<Directory "D:\Apache2.2\htdocs">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
workers.properties
查看复制到剪切板打印
worker.list=myloadbalancer
worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009
worker.tomcat1.lbfactor=1
worker.myloadbalancer.type=lb
worker.myloadbalancer.balance_workers=tomcat1
worker.status.type=status
worker.list=myloadbalancer
worker.tomcat1.type=ajp13
worker.tomcat1.host=localhost
worker.tomcat1.port=8009
worker.tomcat1.lbfactor=1
worker.myloadbalancer.type=lb
worker.myloadbalancer.balance_workers=tomcat1
worker.status.type=status
server.xml
去掉前后的<!-- 和 -->启用 8009端口查看复制到剪切板打印
<Connector port="8009"
..
<Connector port="8009"
...
说明一下
在httpd.conf里面启用了rewrite_module,和 mod_jk, 并配置了 与tomcat的连接配置。
虚拟主机的配置是关键
ServerName all-sites
可以修改为
ServerName test.com
ServerAlias *.test.com
这样可以明确指定这个虚拟主机负责的域名
urlRewrite 配置部分
RewriteCond %{HTTP_HOST} ^[a-z0-9\-]+\.test\.com$
RewriteRule ^/(.*)$ /%{HTTP_HOST}/$1
RewriteRule ^/([a-z0-9\-]+)\.test\.com/?$ /index.jsp?u=$1 [L,PT]
RewriteRule ^/([a-z0-9\-]+)\.test\.com/blog(/(.*))?$ /blog.jsp?u=$1&$3 [L,PT]
第一行,声明这个重写只对主机名(%{HTTP_HOST}) 类似 XXXX.test.com 感兴趣,那个是正则的
第二行,将所有的对此域名的请求进行第一次改写,比如
a.test.com 改成
/a.test.com
把
b.test.com/blog 改成
/b.test/com/blog
第三行,对无参数的访问进行调整
将 /a.test.com 改成
/index.jsp?u=a
第四行,对有参数路径的进行调整
将 /b.test.com/blog 改成
/blog.jsp?u=b
如果还有参数比如
b.test.com/id=3 则最终改写为
/b.test.com/id=3
/blog.jsp?u=b&id=3
剩下的就不多说了!