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.2htdocs
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.2htdocs">
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.2htdocs
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.2htdocs">
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
剩下的就不多說了!
http://www.laozizhu.com/view.jsp?articleId=553