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

如何在单台Windows Server 2012上实现多版本Tomcat绑定80端口并配置虚拟主机

Solution for Routing Multiple Tomcat Instances to Port 80 & Configuring Virtual Hosts

Great question! The cleanest way to achieve this is using a reverse proxy to route incoming port 80 traffic to your individual Tomcat instances based on the requested domain name. Below are two reliable solutions tailored for Windows Server 2012, plus steps to set up Tomcat virtual hosts for better app isolation.

Option 1: Use IIS as Reverse Proxy (Native Windows Solution)

Since you're on Windows Server, IIS is a natural fit—it integrates seamlessly with the OS and is easy to manage via the GUI.

  1. Install Required Modules

    • Open IIS Manager, go to the Web Platform Installer (under "Management" in the right-hand pane).
    • Search for and install:
      • URL Rewrite
      • Application Request Routing (ARR)
  2. Set Up a Proxy Site

    • Create a new IIS site (or use the default site) that listens on port 80. You don't need any physical files in the site directory—its sole purpose is to proxy traffic.
    • In the site's features, open URL Rewrite, then click "Add Rule(s)".
  3. Add Rewrite Rules for Each Domain
    For each app, create a rule that matches the domain and forwards traffic to the corresponding Tomcat port:

    • Rule name: Route to app1
    • Match URL: .* (matches any path)
    • Conditions: Add a condition where {HTTP_HOST} matches ^app1domain.com$
    • Action: Select "Rewrite", set the rewrite URL to http://localhost:2121/{R:0}
    • Repeat this for app2 (http://localhost:2222/{R:0}) and app3 (http://localhost:2323/{R:0})

    Alternatively, you can paste this directly into the web.config of your IIS site:

    <rewrite>
      <rules>
        <rule name="Route app1" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^app1domain.com$" />
          </conditions>
          <action type="Rewrite" url="http://localhost:2121/{R:0}" />
        </rule>
        <rule name="Route app2" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^app2domain.com$" />
          </conditions>
          <action type="Rewrite" url="http://localhost:2222/{R:0}" />
        </rule>
        <rule name="Route app3" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^app3domain.com$" />
          </conditions>
          <action type="Rewrite" url="http://localhost:2323/{R:0}" />
        </rule>
      </rules>
    </rewrite>
    

Option 2: Use Nginx as Reverse Proxy (Lightweight & Flexible)

If you prefer a cross-platform, lightweight option, Nginx works great on Windows too.

  1. Download & Set Up Nginx

    • Grab the latest Windows build of Nginx, extract it to a directory like C:\nginx.
  2. Configure Nginx for Domain Routing
    Open C:\nginx\conf\nginx.conf and replace the default server block with these server blocks for each app:

    server {
        listen 80;
        server_name app1domain.com;
    
        location / {
            proxy_pass http://localhost:2121;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    
    server {
        listen 80;
        server_name app2domain.com;
    
        location / {
            proxy_pass http://localhost:2222;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    
    server {
        listen 80;
        server_name app3domain.com;
    
        location / {
            proxy_pass http://localhost:2323;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
    
  3. Start Nginx

    • Open Command Prompt as admin, navigate to C:\nginx, and run start nginx.
    • To run it as a Windows service (for auto-start on boot), use a dedicated service wrapper tool.

Configuring Tomcat Virtual Hosts (Best Practice)

Even with a reverse proxy, setting up virtual hosts in Tomcat ensures better app isolation and makes it easier to add new apps later.

  1. Edit Tomcat's server.xml
    For each Tomcat instance, open {Tomcat Home}\conf\server.xml and add a <Host> element inside the <Engine> block:

    <Host name="app1domain.com" appBase="webapps/app1" unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" 
               directory="logs" prefix="app1_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    </Host>
    
    • name: Should match the domain name for the app.
    • appBase: The directory where the app's WAR file or exploded files live (you can create a subdirectory under webapps for each app).
  2. Restart Tomcat
    After making changes, restart the Tomcat service to apply the virtual host configuration.

Additional Tips

  • Firewall Setup: Ensure inbound port 80 is open on your server's firewall. You don't need to open the Tomcat ports (2121, 2222, etc.) to the public—they only need to be accessible locally from the reverse proxy.
  • DNS Configuration: Point each domain (app1domain.com, app2domain.com, etc.) to your server's public IP address.
  • Adding New Tomcats: When installing a new Tomcat instance, assign it a unique HTTP port (e.g., 2424), then add a new rule in your reverse proxy (IIS or Nginx) to route the new domain to this port.
  • Service Management: For IIS, it's already a Windows service. For Nginx, use a service wrapper to ensure it starts automatically when the server boots.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 07:36:09