基于EC2实例通过域名实现Docker端口转发可行吗?如何操作?
Absolutely feasible! You don’t need to spin up any extra EC2 instances—we can use a reverse proxy (like Nginx) on your existing EC2 instance to route traffic from port 80 to your two Docker containers based on the domain name. Here’s how to do it step by step:
1. Open port 80 in your EC2 security group
First, make sure incoming HTTP traffic on port 80 can reach your instance:
- Head to the EC2 console, find the security group attached to your instance
- Add an inbound rule: Type = HTTP, Protocol = TCP, Port range = 80, Source = 0.0.0.0/0 (you can restrict this to specific IPs later if you want tighter security)
2. Install Nginx on your EC2 instance
Connect to your EC2 via SSH, then run the appropriate commands for your OS:
For Amazon Linux 2/RHEL-based distros:
sudo yum update -y sudo yum install nginx -y sudo systemctl start nginx sudo systemctl enable nginx # Make Nginx start on boot
For Ubuntu/Debian:
sudo apt update && sudo apt install nginx -y sudo systemctl start nginx sudo systemctl enable nginx
3. Configure DNS records for your domains
Go to your domain registrar or DNS provider (e.g., AWS Route 53) and create two A records:
xyz.compointing to your EC2 instance’s public IP addressportal.xyz.comalso pointing to the same EC2 public IP
Wait 5-10 minutes for DNS propagation to take effect before moving on.
4. Set up Nginx virtual hosts for domain routing
First, back up the default config to be safe:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
Then create a dedicated config file for your domain routing:
sudo nano /etc/nginx/conf.d/domain-routing.conf
Paste this content into the file (it defines two server blocks to handle each domain):
# Route xyz.com to your WordPress container (port 8081) server { listen 80; server_name xyz.com www.xyz.com; # Include www if you want to support that subdomain location / { proxy_pass http://localhost:8081; # Pass original request details to the backend container proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } # Route portal.xyz.com to your Angular/NGINX container (port 8082) server { listen 80; server_name portal.xyz.com; location / { proxy_pass http://localhost:8082; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Save and exit the editor (for nano: press Ctrl+O, hit Enter, then Ctrl+X).
5. Test and apply the Nginx config
First, check for syntax errors in your config:
sudo nginx -t
If you see the message "test is successful", reload Nginx to apply the changes:
sudo systemctl reload nginx
6. Verify the setup
- Open a browser and visit
xyz.com—it should load your WordPress site from Docker1 - Visit
portal.xyz.com—it should load your Angular/NGINX portal from Docker2
Optional: Redirect www.xyz.com to non-www
If you want www.xyz.com to automatically redirect to xyz.com, add this extra server block to the same config file:
server { listen 80; server_name www.xyz.com; return 301 $scheme://xyz.com$request_uri; }
Run sudo nginx -t to check, then sudo systemctl reload nginx to apply.
内容的提问来源于stack exchange,提问作者Ric

