Nginx Apache 502 Bad Gateway Error - How to Fix

Troubleshooting guide for Nginx and Apache 502 Bad Gateway errors. Learn how to diagnose gateway errors, identify root causes, and fix backend connectivity issues.

Last updated: 2026-01-11

Nginx Apache 502 Bad Gateway Error - How to Fix

502 Bad Gateway errors indicate that Nginx or Apache (acting as a reverse proxy) cannot communicate with backend servers. This troubleshooting guide helps you diagnose gateway errors, identify root causes, and fix backend connectivity issues.

For comprehensive web server monitoring, see Application Performance Monitoring. For checking response times, see Check Nginx Apache Response Times.

Symptoms of 502 Bad Gateway Error

  • HTTP 502 status code in responses
  • "502 Bad Gateway" error page displayed
  • Intermittent 502 errors
  • Backend servers unreachable
  • Timeout errors in web server logs

Step 1: Check Backend Server Status

Verify Backend Connectivity

# Test backend server connectivity
curl -I http://backend-server:port/health

# Check backend server response
curl -v http://backend-server:port/

# Test backend from web server
curl -I http://localhost:backend-port/health

# Check backend server logs
tail -50 /var/log/backend/error.log

Check Backend Service Status

# Check if backend service is running
systemctl status backend-service

# Check backend process
ps aux | grep backend-process

# Check backend port listening
netstat -tlnp | grep backend-port
ss -tlnp | grep backend-port

Step 2: Check Web Server Configuration

Review Nginx Configuration

# Check Nginx configuration
nginx -t

# View Nginx upstream configuration
nginx -T | grep -A 10 upstream

# Check Nginx error logs
tail -50 /var/log/nginx/error.log | grep 502

# Test Nginx configuration
nginx -T | grep -E "proxy_pass|upstream"

Review Apache Configuration

# Check Apache configuration
apache2ctl configtest  # Debian/Ubuntu
httpd -t                # CentOS/RHEL

# View Apache proxy configuration
apache2ctl -S | grep ProxyPass

# Check Apache error logs
tail -50 /var/log/apache2/error.log | grep 502

Step 3: Identify Root Causes

Common Causes of 502 Errors

  1. Backend Server Issues:

    • Backend server down or crashed
    • Backend service not running
    • Backend port not listening
    • Backend application errors
  2. Network Connectivity Issues:

    • Firewall blocking connections
    • Network routing problems
    • Backend server unreachable
    • DNS resolution failures
  3. Timeout Issues:

    • Backend response timeout too short
    • Backend processing too slow
    • Connection timeout exceeded
    • Proxy timeout configuration
  4. Configuration Errors:

    • Incorrect backend server address
    • Wrong backend port
    • Proxy configuration errors
    • Upstream server misconfiguration

Step 4: Diagnose Specific Issues

Check Backend Server Logs

# View backend error logs
tail -100 /var/log/backend/error.log

# Check for backend errors
grep -i error /var/log/backend/error.log | tail -20

# Check backend access logs
tail -50 /var/log/backend/access.log

Check Web Server Logs

# View Nginx error logs
tail -100 /var/log/nginx/error.log | grep 502

# Check Nginx access logs for 502 errors
tail -100 /var/log/nginx/access.log | grep " 502 "

# View Apache error logs
tail -100 /var/log/apache2/error.log | grep 502

Test Backend Connectivity

# Test backend from web server
curl -v http://backend-server:port/health

# Test with timeout
curl --max-time 5 http://backend-server:port/health

# Check backend response time
time curl http://backend-server:port/health

Step 5: Fix 502 Bad Gateway Errors

Fix Backend Server Issues

  1. Restart Backend Service:

    # Restart backend service
    systemctl restart backend-service
    
    # Check service status
    systemctl status backend-service
    
  2. Fix Backend Application Errors:

    • Review backend application logs
    • Fix application bugs
    • Update application configuration
    • Restart application

Fix Network Connectivity Issues

  1. Check Firewall Rules:

    # Check firewall status
    sudo ufw status
    sudo firewall-cmd --list-all
    
    # Allow backend port
    sudo ufw allow backend-port/tcp
    
  2. Verify Network Connectivity:

    # Test network connectivity
    ping backend-server
    
    # Test port connectivity
    telnet backend-server backend-port
    nc -zv backend-server backend-port
    

Fix Timeout Issues

  1. Increase Proxy Timeouts:

    # Nginx configuration
    proxy_connect_timeout 60s;
    proxy_send_timeout 60s;
    proxy_read_timeout 60s;
    
  2. Increase Apache Timeouts:

    # Apache configuration
    ProxyTimeout 60
    Timeout 60
    

Fix Configuration Errors

  1. Update Backend Server Address:

    # Nginx upstream configuration
    upstream backend {
        server backend-server:port;
    }
    
  2. Verify Proxy Configuration:

    # Nginx proxy configuration
    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
    

Step 6: Prevent Future 502 Errors

Implement Proper Monitoring

Set up automated monitoring with Zuzia.app to track backend health continuously:

  1. Add Backend Health Monitoring:

    curl -o /dev/null -s -w "%{http_code}" http://backend-server:port/health
    
    • Monitor backend availability
    • Alert when backend becomes unavailable
  2. Monitor 502 Error Rate:

    tail -1000 /var/log/nginx/access.log | awk '$9 == 502 {count++} END {print count}'
    
    • Track 502 error frequency
    • Alert on 502 error spikes

Best Practices

  1. Monitor Backend Health:

    • Implement health check endpoints
    • Monitor backend availability
    • Alert on backend failures
  2. Configure Proper Timeouts:

    • Set appropriate timeout values
    • Monitor backend response times
    • Adjust timeouts based on performance
  3. Implement Load Balancing:

    • Use multiple backend servers
    • Configure health checks
    • Distribute load evenly
  4. Monitor Web Server Logs:

    • Review logs regularly
    • Set up log aggregation
    • Alert on error patterns

FAQ: Common Questions About 502 Errors

How do I prevent 502 errors?

Prevent 502 errors by monitoring backend health, configuring proper timeouts, implementing health checks, using multiple backend servers, and monitoring web server logs continuously.

Why do I get intermittent 502 errors?

Intermittent 502 errors can be caused by backend server overload, network connectivity issues, timeout problems, or backend server restarts. Monitor backend health and performance to identify patterns.

How do I debug 502 errors?

Debug 502 errors by checking backend server status, reviewing web server logs, testing backend connectivity, verifying configuration, and monitoring backend performance.

Can 502 errors impact SEO?

502 errors can impact SEO if they occur frequently or for extended periods. Search engines may reduce page rankings or temporarily remove pages from search results. Monitor and fix 502 errors promptly.

Note: The content above is part of our brainstorming and planning process. Not all described features are yet available in the current version of Zuzia.

If you'd like to achieve what's described in this article, please contact us – we'd be happy to work on it and tailor the solution to your needs.

In the meantime, we invite you to try out Zuzia's current features – server monitoring, SSL checks, task management, and many more.

We use cookies to ensure the proper functioning of our website.