apache和tomcat相結合使用實現僞靜態,同時把靜態文件從工程裏分離開

轉:http://blog.csdn.net/leidengyan/article/details/6069630


由於可能我們做網站的時候經常想把靜態文件和動態文件相分離,比如將幫助頁面的靜態頁面放到一些文件夾下,而不是放在工程,同時還有一些人希望能夠實現僞靜態功能,

下面我介紹tomcat和apache相結合,實現僞靜態,靜態文件和動態文件相分離的配置:
一、 首先安裝tomcat6.0,tomcat5.5也可以,安裝好了以後進入 安裝tomcat路徑/conf文件夾,編輯該文件夾下的文件server.xml。
找到
<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):

  1. NameVirtualHost *:80  
  2. ####以下是代理的模式,即www.soogifts.com這個域名下的所有連接都會跳到tomcat服務器下去解析(ajp://127.0.0.1:8009/)##############  
  3. <VirtualHost *:80>  
  4.   ServerName www.soogifts.com  
  5.     ServerAlias soogifts.com  
  6.     DocumentRoot "D:/www/ROOT/"  
  7.   ServerAdmin [email protected]  
  8.     ErrorLog "logs/www.soogifts.com-error_log"  
  9.     CustomLog "logs/www.soogifts.com-access_log" common  
  10.       
  11.     ProxyPass / ajp://127.0.0.1:8009/  
  12.     ProxyPassReverse / ajp://127.0.0.1:8009/  
  13. </VirtualHost>  
  14. ####以下是靜態資源的轉發,image.soogifts.com這個域名下的所有內容都會到D:/www/soogiftsImg/這個目錄下去找##############  
  15. <VirtualHost *:80>  
  16.     ServerName image.soogifts.com  
  17.     DocumentRoot "D:/www/soogiftsImg/"  
  18.     ServerAdmin [email protected]  
  19.     ErrorLog "logs/image.soogifts.com-error_log"  
  20.     CustomLog "logs/image.soogifts.com-access_log" common  
  21. </VirtualHost>  
  22.   
  23. ##############以下是包含rewrite規則的寫法,rewrite規則在rewrite-talentonline.conf這個文件當中######################  
  24. <VirtualHost *:80>  
  25.     ServerName www.thetalentonline.com  
  26.     DirectoryIndex index.shtml index.html  
  27.     DocumentRoot "D:/develop/ws_dream/talent/WebRoot/html"  
  28.   ServerAdmin [email protected]  
  29.     ErrorLog "logs/www.thetalentonline.com-error_log"  
  30.     CustomLog "logs/www.thetalentonline.com-access_log" common  
  31.     Include conf/extra/rewrite-talentonline.conf  
  32. </VirtualHost>  

rewrite-talentonline.conf(D:\Apache2.2\conf\extra\rewrite-talentonline.conf):

  1. RewriteEngine on  
  2. RewriteLog "logs/www.talentonline.cn-rewrite.log"  
  3. RewriteLogLevel 9  
  4. RewriteRule ^/([\S]+)\.jsp$ ajp://127.0.0.1:8009/$1\.jsp [P,L]  
  5. RewriteRule ^/([\S]+)\.jsp;jsessionid=([\S]+)$ ajp://127.0.0.1:8009/$1\.jsp;jsessionid=$2 [P,L]  
  6. RewriteRule ^/([\S]+)\.do$ ajp://127.0.0.1:8009/$1\.do [P,L]  
  7. RewriteRule ^/([\S]+)\.do;jsessionid=([\S]+)$ ajp://127.0.0.1:8009/$1\.do;jsessionid=$2 [P,L]  
  8.   
  9. RewriteRule /(\S+)\.js$ ajp://127.0.0.1:8009/$1\.js [P]  
  10. RewriteRule /(\S+)\.css$ ajp://127.0.0.1:8009/$1\.css [P]  
  11. RewriteRule /(\S+)\.gif$ ajp://127.0.0.1:8009/$1\.gif [P]  
  12. RewriteRule /(\S+)\.jpg$ ajp://127.0.0.1:8009/$1\.jpg [P]  
  13. RewriteRule /(\S+)\.ico$ ajp://127.0.0.1:8009/$1\.ico [P]  
  14. RewriteRule /(\S+)\.png$ ajp://127.0.0.1:8009/$1\.png [P]  

當然,上面的域名我並沒有到網上去申請來,然後通過dns解析到我的機器,只需要修改一下本機的一個文件,即可將你想要的域名都指向本機(只對本機有效),這個文件的地址是:C:\Windows\System32\drivers\etc\HOSTS

以下是我的hosts文件的配置,供大家參考:

  1. #ip                域名  
  2. 127.0.0.1           localhost  
  3. 127.0.0.1           www.soogifts.com  
  4. 127.0.0.1           image.soogifts.com  
  5. 127.0.0.1           www.thetalentonline.com 

發佈了5 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章