如何在单台Windows Server 2012上实现多版本Tomcat绑定80端口并配置虚拟主机
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.
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)
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)".
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.configof 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>- Rule name:
Option 2: Use Nginx as Reverse Proxy (Lightweight & Flexible)
If you prefer a cross-platform, lightweight option, Nginx works great on Windows too.
Download & Set Up Nginx
- Grab the latest Windows build of Nginx, extract it to a directory like
C:\nginx.
- Grab the latest Windows build of Nginx, extract it to a directory like
Configure Nginx for Domain Routing
OpenC:\nginx\conf\nginx.confand replace the defaultserverblock 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; } }Start Nginx
- Open Command Prompt as admin, navigate to
C:\nginx, and runstart nginx. - To run it as a Windows service (for auto-start on boot), use a dedicated service wrapper tool.
- Open Command Prompt as admin, navigate to
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.
Edit Tomcat's
server.xml
For each Tomcat instance, open{Tomcat Home}\conf\server.xmland 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 "%r" %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 underwebappsfor each app).
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

