tomcat多虚拟主机配置+httpd 反向代理
1.tomcat 设置 <Engine name="Catalina" defaultHost="www.bbb.com"> #Engine默认主机defaultHost <Realm className="org.apache.catalina.realm.LockOutRealm"> <Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/> </Realm> <Host name="www.aaa.com" appBase="webapps"unpackWARs="true" autoDeploy="true"> <Context path="/test" docBase="/data/test" reloadable="false" /> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhostA_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> <Host name="www.bbb.com" appBase="website"unpackWARs="true" autoDeploy="true"> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhostB_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> </Engine> 2.创建页面 echo /usr/local/tomcat/webapps/ROOT/index.html > /usr/local/tomcat/webapps/ROOT/index.html mkdir /usr/local/tomcat/website/ROOT/ echo www.bbb.com/index.jsp > website/ROOT/index.jsp
[root@localhost7A ]# cat /etc/httpd/conf.d/magedu.conf <VirtualHost *:80> ServerName www.magedu.com ProxyRequests Off ProxyVia On ProxyPreserveHost off ProxyPass / http://www.aaa.com:8080 ProxyPassReverse / http://www.aaa.com:8080 </VirtualHost> [root@localhost7A ]# cat /etc/httpd/conf.d/magedu.conf <VirtualHost *:80> ServerName www.zzhz.com ProxyRequests Off ProxyVia On ProxyPreserveHost off #重点:如果ServerName 是一个新域名,如www.magedu.net 那么此选项应该为off. ProxyPass / http://www.bbb.com:8080 ProxyPassReverse / http://www.bbb.com:8080 </VirtualHost>
5.添加nginx和tomcat和客户端hosts解析 192.168.80.100 www.magedu.net www.zzhz.com 192.168.80.140 www.aaa.com www.bbb.com 6.测试中发现的问题:httpd中的ServerName指一个新的域名:如上面的www.magedu.net ,测试结果都访问到Engine中defaultHost="www.bbb.com 指定的服务器上, 因为ProxyPreserveHost:On开启。让代理保留原请求的Host首部(www.magedu.net),此时后端服务器无法识别。当off时,首部为www.aaa.com,而tomcat中定义了。 [root@localhost6B ~]# curl www.magedu.net /usr/local/tomcat/webapps/ROOT/index.html [root@localhost6B ~]# curl www.magedu.net #当ProxyPreserveHost on 时,走的是Engine默认主机www.bbb.com www.bbb.com/index.jsp [root@localhost6B ~]# curl www.zzhz.com www.bbb.com/index.jsp [root@localhost6B ~]# curl www.zzhz.com www.bbb.com/index.jsp