单IP环境下Apache多域名对应多Tomcat 9应用的配置实现咨询
Got it, let's walk through exactly how to set up this Apache-Tomcat proxy configuration to match your requirements. Since all your domains point to the same server IP, we'll use Apache's virtual hosts to route requests to the right Tomcat apps based on the incoming domain/subdomain.
First, make sure Apache has the tools it needs to handle reverse proxying. Run these commands (adjust for your OS if you're not on Debian/Ubuntu):
a2enmod proxy a2enmod proxy_http a2enmod vhost_alias
Restart Apache to apply the changes:
systemctl restart apache2
We'll create separate virtual host blocks for each domain/subdomain to define how requests get routed to your Tomcat apps. Create a new config file (e.g., /etc/apache2/sites-available/tomcat-proxy.conf) and add the following:
Virtual Host for domain1.com
This routes traffic to tomcatapp1 and tomcatapp2 (accessible via subpaths like domain1.com/app1):
<VirtualHost *:80> ServerName domain1.com ServerAlias www.domain1.com # Optional, include if you want www to work too # Route to tomcatapp1 ProxyPass /app1 http://localhost:8080/tomcatapp1 ProxyPassReverse /app1 http://localhost:8080/tomcatapp1 # Route to tomcatapp2 ProxyPass /app2 http://localhost:8080/tomcatapp2 ProxyPassReverse /app2 http://localhost:8080/tomcatapp2 # Optional: Set a default landing page if someone visits the root domain ProxyPass / http://localhost:8080/your-default-app ProxyPassReverse / http://localhost:8080/your-default-app </VirtualHost>
Virtual Host for domain2.com
This covers all the apps you listed (tomcatapp1, tomcatapp2, tomcatapp3, tomcatapp4, tomcatapp5, tomcatapp6):
<VirtualHost *:80> ServerName domain2.com ServerAlias www.domain2.com # Optional # Route to tomcatapp1 ProxyPass /app1 http://localhost:8080/tomcatapp1 ProxyPassReverse /app1 http://localhost:8080/tomcatapp1 # Route to tomcatapp2 ProxyPass /app2 http://localhost:8080/tomcatapp2 ProxyPassReverse /app2 http://localhost:8080/tomcatapp2 # Route to tomcatapp3 ProxyPass /app3 http://localhost:8080/tomcatapp3 ProxyPassReverse /app3 http://localhost:8080/tomcatapp3 # Route to tomcatapp4 ProxyPass /app4 http://localhost:8080/tomcatapp4 ProxyPassReverse /app4 http://localhost:8080/tomcatapp4 # Route to tomcatapp5 ProxyPass /app5 http://localhost:8080/tomcatapp5 ProxyPassReverse /app5 http://localhost:8080/tomcatapp5 # Route to tomcatapp6 ProxyPass /app6 http://localhost:8080/tomcatapp6 ProxyPassReverse /app6 http://localhost:8080/tomcatapp6 </VirtualHost>
Virtual Host for subdomain1.domain1.com
This routes to tomcatapp4, tomcatapp5, tomcatapp6, and tomcatapp1:
<VirtualHost *:80> ServerName subdomain1.domain1.com # Route to tomcatapp1 ProxyPass /app1 http://localhost:8080/tomcatapp1 ProxyPassReverse /app1 http://localhost:8080/tomcatapp1 # Route to tomcatapp4 ProxyPass /app4 http://localhost:8080/tomcatapp4 ProxyPassReverse /app4 http://localhost:8080/tomcatapp4 # Route to tomcatapp5 ProxyPass /app5 http://localhost:8080/tomcatapp5 ProxyPassReverse /app5 http://localhost:8080/tomcatapp5 # Route to tomcatapp6 ProxyPass /app6 http://localhost:8080/tomcatapp6 ProxyPassReverse /app6 http://localhost:8080/tomcatapp6 </VirtualHost>
Make sure your Tomcat apps are accessible at the paths we defined in the Apache config. You have two easy ways to do this:
- Rename your WAR files: For example, rename
tomcatapp1.wartotomcatapp1.war(wait, no—keep the name as-is! Tomcat automatically deploys it tohttp://localhost:8080/tomcatapp1if the WAR file is namedtomcatapp1.war). - Explicitly set context paths: If you need a different path, add
<Context>entries inside the<Host>block in Tomcat'sconf/server.xml:
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="/tomcatapp1" docBase="tomcatapp1" reloadable="true"/> <Context path="/tomcatapp2" docBase="tomcatapp2" reloadable="true"/> <!-- Repeat for all your apps --> </Host>
Enable the new virtual host config:
a2ensite tomcat-proxy.conf
Restart Apache one more time:
systemctl restart apache2
Now test by visiting URLs like http://domain1.com/app1 or http://subdomain1.domain1.com/app4 to confirm they route to the correct Tomcat apps.
- If you want a domain to point directly to an app (e.g.,
http://domain1.comgoes straight to tomcatapp1), replace the rootProxyPass /line withProxyPass / http://localhost:8080/tomcatapp1. - Double-check that your DNS records for all domains/subdomains point to your server's public IP.
- If you hit issues, check Apache's error logs (
/var/log/apache2/error.log) and Tomcat's logs (TOMCAT_HOME/logs/catalina.out) for clues.
内容的提问来源于stack exchange,提问作者Vidya

