Free auditStart free
GuideError · HTTP status codes

502 Bad Gateway in nginx

Fix an nginx 502 Bad Gateway: check the upstream address, socket, buffer sizes and timeouts for proxy_pass and PHP-FPM, with config you can copy and test.

4 min
read
6
sources
Oct 22, 2026
updated
Scaplet Editorial Team
author
The short answer

In nginx, a 502 Bad Gateway means nginx passed the request to an upstream (an app, PHP-FPM or another server) and did not get a valid response. The usual causes are an upstream that is down, a wrong address or socket in proxy_pass or fastcgi_pass, or a response header larger than the buffer. Test the upstream directly, then compare it with the nginx config.

  1. Why nginx returns a 502
  2. Causes specific to nginx and where to fix them
  3. Verify the fix
30-second check

Where does your site stand?

Four quick questions, then a straight answer.

1/4Have you checked your own site for this?
Summarize this page withChatGPTClaudePerplexityGoogleAI Mode
01Check it

Why nginx returns a 502

nginx is acting as the gateway in RFC 9110 terms: it takes the request and passes it to another server with proxy_pass (HTTP) or fastcgi_pass (PHP-FPM and other FastCGI apps). A 502 is the status for a gateway that "received an invalid response from an inbound server". The general meaning, who causes it and the CDN variants are on 502 Bad Gateway. This page covers what is specific to nginx.

Start with the nginx error log for the failing request, and read what it says about the upstream before you change any setting.

First three checks
# 1. Read the newest nginx errors (path depends on your error_log setting)
sudo tail -n 50 /var/log/nginx/error.log

# 2. Test the upstream on its own, bypassing nginx
curl -sSI http://127.0.0.1:8000/

# 3. Validate the config and reload
sudo nginx -t && sudo nginx -s reload
02nginx 502 causes

Causes specific to nginx and where to fix them

Cause

Nothing listening at the upstream

What to check
The address in proxy_pass; whether the app or PHP-FPM is running
Fix
Start the service and match the address. nginx accepts a domain name or IP address with an optional port, or a UNIX-domain socket path written as http://unix:/tmp/backend.socket:/uri/
Cause

Wrong socket path for PHP-FPM

What to check
The fastcgi_pass value against the socket or port the FastCGI server uses
Fix
Use the same value in both places, for example fastcgi_pass unix:/tmp/fastcgi.socket; or fastcgi_pass localhost:9000;
Cause

Upstream response header too big

What to check
proxy_buffer_size or fastcgi_buffer_size
Fix
Raise the buffer. The docs: the first part of the response "usually contains a small response header; if it exceeds the buffer size, the response is considered invalid". The default is one memory page, 4K or 8K depending on platform
Cause

Upstream closes or returns nothing valid

What to check
The upstream app's own log
Fix
Fix the app; proxy_next_upstream lists invalid_header ("a server returned an empty or invalid response") as a case nginx can retry on another server in a group
Cause

nginx cannot select an upstream server

What to check
The upstream block and the $upstream_status and $upstream_addr log variables
Fix
Bring a server in the group back. nginx documents that $upstream_status keeps the 502 status code when a server cannot be selected, so log it to separate this case from a 502 the upstream sent
Cause

Upstream is HTTPS and fails verification or the handshake

What to check
The proxy_ssl_* directives and the upstream certificate
Fix
Correct the certificate, name or protocol settings, then retest with curl against the upstream
Proxy to an app with explicit buffer and timeout settings
location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;

    proxy_connect_timeout 60s;   # default 60s
    proxy_read_timeout    60s;   # default 60s
    proxy_buffer_size     8k;    # raise only if upstream headers are large
}
PHP-FPM over a socket
location ~ \.php$ {
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/run/php/php-fpm.sock;  # must match the pool's listen setting
}
03How it works

Verify the fix

If nginx sits behind a CDN, test both the origin and the CDN hostname; a CDN can show its own 502 even when nginx is healthy. Google reads a 502 as a server error, so see 5xx server errors for the crawl impact.

  1. 01

    Reproduce on the same URL

    Request the exact URL that failed, with curl -sSI, and confirm a 2xx or the intended status rather than 502.

  2. 02

    Watch the error log while you test

    Run tail -f on the error log and repeat the request. A fixed upstream stops producing new lines for that request.

  3. 03

    Check under load and after restarts

    Restart the upstream service and test again. If the 502 only appears right after a restart, the app may not have been ready when nginx sent the first requests; add a health check or a start delay in your process manager.

Questions

Asked a lot.

4 questions people ask next, answered from the sources on this page.

01How do I fix 502 Bad Gateway in nginx?

Read the nginx error log to find the upstream it names, test that upstream directly with curl, then check the address or socket in proxy_pass or fastcgi_pass, the buffer settings, and whether the upstream service is running. Run nginx -t and reload after any change.

02Why is my nginx reverse proxy not working?

The proxy needs a reachable upstream and a valid response from it. Check that the upstream is running at the address proxy_pass points to, that the config passes nginx -t, and that the upstream's response headers fit the proxy buffer.

03Does a longer proxy_read_timeout stop 502 errors?

Not usually. It controls how long nginx waits between reads from the upstream, which matters for slow responses that end in a 504. RFC 9110 defines a 502 as an invalid response from an inbound server, so look at what the upstream sent, or failed to send.

04Is the nginx 502 error the same on Apache or Cloudflare?

The meaning is the same, because RFC 9110 defines it. The causes and settings differ by stack, and Cloudflare and CloudFront document their own.

Sources · 6

Every fact, linked and dated.

Official docs first. The date is when we last opened each page.
  • rfc-editor.org
  • nginx.org
  • mozilla.org
  • google.com
  1. 01RFC 9110: HTTP Semanticsrfc-editor.org · checked 2026-10-25
  2. 02nginx: ngx_http_proxy_modulenginx.org · checked 2026-10-25
  3. 03nginx: ngx_http_fastcgi_modulenginx.org · checked 2026-10-25
  4. 04nginx: ngx_http_upstream_modulenginx.org · checked 2026-10-25
  5. 05MDN: 502 Bad Gatewaydeveloper.mozilla.org · checked 2026-10-25
  6. 06How HTTP status codes affect Google's crawlersdevelopers.google.com · checked 2026-10-25
Free · 2 minutes

Tools point. Agencies advise. This one ships.

Run the free audit, then approve the fixes. Every change can be undone.

Free, no card60 secondsNobody will call you
  • 46 fixes shipped without a ticket Brightpipe
  • 0→7 ChatGPT citations in six weeks Paylane