# 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.

Canonical: https://scaplet.com/errors/http/502/nginx

Guide Error · HTTP status codes

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

502 5xx · Server error

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](#why-nginx-returns-a-502)
2. [Causes specific to nginx and where to fix them](#causes-specific-to-nginx-and-where-to-fix-them)
3. [Verify the fix](#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 with [ChatGPT](https://chatgpt.com/?hints=search&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F502%2Fnginx%20(%22502%20Bad%20Gateway%20in%20nginx%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Claude](https://claude.ai/new?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F502%2Fnginx%20(%22502%20Bad%20Gateway%20in%20nginx%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [Perplexity](https://www.perplexity.ai/search?q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F502%2Fnginx%20(%22502%20Bad%20Gateway%20in%20nginx%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.) [![Google](/logos/ai/google-color.svg)AI Mode](https://www.google.com/search?udm=50&q=Summarize%20https%3A%2F%2Fscaplet.com%2Ferrors%2Fhttp%2F502%2Fnginx%20(%22502%20Bad%20Gateway%20in%20nginx%22)%20in%205%20bullet%20points%2C%20then%20tell%20me%20the%20one%20thing%20I%20should%20do%20next.)

_01_Check 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](/glossary/inbound-marketing) server". The general meaning, who causes it and the CDN variants are on [502 Bad Gateway](/errors/http/502). 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
```

_02_nginx 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](/glossary/http-status-codes) when a server cannot be selected, so log it to separate this case from a 502 the upstream sent

Cause

### Upstream is [HTTPS](/glossary/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 }
```

**Do not raise timeouts to fix a 502**A longer `proxy_read_timeout` helps a 504 (slow upstream), not a 502 (invalid or refused reply). The directive sets a timeout for reading a response from the proxied server, applied between two successive reads, not to the whole response. If the symptom is a timeout, go to [504 Gateway Timeout](/errors/http/504).

_03_How 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](/seo-issues/server-errors-5xx) 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.

_01_ How 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.

_02_ Why 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.

_03_ Does 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.

_04_ Is 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. 01 [RFC 9110: HTTP Semantics](https://www.rfc-editor.org/rfc/rfc9110.html) rfc-editor.org · checked 2026-10-25
2. 02 [nginx: ngx_http_proxy_module](https://nginx.org/en/docs/http/ngx_http_proxy_module.html) nginx.org · checked 2026-10-25
3. 03 [nginx: ngx_http_fastcgi_module](https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html) nginx.org · checked 2026-10-25
4. 04 [nginx: ngx_http_upstream_module](https://nginx.org/en/docs/http/ngx_http_upstream_module.html) nginx.org · checked 2026-10-25
5. 05 [MDN: 502 Bad Gateway](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/502) developer.mozilla.org · checked 2026-10-25
6. 06 [How HTTP status codes affect Google's crawlers](https://developers.google.com/crawling/docs/troubleshooting/http-status-codes) developers.google.com · checked 2026-10-25

## Keep learning

[All errors](/errors)

[Learn 502 Bad Gateway HTTP status codes](/errors/http/502) [Related 404 Not Found on nginx HTTP status codes](/errors/http/404/nginx) [Read next 502 Bad Gateway in Apache HTTP status codes](/errors/http/502/apache) [Do Run a free site audit Free, about two minutes](/site-audit)

Other 502 errors
[Render 502 Bad Gateway](/errors/cdn/render-502) [502 Bad Gateway](/errors/http/502) [502 Bad Gateway in Apache](/errors/http/502/apache) [502 Bad Gateway on Azure](/errors/http/502/azure) [502 Bad Gateway on Cloudflare](/errors/http/502/cloudflare)

Nearby in errors
[503 Service Unavailable](/errors/http/503) [503 Service Unavailable in Apache](/errors/http/503/apache)

More in 502 Bad Gateway
[502 Bad Gateway in Apache](/errors/http/502/apache) [502 Bad Gateway in nginx](/errors/http/502/nginx) [502 Bad Gateway on Azure](/errors/http/502/azure) [502 Bad Gateway on Cloudflare](/errors/http/502/cloudflare)

Terms on this page
[Inbound marketing](/glossary/inbound-marketing) [HTTP status codes](/glossary/http-status-codes) [HTTPS](/glossary/https)

Free · 2 minutes

## Tools point. Agencies advise. _This one ships._

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

[Audit my site free](/site-audit)

Free, no card 60 seconds Nobody will call you

- **46** fixes shipped without a ticket Brightpipe
- **0→7** ChatGPT citations in six weeks Paylane
