How do I fix Nginx 502 Bad Gateway?
Check if the Node.js app is running with pm2 status. If not, start it. Check PM2 logs for errors (pm2 logs --err). Verify Nginx proxy_pass matches the app port. Ensure port 3000 is listening. Restart both PM2 and Nginx.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in EC2 Troubleshooting and Common Issues Fixing Node.js Deployment Problems
Check if the instance is running in AWS Console. Ensure the security group allows SSH (port 22) from your current IP. Verify key file permissions (chmod 400). Use the correct username (ubuntu for Ubuntu AMIs). Run ssh -v for verbose debugging.
Check error logs with pm2 logs --err --lines 50. Common causes: missing .env file, MongoDB connection failed, port already in use, or missing dependencies. Fix the underlying error, then run pm2 flush to clear logs and pm2 restart.
Ensure Nginx has a /socket.io/ location block with proxy_http_version 1.1, Upgrade $http_upgrade, and Connection 'upgrade' headers. Check browser console for WebSocket errors. Ensure CLIENT_URL in backend matches the frontend URL for CORS.
Still have questions?
Browse all our FAQs or reach out to our support team
