前面也透露了,tomcat和apache的集成没有用到传统的jk mod方式去集成,采用的是端口代理,原理就是将某个端口的请求转发到指定端口,因为我的vps需要支持php和tomcat所以就不能将这个IP下80端口的请求全部转发到8080端口,这是没意义的。所以,现在的需求就是将某个域名80端口的请求转发到8080端口。
1.httpd.conf配置
打开httpd.conf查找httpd-vhosts.conf,找到后将这一行的注释取消掉。
之前:
1 | #Include conf/extra/httpd-vhosts.conf |
之后
1 | Include conf/extra/httpd-vhosts.conf |
2.加载代理模块
找到proxy_module,去掉注释。忘记之前是怎么样了,反正现在的代理模块我是全开了。
1 2 3 4 5 6 7 | LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_ajp_module modules/mod_proxy_ajp.so LoadModule proxy_balancer_module modules/mod_proxy_balancer.so LoadModule proxy_connect_module modules/mod_proxy_connect.so LoadModule proxy_ftp_module modules/mod_proxy_ftp.so LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_scgi_module modules/mod_proxy_scgi.so |
3.编辑httpd-vhosts.conf文件
在conf目录找到extra目录,打开httpd-vhosts.conf,复制一下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | # # Virtual Hosts # # If you want to maintain multiple domains/hostnames on your # machine you can setup VirtualHost containers for them. Most configurations # use only name-based virtual hosts so the server doesn't need to worry about # IP addresses. This is indicated by the asterisks in the directives below. # # Please see the documentation at # <URL:http://httpd.apache.org/docs/2.2/vhosts/> # for further details before you try to setup virtual hosts. # # You may use the command line option '-S' to verify your virtual host # configuration. # # Use name-based virtual hosting. # NameVirtualHost *:80 # # VirtualHost example: # Almost any Apache directive may go into a VirtualHost container. # The first VirtualHost section is used for all requests that do not # match a ServerName or ServerAlias in any <VirtualHost> block. # # 设置虚拟主机,定义端口为80 <VirtualHost *:80> ProxyPreserveHost on ServerName yumuer.com ProxyPass / http://yumuer.com:8080/ ProxyPassReverse / http://yumuer.com:8080/ ErrorLog logs/yumuer.com-error_log.txt CustomLog logs/yumuer.com-access_log.txt common </VirtualHost> # 允许客户端访问此路径 <Directory "D:/soft/tomcat7/webapps/ROOT"> Options Indexes FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory> |
ProxyPreserveHost 代理模式是否开启,on表示开启,off表示关闭。
ServerName 服务名称,也可以理解为我们绑定的域名,我绑定的是yumuer.com(可以去看看哦,小广告)。
ProxyPass / http://yumuer.com:8080/
ProxyPassReverse / http://yumuer.com:8080/
这两是代理访问的路径和端口号,记得是请求转发哦,不是跳转。
其他的就不解释了,看看就懂了。不懂的就google翻译。
4.关闭必要权限
httpd.conf下面找到DocumentRoot最近的一个Directory,设置拒绝访问deny from all,如果你的配置本身就是这样,就不需要修改了。
好了,访问一下你的http://yumuer.com,是否已经转发到tomcat上面去了。测试的时候可以将tomcat和apache project目录放不同的文件,方便测试不同访问。