Hi NGINX Community,
I’m running into a 502 Bad Gateway error when using NGINX as a reverse proxy for my Node.js application. The setup works intermittently, but the error appears randomly, breaking my app’s frontend. I’m new to NGINX and need help debugging this.
Details:
- Environment: Ubuntu 22.04, NGINX 1.26.0 (latest, July 2025), Node.js 20.x running on port 3000.
- Setup: NGINX as reverse proxy, forwarding requests from
example.com
tolocalhost:3000
. Using default SSL (Let’s Encrypt). - Config:
server {
listen 80;
server_name example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
- Issue: 502 Bad Gateway errors occur randomly (e.g., after 5–10 requests). Node.js app is running (confirmed via
curl localhost:3000
). - Logs: NGINX error log shows:
2025/07/03 14:32:10 [error] 1234#1234: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.100, server: example.com, upstream: "http://127.0.0.1:3000/"
- Steps Tried:
- Restarted NGINX (
sudo systemctl restart nginx
) and Node.js app. - Verified Node.js is listening on
localhost:3000
(works via direct curl). - Checked firewall (
ufw allow 80,443
) and permissions on SSL certs. - Increased
proxy_timeout
to 30s in NGINX config—no change. - Read NGINX docs on reverse proxy but couldn’t pinpoint the issue.
- Restarted NGINX (
- Expected: Stable proxying to Node.js app without 502 errors.
- Actual: Random 502 Bad Gateway errors, connection refused in logs.
Questions:
- Is my NGINX config missing key directives for stable proxying to Node.js?
- Could the “Connection refused” error indicate a Node.js issue, and how do I debug it?
- Any best practices for NGINX reverse proxy settings (e.g., buffers, timeouts)?
- Should I check specific server resources (e.g., memory, CPU) affecting upstream?
I’ve reviewed the Community Guidelines and want to ensure my config is solid. Any debugging tips or config tweaks would be greatly appreciated!