search
Tutorials

Fix: 502 Bad Gateway on PHP sites error and fix

Quick fix for 502 Bad Gateway error on PHP sites. Learn how to resolve server gateway issues and improve PHP application stability.

person By Gautam Sharma
calendar_today January 8, 2026
schedule 1 min read
PHP 502 Bad Gateway Server Nginx Apache Error Fix

The ‘502 Bad Gateway’ error occurs when a server acting as a gateway receives an invalid response from an upstream server. This commonly happens with PHP applications behind reverse proxies like Nginx.


How the Error Happens

❌ Error Scenario:

# ❌ 502 Bad Gateway response
# Server returns 502 instead of expected content

✅ Quick Fix - Resolve 502 Bad Gateway

Solution 1: Nginx Configuration

# ✅ Update nginx.conf
upstream php_backend {
    server 127.0.0.1:9000 max_fails=3 fail_timeout=30s;
}

server {
    location ~ \.php$ {
        fastcgi_pass php_backend;
        fastcgi_read_timeout 300;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
    }
}

Solution 2: PHP-FPM Configuration

# ✅ Update php-fpm pool configuration
# /etc/php/7.4/fpm/pool.d/www.conf
listen.backlog = 65535
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

Solution 3: Restart Services

# ✅ Restart PHP-FPM and web server
sudo systemctl restart php7.4-fpm
sudo systemctl restart nginx
Gautam Sharma

About Gautam Sharma

Full-stack developer and tech blogger sharing coding tutorials and best practices

Related Articles

Tutorials

Fix: 502 Bad Gateway on Vercel

Complete guide to fix '502 Bad Gateway' error on Vercel. Learn how to resolve server gateway issues and improve Vercel deployment stability.

January 8, 2026
Tutorials

Fix: Allowed memory size exhausted in WordPress error - Quick Solution

Quick fix for 'Allowed memory size exhausted' error in WordPress. Learn how to increase PHP memory limit and optimize WordPress performance.

January 8, 2026
Tutorials

Fix: 429 Too Many Requests error

Quick fix for '429 Too Many Requests' error. Learn how to implement rate limiting and handle API rate limits effectively.

January 8, 2026