Express+Docker-Compose环境下Nginx代理配置及访问异常求助
Hey there, let's break down why you can't access your UI app directly via 188.222.22.22 (default port 80) and fix this step by step. First, let's recap your setup to align: you've got a Docker Compose stack running Rails, PostgreSQL, Express (UI), and Nginx. You can reach Rails via 188.222.22.22:3000 (proxied through Nginx) and Express directly via 188.222.22.22:4000, but hitting the raw IP doesn't load the UI. Let's dig into the most likely culprits.
1. Fix Nginx's Port 80 Routing Config
Since Nginx acts as your reverse proxy, it needs to listen on port 80 and forward those requests to your Express UI container. Here's what to check in your nginx.conf:
- Make sure there's a dedicated server block for port 80 that points to your Express service (not just the Rails one). A correct snippet would look like this:
server { listen 80; server_name 188.222.22.22; # Leave empty to catch all incoming requests if needed location / { proxy_pass http://express:4000; # Use the exact service name from your docker-compose.yml 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; } # Keep your existing Rails proxy block for port 3000 below (example) server { listen 3000; location / { proxy_pass http://rails:3000; # ... your existing Rails proxy headers } } }
Common mistakes here: Forgetting the port 80 server block entirely, using localhost:4000 instead of the Docker service name (containers communicate via service names in the Compose network), or missing proxy headers that cause Express to reject the request.
2. Verify Docker Compose Port Mappings
Your Nginx container must have port 80 mapped to the host's port 80. Double-check your docker-compose.yml for the Nginx service:
services: nginx: image: nginx:alpine ports: - "80:80" # Critical: Host port 80 maps to container port 80 - "3000:3000" # For Rails access volumes: - ./nginx.conf:/etc/nginx/nginx.conf # Ensure your config is mounted correctly depends_on: - rails - express express: build: ./express-ui ports: - "4000:4000" # Keep this for direct access testing environment: - NODE_ENV=production depends_on: - postgres # ... your other services (rails, postgres)
If Nginx's port 80 isn't mapped to the host's 80, requests to 188.222.22.22 won't reach Nginx at all. Also, confirm the Nginx container is running with docker-compose ps.
3. Ensure Express Listens on 0.0.0.0
In your server.ts, make sure your Express app binds to 0.0.0.0 instead of localhost or 127.0.0.1. Containers need to listen on all interfaces to accept connections from other containers (like Nginx) or the host. Example:
const PORT = process.env.PORT || 4000; app.listen(PORT, '0.0.0.0', () => { console.log(`Express UI running on port ${PORT}`); });
If it's only listening on localhost, Nginx can't reach it—localhost inside the Express container refers to itself, not the shared Docker network.
4. Test Network Connectivity Between Containers
To rule out network issues, exec into the Nginx container and try curling the Express service:
docker-compose exec nginx curl http://express:4000
If this returns your UI content, the network between Nginx and Express is working. If not, check that the Express container is healthy and both services are in the same Docker network (Compose creates a default network automatically, so they should be unless you customized it).
5. Check Host & Cloud Firewall Rules
Sometimes the host's firewall (like ufw on Ubuntu or firewalld on CentOS) blocks port 80. Verify it's open:
# For ufw users ufw allow 80/tcp # For firewalld users firewall-cmd --add-port=80/tcp --permanent firewall-cmd --reload
If you're using a cloud provider (AWS, GCP, etc.), also confirm port 80 is open in your security group rules—this is a super common oversight!
Final Steps After Fixing Configs
Once you've adjusted your configs:
- Restart the affected containers:
docker-compose up -d --force-recreate nginx express - Clear your browser cache or use incognito mode to avoid cached responses
- Test accessing
188.222.22.22again
If you still run into issues, feel free to share snippets of your actual config files and I can help dig deeper!
内容的提问来源于stack exchange,提问作者dkey

