Nginx多端口绑定多域名异常:Docker应用域名访问问题
Hey there! Since your App_1 is working perfectly with its domain, we can focus on a few key areas that are likely causing the problem with App_2. Let's walk through the most common fixes step by step:
1. Verify Your Nginx Server Block for App_2
First, double-check your Nginx configuration file for App_2's server block. It's easy to miss a small detail here that breaks everything.
Your config should look something like this (adjust paths/IPs as needed):
server { listen 80; server_name app.2.com; # Make sure this matches your domain exactly location / { # Point this to your App_2's accessible address (IP:port or Docker container name if on the same network) proxy_pass http://192.168.1.x:8081; # These headers help your app recognize the original request details 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; } }
After updating the config, test it for syntax errors with:
nginx -t
If it passes, reload Nginx to apply changes:
systemctl reload nginx # For systemd systems # Or service nginx reload # For older init systems
2. Confirm DNS Resolution for app.2.com
If your domain isn't pointing to the right server IP, you'll get errors even with a perfect Nginx config.
- On your local machine, run
nslookup app.2.comorping app.2.comto check if it resolves to192.168.1.x. - If this is a local test (not a public domain), add an entry to your hosts file:
- Linux/macOS: Edit
/etc/hostsand add192.168.1.x app.2.com - Windows: Edit
C:\Windows\System32\drivers\etc\hosts(as admin) and add the same line
- Linux/macOS: Edit
3. Check Firewall/Security Group Rules
Even though you can access App_2 via IP:8081, make sure the port Nginx is listening on (usually 80 for HTTP) is open:
- On Linux, use
ufw status(if using UFW) orfirewall-cmd --list-all(if using firewalld) to confirm port 80 is allowed. - If you're using a cloud provider, check your security group settings to ensure inbound traffic on port 80 is permitted.
4. Validate Docker Network & Port Mapping
Since IP:8081 works, your port mapping is probably correct, but let's double-check:
- Run
docker psand look for App_2's port entry. It should show0.0.0.0:8081->[container-port]/tcp(not127.0.0.1:8081..., which would only allow local access from the server itself). - If your Nginx is also running in Docker, ensure App_2 is connected to the same Docker network as Nginx. You can connect it with:
Then you can update thedocker network connect [nginx-network-name] [app2-container-name]proxy_passin Nginx to use the container name (e.g.,http://app2-container:8081) instead of the host IP, which is more reliable for Docker setups.
5. Dig Into Nginx Logs
If none of the above fixes work, check Nginx's logs to get specific error details:
- Error Log: Typically located at
/var/log/nginx/error.log— look for entries when you try to access app.2.com (e.g., 502 Bad Gateway means Nginx can't reach App_2, 404 means the proxy path is wrong). - Access Log: Located at
/var/log/nginx/access.log— check if the requests to app.2.com are even reaching Nginx.
Start with the first two steps (Nginx config and DNS) since those are the most common culprits. Let me know if you hit any specific errors and we can narrow it down further!
内容的提问来源于stack exchange,提问作者Alan Aranda

