RackUp IT - View Cloud Hosting Plans

Get started now or speak directly with our engineering team for specialized assistance.

Logo
DevelopmentWEB & APPCalculatorTOOLServicesProductBlogOur TeamCareers
View Plans
Home>Blog>502 Bad Gateway in Nginx: Causes and How to Fix It
VPS Hosting

502 Bad Gateway in Nginx: Causes and How to Fix It

Alex Vance
Alex Vance
Published Oct 7, 2026· Updated Nov 13, 2026•✓ Verified Expert Reviewed•9 min read
502 Bad Gateway in Nginx: Causes and How to Fix It

Key Takeaways

  • ✓A 502 means Nginx is fine but the app behind it is down, unreachable or returning a broken response.
  • ✓The Nginx error log tells you exactly which of the 10 causes you have.
  • ✓Copy-paste fixes for PHP-FPM, Node.js, Gunicorn and Docker upstreams.
  • ✓How 502 differs from 504 and Cloudflare 502 errors.

Table of Contents

  • •502 Bad Gateway in Nginx: Causes and How to Fix It
  • •What Causes a 502 Bad Gateway?
  • •Step 1: Read the Nginx Error Log
  • •Fix 1: The Upstream Is Not Running
  • •Fix 2: Nginx Points at the Wrong Address or Port
  • •Fix 3: PHP-FPM Socket Path Mismatch
  • •Fix 4: Permission Denied on the Socket
  • •Fix 5: Upstream Headers Too Big
  • •Fix 6: The App Crashes or Runs Out of Memory
  • •Fix 7: PHP-FPM Has No Free Workers
  • •Fix 8: The Upstream Resets the Connection
  • •Fix 9: A Backend on Another Server Can’t Be Reached
  • •Fix 10: Nginx Can’t Resolve the Upstream Hostname
  • •502 Bad Gateway on WordPress
  • •502 Bad Gateway with Docker
  • •Apply Changes Safely
  • •502 vs 504 vs Cloudflare 502
  • •Frequently Asked Questions
  • •Rather Not Debug Nginx at 2 AM?

502 Bad Gateway in Nginx: Causes and How to Fix It

A 502 Bad Gateway page with nginx (or nginx/1.24.0, nginx/1.29.1 and so on) at the bottom means Nginx itself is running, but it is acting as a reverse proxy and the application behind it did not return a valid response. That application is called the upstream: usually PHP-FPM for WordPress and Laravel, a Node.js process, Gunicorn or uWSGI for Python, or a Docker container.

So the fix is almost never in Nginx alone. You need to find out why the upstream failed, and the Nginx error log tells you.

Not running Nginx, or only visiting a site that shows this error? Read 502 Bad Gateway: what it means and how to fix it.

What Causes a 502 Bad Gateway?

A 502 means the server in front (Nginx) got no valid response from the app behind it. The usual causes:

  • The app (PHP-FPM, Node.js, Gunicorn or a container) isn’t running: Fix 1.

  • Nginx points at the wrong address, port or socket: Fixes 2 and 3.

  • Socket permissions stop Nginx connecting: Fix 4.

  • Response headers are too big for Nginx’s buffers: Fix 5.

  • Every app worker is busy, or the app was killed for using too much memory.

  • The upstream closed the connection or can’t be reached (errors 104, 110, 113).

  • The upstream’s DNS changed after Nginx started.

For Python apps, the most common setup is Gunicorn behind Nginx; see deploying Django on a VPS with Gunicorn and Nginx for a working configuration.

Just visiting a site that shows 502? It’s a problem on the site’s server, not your device; try again in a minute. If you run the site, start with the error log in Step 1.

Step 1: Read the Nginx Error Log

Run this on the server, then reload the failing page and run it again:

sudo tail -n 50 /var/log/nginx/error.log

If your site has its own log, check the error_log line in its server block. Then match the message:

Error log message

What it means

Fix

connect() failed (111: Connection refused) while connecting to upstream

Nothing is listening on the upstream address

Fix 1 and 2

connect() to unix:/run/php/php8.3-fpm.sock failed (2: No such file or directory)

PHP-FPM socket path is wrong or PHP-FPM is stopped

Fix 3

connect() to unix:... failed (13: Permission denied)

Nginx cannot open the socket or port

Fix 4

upstream sent too big header while reading response header from upstream

Response headers (often cookies) exceed the buffer

Fix 5

upstream prematurely closed connection while reading response header

The app crashed or was killed mid-request

Fix 6

no live upstreams while connecting to upstream

Every server in an upstream block is marked down

Fix 1 and 2 for each backend

connect() to unix:/run/php/php8.3-fpm.sock failed (11: Resource temporarily unavailable)

