You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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 app2 works directly in Tomcat first: Run curl http://localhost:8080/app2—if this fails, fix the Tomcat deployment before troubleshooting Apache
  • For local testing, add myweb2.com to your /etc/hosts file: 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 myweb1 works, but confirm with sudo a2enmod proxy proxy_http if needed)

内容的提问来源于stack exchange,提问作者Innocento

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:19:49