Skip to content

🆘 FAQ & Troubleshooting

Issue 1: 502 Bad Gateway

Meaning: Nginx is acting as a reverse proxy, but the backend service is not responding.

bash
# Troubleshooting steps:
# 1. Is the backend service running?
systemctl status myapp
curl http://localhost:8080/health

# 2. Is the port correct?
ss -tlnp | grep 8080

# 3. Check Nginx error log
tail -50 /var/log/nginx/error.log
connect() failed (111: Connection refused) while connecting to upstream
# → Backend is not running

upstream prematurely closed connection while reading response header
# → Backend crashed

no live upstreams while connecting to upstream
# → All backends are down

# 4. SELinux might be blocking Nginx from connecting to the backend
getenforce
setsebool -P httpd_can_network_connect 1

Issue 2: 413 Request Entity Too Large

nginx
# Client upload exceeds Nginx limit
# Add in http/server/location block:
client_max_body_size 100m;    # Allow max 100MB

# If using PHP, also update php.ini
# upload_max_filesize = 100M
# post_max_size = 100M

Issue 3: Config Syntax Check

bash
# Always check syntax after editing config!
nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

# If there's an error, fix it based on the message. Common errors:
# - Missing semicolon ; at end of line
# - Mismatched braces
# - Unclosed quotes

# Reload config (without interrupting existing connections)
nginx -s reload

# Force reopen log files (after log rotation)
nginx -s reopen

Issue 4: Access Log Analysis

bash
# Top 10 most frequent IPs
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -10

# Status code distribution
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn
  45231 200
   3201 301
   1523 404
    892 500
    234 502

# Slowest requests (if upstream_time is logged)
awk '{print $NF, $7}' /var/log/nginx/access.log | sort -rn | head -10

# Real-time request rate monitoring
watch -n 1 'wc -l /var/log/nginx/access.log'