内网在线Lite MVPalice
返回/0a47122c...

你好 麻烦帮我看看 nginx 日志里那个 502 是怎么回事

4 成员已结晶 v1 · score 0.85创建 5/3/2026, 7:45:01 AM·最近更新 5/3/2026, 7:45:02 AM
结晶技能 · nginx-502-troubleshootingv1score 0.854 个经验综合 · 5/3/2026, 7:48:43 AM
何时使用

When a user reports an Nginx 502 Bad Gateway error and needs to diagnose the root cause from logs.

5 步 workflow
  1. 1Locate and examine Nginx error logs
    why: To identify the specific upstream failure details recorded in the logs.
    how: Check /var/log/nginx/error.log or custom log paths; look for 'upstream prematurely closed connection' or 'no live upstreams' messages.
  2. 2Check upstream service status
    why: To determine if the backend (e.g., PHP-FPM, Node.js) is running and responsive.
    how: Use systemctl status php-fpm or curl localhost:port to verify the upstream service is active and not overloaded.
  3. 3Analyze upstream pool configuration
    why: To identify misconfigurations in backup pools or worker limits that cause fallback failures.
    how: Review nginx.conf for upstream blocks; check if backup servers are defined and if worker_connections or fastcgi_read_timeout are too low.
  4. 4Adjust timeout and keepalive settings
    why: To prevent timeouts during slow upstream responses and maintain connections.
    how: Increase fastcgi_read_timeout or proxy_read_timeout in nginx.conf; add keepalive to upstream blocks for persistent connections.
  5. 5Test and reload Nginx configuration
    why: To apply changes and verify the 502 error is resolved.
    how: Run nginx -t to validate config, then systemctl reload nginx; monitor logs for recurrence.
Edge cases
  • If logs are empty or rotated, check log rotation settings or use journalctl -u nginx.
  • If upstream service is down entirely, restart it and check for resource exhaustion (CPU/memory).
  • If 502 persists after config changes, consider network issues or firewall rules blocking upstream ports.
Branches
  • For PHP-FPM issues: adjust pm.max_children and pm.start_servers in php-fpm.conf.
  • For proxy timeouts: increase proxy_connect_timeout and proxy_send_timeout in nginx.conf.
  • For load balancer setups: verify health checks and upstream weights in nginx upstream blocks.
成员经验 (4)· 按余弦相似度降序