apache和tomcat相结合使用实现伪静态,同时把静态文件从工程里分离开
2015-06-04 17:07
639 查看
转: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):
[html]
view plaincopy
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 service@soogifts.com
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 service@soogifts.com
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 service@thetalentonline.com
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):
[html]
view plaincopy
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文件的配置,供大家参考:
[html]
view plaincopy
#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
由于可能我们做网站的时候经常想把静态文件和动态文件相分离,比如将帮助页面的静态页面放到一些文件夹下,而不是放在工程,同时还有一些人希望能够实现伪静态功能,
下面我介绍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):
[html]
view plaincopy
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 service@soogifts.com
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 service@soogifts.com
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 service@thetalentonline.com
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):
[html]
view plaincopy
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文件的配置,供大家参考:
[html]
view plaincopy
#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
相关文章推荐
- linux下tomcat开机启动配置
- MyEclipse server里面的tomcat 不见了
- linux 系统 mysql+ssm+tomcat 部署(一) mysql安装
- 你喜欢使用eclipse+tomcat编程吗?!
- tomcat 优化
- eclipse tomcat 配置成自定义tomcat目录
- 黑马程序员-Java之自定义图形化界面的浏览器访问本地的Tomcat服务器
- 基于nginx的tomcat负载均衡和集群(超简单)
- Eclipse EE 发布项目导致 Tomcate 的配置文件 server.xml 还原
- Apache+Tomcat+JK集群配置
- 当tomcat注册为windows服务时候,怎样设置远程debug
- Linux下安装Tomcat服务器和部署Web应用
- Tomcat的安装及使用
- tomcat结合nginx使用小结
- Tomcat6 配置多虚拟主机,多域名绑定一IP
- 深入理解tomcat读书笔记
- tomcat部署项目 以及 如何去掉项目名称
- tomcat源码阅读(一)——环境搭建
- 启动tomcat时 一闪而过解决方法
- druid—tomcat配置使用(个人记录勿喷。。。)