How do I debug a 502 Bad Gateway in Nginx?
Check sudo tail -20 /var/log/nginx/error.log for the error cause. Verify Node.js is running with pm2 status. Check that the proxy_pass port matches the Node.js port. Restart both Nginx and PM2 if needed.
Verify This Answer
Cross-check this information using these trusted sources:
More FAQs in Nginx Logging and Debugging Access Logs, Error Logs, and Troubleshooting
Access logs are at /var/log/nginx/access.log (every HTTP request) and error logs are at /var/log/nginx/error.log (errors and warnings). View in real-time with sudo tail -f /var/log/nginx/access.log or /var/log/nginx/error.log.
Define log_format in nginx.conf with variables like $request_time (total request time), $upstream_connect_time (time to connect to Node.js), and $upstream_response_time (Node.js response time). Then use access_log /path/to/log main; to apply it.
Use awk to extract the URL field and count: sudo awk '{print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20. This shows the top 20 most requested URLs with request counts.
Still have questions?
Browse all our FAQs or reach out to our support team
