轉:http://blog.csdn.net/leidengyan/article/details/6069630
由於可能我們做網站的時候經常想把靜態文件和動態文件相分離,比如將幫助頁面的靜態頁面放到一些文件夾下,而不是放在工程,同時還有一些人希望能夠實現僞靜態功能,
找到
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
確定你待會ajp跳轉時的tomcat端口號。我的tomcat ajp協議的端口是8009。
二、安裝apache2.2,我之前試過apache2.0,發現apache2.0沒有ajp跳轉的功能,只能用proxy代理訪問。
我的apache2.0原來的訪問代碼如下:
ProxyPass / http://127.0.0.1:8099/
ProxyPassReverse / http://127.0.0.1:8099/
這樣跳轉的缺點就是到tomcat容器時java獲得的連接是127.0.0.1:8099,而不是域名。還有報錯時會暴露出tomcat的訪問端口,對安全造成一定的影響。
回到主題,安裝完apache2.2以後。進入安裝apache的路徑/conf文件夾下,
三、配置http.conf文件
編輯httpd.conf文件:
1 找到以下兩行,將前面的#號去掉,這個是加載ajp跳轉的功能#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
2 找到以下代碼,將前面的#去掉,這個是加載rewrite模塊的功能
#LoadModule rewrite_module modules/mod_rewrite.so
3 找到以下代碼,將前面的#去掉:
#Include conf/extra/httpd-vhosts.conf
四、配置httpd-vhosts文件
然後打開安裝apache的路徑/conf/extra文件夾,編輯httpd-vhosts.conf文件在文件中加入你的域名,比如:
<VirtualHost *:80>
ServerName www.域名.com
DocumentRoot "D:/www/域名"
ServerAdmin service@域名.com
ErrorLog "logs/www.域名.com-error_log"
CustomLog "logs/www.域名.com-access_log" common
Include conf/extra/rewrite-域名.conf
</VirtualHost>
ServerName是你的域名
DocumentRoot是你的靜態文件存放的路徑。
然後在這個文件夾下創建一個文件叫rewrite-域名.conf,編輯這個文件寫上你的跳轉規則:
如:
RewriteEngine on
RewriteLog "logs/www.talentonline.com-rewrite.log"
RewriteLogLevel 9
RewriteRule ^/([/S]+)/.do$ ajp://127.0.0.1:8009/$1/.do [P,L]
RewriteRule ^/([/S]+)/.do;jsessionid=([/S]+)$ ajp://127.0.0.1:8009/$1/.do;jsessionid=$2 [P,L]
RewriteRule /(/S+)/.js$ ajp://127.0.0.1:8009/$1/.js [P]
RewriteRule /(/S+)/.css$ ajp://127.0.0.1:8009/$1/.css [P]
RewriteRule /(/S+)/.gif$ ajp://127.0.0.1:8009/$1/.gif [P]
RewriteRule /(/S+)/.jpg$ ajp://127.0.0.1:8009/$1/.jpg [P]
##以下你可以寫你自己的rewrite跳轉規則
這樣就可以實現靜態頁面從工程分離出去,放到單獨的文件夾下,這樣不但可以縮小工程的大小,還能加快訪問速度。
趕快試一下吧。
五、附
以下附上我的httpd-vhosts.conf和rewrite-talentonline.conf代碼,供大家參考:
httpd-vhosts.conf(D:\Apache2.2\conf\extra\httpd-vhosts.conf):
- NameVirtualHost *:80
- ####以下是代理的模式,即www.soogifts.com這個域名下的所有連接都會跳到tomcat服務器下去解析(ajp://127.0.0.1:8009/)##############
- <VirtualHost *:80>
- ServerName www.soogifts.com
- ServerAlias soogifts.com
- DocumentRoot "D:/www/ROOT/"
- ServerAdmin [email protected]
- ErrorLog "logs/www.soogifts.com-error_log"
- CustomLog "logs/www.soogifts.com-access_log" common
- ProxyPass / ajp://127.0.0.1:8009/
- ProxyPassReverse / ajp://127.0.0.1:8009/
- </VirtualHost>
- ####以下是靜態資源的轉發,image.soogifts.com這個域名下的所有內容都會到D:/www/soogiftsImg/這個目錄下去找##############
- <VirtualHost *:80>
- ServerName image.soogifts.com
- DocumentRoot "D:/www/soogiftsImg/"
- ServerAdmin [email protected]
- ErrorLog "logs/image.soogifts.com-error_log"
- CustomLog "logs/image.soogifts.com-access_log" common
- </VirtualHost>
- ##############以下是包含rewrite規則的寫法,rewrite規則在rewrite-talentonline.conf這個文件當中######################
- <VirtualHost *:80>
- ServerName www.thetalentonline.com
- DirectoryIndex index.shtml index.html
- DocumentRoot "D:/develop/ws_dream/talent/WebRoot/html"
- ServerAdmin [email protected]
- ErrorLog "logs/www.thetalentonline.com-error_log"
- CustomLog "logs/www.thetalentonline.com-access_log" common
- Include conf/extra/rewrite-talentonline.conf
- </VirtualHost>
rewrite-talentonline.conf(D:\Apache2.2\conf\extra\rewrite-talentonline.conf):
- RewriteEngine on
- RewriteLog "logs/www.talentonline.cn-rewrite.log"
- RewriteLogLevel 9
- RewriteRule ^/([\S]+)\.jsp$ ajp://127.0.0.1:8009/$1\.jsp [P,L]
- RewriteRule ^/([\S]+)\.jsp;jsessionid=([\S]+)$ ajp://127.0.0.1:8009/$1\.jsp;jsessionid=$2 [P,L]
- RewriteRule ^/([\S]+)\.do$ ajp://127.0.0.1:8009/$1\.do [P,L]
- RewriteRule ^/([\S]+)\.do;jsessionid=([\S]+)$ ajp://127.0.0.1:8009/$1\.do;jsessionid=$2 [P,L]
- RewriteRule /(\S+)\.js$ ajp://127.0.0.1:8009/$1\.js [P]
- RewriteRule /(\S+)\.css$ ajp://127.0.0.1:8009/$1\.css [P]
- RewriteRule /(\S+)\.gif$ ajp://127.0.0.1:8009/$1\.gif [P]
- RewriteRule /(\S+)\.jpg$ ajp://127.0.0.1:8009/$1\.jpg [P]
- RewriteRule /(\S+)\.ico$ ajp://127.0.0.1:8009/$1\.ico [P]
- RewriteRule /(\S+)\.png$ ajp://127.0.0.1:8009/$1\.png [P]
當然,上面的域名我並沒有到網上去申請來,然後通過dns解析到我的機器,只需要修改一下本機的一個文件,即可將你想要的域名都指向本機(只對本機有效),這個文件的地址是:C:\Windows\System32\drivers\etc\HOSTS
以下是我的hosts文件的配置,供大家參考:
- #ip 域名
- 127.0.0.1 localhost
- 127.0.0.1 www.soogifts.com
- 127.0.0.1 image.soogifts.com
- 127.0.0.1 www.thetalentonline.com