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

单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.

Step 1: Enable Required Apache Modules

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
Step 2: Configure Apache Virtual Hosts

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>
Step 3: Configure Tomcat Application Contexts

Make sure your Tomcat apps are accessible at the paths we defined in the Apache config. You have two easy ways to do this:

  1. Rename your WAR files: For example, rename tomcatapp1.war to tomcatapp1.war (wait, no—keep the name as-is! Tomcat automatically deploys it to http://localhost:8080/tomcatapp1 if the WAR file is named tomcatapp1.war).
  2. Explicitly set context paths: If you need a different path, add <Context> entries inside the <Host> block in Tomcat's conf/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>
Step 4: Enable the Config and Test

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.

Quick Notes
  • If you want a domain to point directly to an app (e.g., http://domain1.com goes straight to tomcatapp1), replace the root ProxyPass / line with ProxyPass / 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:12:52