Skip to content

502 Bad Gateway Error

Introduction

A 502 Bad Gateway error is an HTTP status code that indicates a server (usually a reverse proxy or gateway) received an invalid response from the upstream server it was trying to access.

Common Causes

1. Backend Server Not Responding

  • Application server (PHP-FPM, Node.js, Python) crashed or not running
  • Database server connection failure
  • Request processing timeout

2. Reverse Proxy Issues

  • Nginx or Apache misconfigured upstream
  • Timeout settings too short
  • Insufficient buffer size

3. System Resources

  • Out of RAM
  • CPU overload
  • Disk space full

4. Network Issues

  • Firewall blocking connections
  • DNS resolution failure
  • Closed ports

Troubleshooting

Check Application Server

  1. Go to FlashPanel
  2. Select your Server
  3. Go to the Applications tab
  4. Check the status in the applications list:
    • Active: Application is running normally
    • Inactive: Application is stopped or has issues
  5. If status is Inactive, click on the application to view details and restart it

Via Command Line

PHP-FPM

bash
# Check PHP-FPM status
sudo systemctl status php8.2-fpm
sudo systemctl status php-fpm

# View logs
sudo tail -f /var/log/php8.2-fpm.log
sudo tail -f /var/log/php-fpm/error.log

# Restart service
sudo systemctl restart php8.2-fpm

Node.js / PM2

bash
# Check processes
pm2 list
pm2 status

# View logs
pm2 logs

# Restart
pm2 restart all

Check Nginx

  1. Access your site in FlashPanel
  2. Go to the Nginx tab
  3. Click the Error Log button to view error logs
  4. Check for any errors related to upstream connections or timeouts

Via Command Line

bash
# Test configuration
sudo nginx -t

# View logs
sudo tail -f /var/log/nginx/error.log
sudo tail -f /var/log/nginx/access.log

# Restart Nginx
sudo systemctl restart nginx

Nginx Timeout Configuration

nginx
http {
    # Increase timeout settings
    proxy_connect_timeout 600;
    proxy_send_timeout 600;
    proxy_read_timeout 600;
    send_timeout 600;
    
    # Increase buffer size
    proxy_buffer_size 128k;
    proxy_buffers 4 256k;
    proxy_busy_buffers_size 256k;
}

server {
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_read_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_connect_timeout 300;
    }
}

Check System Resources

bash
# Check RAM
free -h

# Check CPU
top
htop

# Check disk space
df -h

# Check processes
ps aux | grep php-fpm
ps aux | grep nginx
ps aux | grep node

Check Connections

bash
# Test connection to upstream
telnet localhost 9000  # PHP-FPM
telnet localhost 3000  # Node.js app

# Check listening ports
sudo netstat -tulpn | grep LISTEN
sudo ss -tulpn | grep LISTEN

# Test DNS
nslookup domain.com
dig domain.com

Debugging Laravel Application

Check Laravel logs

bash
tail -f storage/logs/laravel.log

Increase PHP memory limit

ini
# php.ini
memory_limit = 512M
max_execution_time = 300

Check PHP-FPM pool config

ini
; /etc/php/8.2/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 20
pm.max_requests = 500

Quick Checklist

  1. ✅ Check application server is running
  2. ✅ Check logs (nginx, php-fpm, application)
  3. ✅ Check system resources (RAM, CPU, Disk)
  4. ✅ Check timeout configuration
  5. ✅ Check network connectivity
  6. ✅ Restart necessary services

Useful Tools

  • htop: Real-time CPU and RAM monitoring
  • iotop: Disk I/O monitoring
  • nethogs: Per-process bandwidth monitoring
  • tcpdump: Network traffic analysis
  • strace: System call debugging
  • lsof: List open files and network connections