Apache与Tomcat反向代理配置求助:多虚拟主机访问异常
Fixing Your Second Apache Virtual Host for Tomcat Apps
Hey there! Let's start by tidying up your existing configuration first, then walk through setting up the second virtual host properly—since you mentioned it's giving you trouble.
First: Fix the Syntax Gap in Your Existing myweb1.com Config
Looking at your current setup, there's an extra </Directory> tag without a matching opening <Directory> block. That could lead to silent errors or unexpected behavior later. Here's the corrected version, plus a note about matching your Tomcat app context:
<VirtualHost *:80> ServerName myweb1.com ProxyRequests Off # I swapped /web1/ to /app1/ here since you said Tomcat's webapps have app1/app2—adjust back if web1 is intentional! ProxyPass / http://localhost:8080/app1/ ProxyPassReverse / http://localhost:8080/app1/ # Wrap directory settings in a proper <Directory> block if you need them <Directory /var/lib/tomcat7/webapps/app1> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost>
Second: Configure the Virtual Host for app2
To get myweb2.com (or whatever domain you want) pointing to your app2 Tomcat app, create a new config file (usually in /etc/apache2/sites-available/myweb2.conf) with this setup:
<VirtualHost *:80> ServerName myweb2.com ProxyRequests Off # Map the domain root directly to app2's Tomcat context ProxyPass / http://localhost:8080/app2/ ProxyPassReverse / http://localhost:8080/app2/ # Optional: Add directory controls if you need to manage local file access <Directory /var/lib/tomcat7/webapps/app2> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> </VirtualHost>
Final Steps to Get It Working
- Enable the new site:
sudo a2ensite myweb2.conf - Restart Apache to apply changes:
sudo service apache2 restart - Double-check
app2works directly in Tomcat first: Runcurl http://localhost:8080/app2—if this fails, fix the Tomcat deployment before troubleshooting Apache - For local testing, add
myweb2.comto your/etc/hostsfile:127.0.0.1 myweb2.com
Quick pitfalls to avoid:
- Typos in the ProxyPass path (make sure it matches exactly what Tomcat uses for
app2) - Forgetting to enable the new site with
a2ensite - Missing proxy modules (you likely have them since
myweb1works, but confirm withsudo a2enmod proxy proxy_httpif needed)
内容的提问来源于stack exchange,提问作者Innocento
相关产品推荐
相关产品推荐