PHP-FPM has no free workers and its queue is full

Fix 7

recv() failed (104: Connection reset by peer) while reading response header from upstream

The upstream dropped the connection mid-request

Fix 8

connect() failed (113: No route to host) or (110: Connection timed out) while connecting to upstream

A backend on another server can’t be reached

Fix 9

no resolver defined to resolve ... or could not be resolved

Nginx can’t look up the upstream’s hostname

Fix 10

Fix 1: The Upstream Is Not Running

Check the service behind Nginx:

# PHP (WordPress, Laravel)
sudo systemctl status php8.3-fpm

# Node.js under PM2
pm2 status

# Python under systemd
sudo systemctl status gunicorn

# Docker
docker ps -a

If it is stopped or restarting in a loop, start it and read its own logs (journalctl -u php8.3-fpm -n 50, pm2 logs, docker logs --tail 100 CONTAINER). Then test the upstream directly, bypassing Nginx:

curl -I http://127.0.0.1:3000

If that fails too, the problem is the app, not Nginx.

Fix 2: Nginx Points at the Wrong Address or Port

Compare what Nginx proxies to with what is actually listening:

sudo nginx -T | grep -E "proxy_pass|fastcgi_pass|uwsgi_pass"
sudo ss -tulpn

Common mismatches:

  • The app moved from port 3000 to 8080 but proxy_pass was not updated.

  • Docker: if Nginx runs on the host, proxy to the published port (127.0.0.1:8080 for -p 8080:80). If Nginx runs in a container, proxy to the service name on the shared network (http://app:3000), never localhost, which points at the Nginx container itself.

  • The app listens only on 127.0.0.1 inside its container. It must listen on 0.0.0.0 to be reachable from outside the container.

Fix 3: PHP-FPM Socket Path Mismatch

After a PHP upgrade the socket name changes (for example php8.1-fpm.sock to php8.3-fpm.sock) but the Nginx config still points at the old one. Check both sides:

grep -E "^listen" /etc/php/*/fpm/pool.d/www.conf
ls -l /run/php/
sudo nginx -T | grep fastcgi_pass

Make fastcgi_pass unix:/run/php/php8.3-fpm.sock; match the real socket, then reload.

Fix 4: Permission Denied on the Socket

The Nginx worker user (www-data on Ubuntu/Debian, nginx on RHEL, AlmaLinux and Oracle Linux) must be allowed to open the PHP-FPM socket. In the pool file set:

listen.owner = www-data
listen.group = www-data
listen.mode = 0660

Restart PHP-FPM. On RHEL-family systems with SELinux enforcing, Nginx is also blocked from connecting to network upstreams until you allow it:

sudo setsebool -P httpd_can_network_connect 1

Fix 5: Upstream Headers Too Big

Large cookies or many Set-Cookie headers (common with WooCommerce, SSO and some page builders) overflow the default buffers. Raise them in the server or location block:

# Reverse proxy (Node, Python, Docker)
proxy_buffer_size 16k;
proxy_buffers 8 16k;
proxy_busy_buffers_size 32k;

# PHP-FPM
fastcgi_buffer_size 32k;
fastcgi_buffers 16 16k;

Fix 6: The App Crashes or Runs Out of Memory

If the log says the upstream closed the connection, the process died while handling the request. The usual cause on small VPS plans is memory:

sudo dmesg -T | grep -i -E "killed process|out of memory"

If the kernel OOM killer is ending PHP-FPM, MySQL or Node, either reduce workers (pm.max_children for PHP-FPM), add swap, or move to a plan with more RAM. Our guide to fixing out-of-memory crashes with a swap file walks through the swap fix.

Fix 7: PHP-FPM Has No Free Workers

If the Nginx log shows (11: Resource temporarily unavailable) on the PHP-FPM socket, every PHP worker is busy and the queue is full. PHP-FPM usually warns about it in its own log:

sudo grep max_children /var/log/php8.3-fpm.log
# WARNING: [pool www] server reached pm.max_children setting (5), consider raising it

Raise pm.max_children, but only as far as your RAM allows. First check how much memory one worker uses:

ps --no-headers -o rss -C php-fpm8.3 | awk '{sum+=$1} END {print sum/NR/1024 " MB per worker"}'

Divide the RAM you can spare for PHP by that number, set the result in /etc/php/8.3/fpm/pool.d/www.conf and restart:

pm.max_children = 20

sudo systemctl restart php8.3-fpm

Setting it higher than your RAM can hold just swaps this error for the out-of-memory crash in Fix 6. If workers are busy because requests are slow, turn on the slow log (request_slowlog_timeout = 5s and slowlog = /var/log/php8.3-fpm-slow.log) to see which scripts hold them up.

Fix 8: The Upstream Resets the Connection

recv() failed (104: Connection reset by peer) means the app closed the connection before sending a full response. Common causes:

  • PHP-FPM killed a long request: look for execution timed out in the PHP-FPM log, then fix the slow code or raise request_terminate_timeout in the pool config.

  • A worker crashed: exited on signal 11 in the PHP-FPM log points to a crashing extension; update or disable it.

  • Gunicorn killed a slow worker: WORKER TIMEOUT in the Gunicorn log means a request took longer than its timeout (30 seconds by default). Speed up the view or raise --timeout.

  • The app restarted during a deploy: requests in flight at that moment fail. This one fixes itself once the app is back.

Fix 9: A Backend on Another Server Can’t Be Reached

When Nginx proxies to an app on a different server, (113: No route to host) or (110: Connection timed out) while connecting to upstream usually means a firewall is blocking the port, or the private IP is wrong. Test from the Nginx server:

nc -zv 10.0.0.5 3000

If it times out, open the port on the backend for the Nginx server only, for example sudo ufw allow from NGINX_SERVER_IP to any port 3000 proto tcp, and check any cloud firewall in front of it. Our guide on how to open ports on a VPS covers UFW, firewalld and iptables.

Fix 10: Nginx Can’t Resolve the Upstream Hostname

no resolver defined to resolve ... appears when proxy_pass uses a variable, for example set $backend http://app:3000; proxy_pass $backend;. Nginx then looks the name up at request time and needs a resolver. In Docker, point it at Docker’s built-in DNS:

resolver 127.0.0.11 valid=30s;

On a normal server use your system’s DNS server instead. If the log says could not be resolved, the name itself is wrong: check the container or host name with docker ps or getent hosts app.

502 Bad Gateway on WordPress

On WordPress, a PHP fatal error from a plugin or theme normally shows a 500 error or a “critical error” page, not a 502. A 502 means PHP-FPM itself didn’t answer: it’s stopped (Fix 1), the socket path or permissions are wrong after an update (Fixes 3 and 4), all workers are busy (Fix 7), or a worker was killed mid-request, often by the out-of-memory killer (Fix 6). Heavy plugins, imports and WooCommerce sales are the usual triggers, so check the PHP-FPM log and dmesg first.

502 Bad Gateway with Docker

When Nginx runs in front of containers, most 502s come down to addressing:

  • Use the service name, not localhost: inside the Nginx container, localhost is the Nginx container itself. Proxy to the Compose service name, for example proxy_pass http://app:3000;, and make sure both containers are on the same network.

  • The app must listen on 0.0.0.0: an app bound to 127.0.0.1 inside its container can’t be reached from Nginx.

  • Check the container is up: docker ps and docker logs --tail 100 app show whether it crashed or is restarting.

  • Restarted containers get new IPs: if Nginx resolved the name once at start-up, use a resolver with a short valid= time (Fix 10) or restart Nginx after the app container restarts.

Apply Changes Safely

Always test before reloading so a typo does not take every site down:

sudo nginx -t && sudo systemctl reload nginx

See common Nginx errors and fixes for the other status codes.

502 vs 504 vs Cloudflare 502

  • 502 Bad Gateway: the upstream refused the connection or returned something invalid. It usually appears instantly.

  • 504 Gateway Timeout: the upstream accepted the request but did not answer in time. Look at slow queries or raise proxy_read_timeout / fastcgi_read_timeout.

  • Cloudflare-branded 502: if the error page shows the Cloudflare logo instead of nginx, Cloudflare could not get a valid response from your origin server at all. Check that the server is up and reachable on ports 80/443.

Frequently Asked Questions

What causes 502 Bad Gateway in Nginx?
Nginx is running as a reverse proxy, but the application behind it (PHP-FPM, Node.js, Gunicorn or a container) is stopped, listening on a different address, blocking the connection, crashing, or sending headers too large for the buffers.

Is a 502 error my fault or the website's?
As a visitor, it is almost always the website's server. Refreshing after a minute sometimes helps if the app was restarting. As the site owner, check the Nginx error log.

How do I fix 502 Bad Gateway with PHP-FPM?
Make sure PHP-FPM is running, that fastcgi_pass points at the real socket in /run/php/, and that the socket is owned by the Nginx user. Then run nginx -t and reload.

Why do I get 502 Bad Gateway with Docker and Nginx?
Usually because Nginx proxies to localhost from inside a container, or the app listens on 127.0.0.1 instead of 0.0.0.0. Proxy to the service name on a shared Docker network or to the published host port.

Does restarting Nginx fix a 502?
Rarely. Restart the upstream app instead; Nginx is only reporting the failure.

Rather Not Debug Nginx at 2 AM?

Every error in this guide is something a managed host fixes for you. RackUp IT managed hosting runs a tuned Nginx, PHP and caching stack with monitoring, backups and SSL handled, so you can spend your time on the site instead of the server.

← Previous ArticleIs cPanel Free? What cPanel Hosting Costs in 2026Next Article →Vercel Alternatives for Next.js: 7 Options Compared (2026)
Interactive 60-Second Tool

Unsure Which Cloud Hosting Plan Fits Your Site?

Answer 4 quick diagnostic questions to estimate the server CPU, RAM and storage you need with transparent pricing.

Free Migration 99.9% Uptime Target
Calculate My Hosting Build
Alex Vance
Written ByAlex Vance

Cloud Infrastructure Engineer.

99.9% Uptime Target

RackUp IT - View Cloud Hosting Plans

View Hosting Plans

View Hosting Plans

CONTENTS

  • 502 Bad Gateway in Nginx: Causes and How to Fix It
  • What Causes a 502 Bad Gateway?
  • Step 1: Read the Nginx Error Log
  • Fix 1: The Upstream Is Not Running
  • Fix 2: Nginx Points at the Wrong Address or Port
  • Fix 3: PHP-FPM Socket Path Mismatch
  • Fix 4: Permission Denied on the Socket
  • Fix 5: Upstream Headers Too Big
  • Fix 6: The App Crashes or Runs Out of Memory
  • Fix 7: PHP-FPM Has No Free Workers
  • Fix 8: The Upstream Resets the Connection
  • Fix 9: A Backend on Another Server Can’t Be Reached
  • Fix 10: Nginx Can’t Resolve the Upstream Hostname
  • 502 Bad Gateway on WordPress
  • 502 Bad Gateway with Docker
  • Apply Changes Safely
  • 502 vs 504 vs Cloudflare 502
  • Frequently Asked Questions
  • Rather Not Debug Nginx at 2 AM?

Categories

  • VPS Hosting
  • DevOps & Tutorials
  • WordPress Hosting
  • Cloud Hosting
  • Server Stack
  • Networking & Security
  • Web Hosting / Managed Cloud
  • E-Commerce
  • Hosting & SEO
  • Hosting Benchmarks
  • Cloud Backup & DevOps
  • Managed VPS

Recent Posts

  • Vultr Alternatives in 2026: VPS Hosting for Developers Compared
  • Docker Port Mapping Explained: -p, EXPOSE and Compose ports
  • Bluehost Alternatives (2026): WordPress Hosting Without the Renewal Price Jump

Tags

Nginx502 Bad GatewayTroubleshootingVPS HostingPHP-FPMDocker

Related Posts

Vultr alternatives coverVPS Hosting
Nov 27, 2026

Vultr Alternatives in 2026: VPS Hosting for Developers Compared

Vultr's Regular Performance prices compared with DigitalOcean, Linode, Hetzner and a fixed-plan VPS at the 8 GB size, plus when container hosting fits better.

Read Article →
What is a VPS? Virtual private servers explainedVPS Hosting
Nov 20, 2026

What Is a VPS? Virtual Private Servers Explained

A VPS (virtual private server) is a virtual machine with its own operating system, resources and root access. How it works, uses, and when you need one.

Read Article →
Linode alternatives coverVPS Hosting
Nov 19, 2026

Linode (Akamai) Alternatives in 2026: VPS Hosting Compared

Linode's shared CPU prices from Akamai's own API, compared with DigitalOcean, Vultr and a fixed-plan VPS, plus when container hosting makes more sense.

Read Article →
Logo

Expert Web & App Development, UI/UX Design, Branding, and Consulting to scale your business.

Company

  • About Us
  • Blog
  • Contact
  • Our Team
  • Careers

Services

  • Web Development
  • App Development
  • UI/UX Design
  • Consulting
  • Managed WordPress VPS
  • Website Speed Test

Subscribe to our newsletter

We accept
  • bKash
  • Nagad
  • Rocket
  • Upay
  • CellFin
  • Meghna Pay
  • OK Wallet
  • Tap
  • mCash
  • DBBL Nexus
  • Visa
  • Mastercard
  • American Express
  • Crypto
Pay with Visa, Mastercard, American Express, DBBL Nexus, CellFin, Rocket, bKash, Tap, Meghna Pay, Nagad, mCash, Upay and OK Wallet. Verified by EPS

© 2026 Rackup IT Solution. All rights reserved.

Privacy PolicyTerms of Service