When a site starts throwing errors, most debugging time is lost in the wrong place: restarting Nginx when PHP-FPM is the problem, editing .htaccess when Cloudflare is the one saying no, raising PHP's max_execution_time when the proxy gave up at 60 seconds anyway.
The status code alone doesn't tell you where to look. What tells you is which layer generated the response. A typical request passes through four of them, and any of them can answer with an error:
browser -> CDN / proxy (Cloudflare) -> web server (Nginx, Apache, LiteSpeed) -> app (PHP-FPM, Node, Gunicorn)
This post is a triage sheet: first work out who sent the error, then read the two things that usually explain it. Each code has a longer fix-it guide linked in its section.
Step 1: who generated the error?
| What you see | Who generated it | Look first at |
|---|---|---|
Browser error with no HTTP code: ERR_CONNECTION_REFUSED, DNS_PROBE_FINISHED_NXDOMAIN, ERR_SSL_PROTOCOL_ERROR
|
The browser. No HTTP response arrived at all | DNS, the port, the firewall, TLS |
| Chrome's own "This page isn't working ... HTTP ERROR 500" | The server sent a 5xx with an empty body; Chrome drew the page | Server error logs |
| Cloudflare-branded page with a Ray ID and a 403 | Cloudflare itself (WAF rule, security level, Browser Integrity Check) | Security Events in the Cloudflare dashboard |
| Cloudflare-branded page with a 502 or 504 | Your origin returned it; Cloudflare displayed it | Origin web server logs |
| Cloudflare page with a 52x code | Cloudflare, about its connection to your origin | The 52x list further down |
Plain white page, "502 Bad Gateway" with an nginx footer |
Nginx, about the backend behind it | /var/log/nginx/error.log |
| "Forbidden: You don't have permission to access this resource." | Apache (or LiteSpeed) itself | Apache error log |
| Framework-styled page or a JSON error body | The application | The app's own log; the web server's error log is often empty |
That last row trips people up. If your application decides to return a 403 or 500, the web server just passes it along. Nginx logs it in the access log, but there's nothing in error.log because, from Nginx's point of view, nothing went wrong. An error in the access log with no matching line in the error log usually means the app sent it.
The browser-only errors in the first row aren't HTTP errors at all. If that's what you have, start with a guide like fixing ERR_CONNECTION_REFUSED instead.
Step 2: confirm it with curl
Browser error pages can mislead (cached pages, extensions, a friendly error page served with a 200). curl shows the raw status and headers:
curl -sI https://example.com/page
Look at three things:
-
The status line.
HTTP/2 502is the real code. -
cf-ray. If it's present, the response passed through Cloudflare. Note thatserver: cloudflareappears on every proxied response, including errors your origin generated, so it proves nothing on its own. -
The body.
curl -s https://example.com/page | head -20shows whether it's an Nginx default page, an Apache page, a Cloudflare page or your app's template.
Then take the CDN out of the picture and ask the origin directly:
# Bypass the CDN: connect straight to the origin IP, keep the real hostname
curl -skI --resolve example.com:443:203.0.113.10 https://example.com/page
# Or from the server itself
curl -skI --resolve example.com:443:127.0.0.1 https://example.com/page
--resolve keeps the correct hostname for SNI and the Host header, so you hit the right virtual host. -k skips certificate checks, which you need if the origin uses a Cloudflare Origin CA certificate; use it only for this test. If the origin answers 200 here but visitors see an error, the problem is between the CDN and the origin, not in your code.
403 Forbidden: something refused on purpose
A 403 means the request was understood and refused. It's a decision, not a crash, so the question is which rule made it.
Nginx, first two checks:
sudo tail -n 50 /var/log/nginx/error.log
namei -l /var/www/example.com/public/index.php
The log line names the cause: directory index of "/var/www/html/" is forbidden (no index file, autoindex off), open() "..." failed (13: Permission denied) (filesystem permissions), or access forbidden by rule (a deny in the matching location). namei -l lists the permissions of every folder in the path; one folder at 700 anywhere above the file blocks the whole site.
Apache, first two checks:
sudo tail -n 50 /var/log/apache2/error.log # /var/log/httpd/error_log on AlmaLinux/Rocky
sudo apachectl -S
AH01630: client denied by server configuration is a Require rule. AH01276: Cannot serve directory is a missing index file. AH00035 ... search permissions are missing is a parent folder without execute permission. apachectl -S shows which virtual host answers for the domain, so you can check its DocumentRoot is what you think it is.
PHP-FPM: if the page body is just Access denied., PHP-FPM refused to run the file because its extension isn't in security.limit_extensions (by default only .php and .phar). That usually means Nginx is passing non-PHP files to FPM.
If none of these logged anything, the app sent the 403 (a REST API permission check, a security plugin), or a WAF did. On AlmaLinux and Rocky also check SELinux with sudo ausearch -m avc -ts recent; wrong file labels look exactly like a permission problem even when ls -l is fine.
Full list of causes, including hotlink protection and Cloudflare IP blocks: how to fix a 403 Forbidden error.
500 Internal Server Error: something crashed and didn't say why
A 500 is the generic "unexpected condition" code. The page tells you nothing; the log tells you everything.
Apache or LiteSpeed (and cPanel hosting), first two checks:
- The error log. On cPanel that's Metrics > Errors, plus any
error_logfile in the folder of the failing script. -
.htaccess. A line likeInvalid command 'RewriteEngin', perhaps misspelledorOptions not allowed hereis a 500 on every request. Rename the file for a minute to confirm, then rename it back.
Nginx + PHP-FPM, first two checks:
sudo tail -f /var/log/nginx/error.log /var/log/php8.3-fpm.log
php -l /var/www/example.com/public/index.php
With the default PHP settings, a fatal error often shows up in the Nginx log as FastCGI sent in stderr: "PHP message: PHP Fatal error: ...", with file and line. The usual suspects are Allowed memory size ... exhausted, Call to undefined function (wrong PHP version or missing extension) and Maximum execution time ... exceeded. php -l catches syntax errors after a bad deploy, but remember the CLI php can be a different version from the one FPM runs.
Nginx can also produce a 500 itself: rewrite or internal redirection cycle means a try_files or rewrite loops back on itself.
Do not turn on display_errors on a live site to see the message. It shows file paths, and sometimes credentials, to every visitor. More on PHP versions, permissions, CGI scripts and WordPress in how to fix a 500 Internal Server Error.
502 Bad Gateway: the backend gave a bad answer (or none)
A 502 comes from a proxy. The front server is usually fine; the thing behind it is stopped, listening somewhere else, crashed mid-request, or sent something the proxy couldn't use.
Nginx, first two checks:
sudo tail -n 50 /var/log/nginx/error.log
systemctl status php8.3-fpm # or php-fpm on AlmaLinux/Rocky, or your app's unit
Match the log line:
| Nginx log message | Meaning | Next check |
|---|---|---|
connect() failed (111: Connection refused) |
Nothing listening on that IP:port |
sudo ss -ltnp, compare with proxy_pass
|
connect() to unix:/run/php/... failed (2: No such file or directory) |
Socket doesn't exist | FPM stopped, or socket name changed after a PHP upgrade |
connect() to unix:... failed (13: Permission denied) |
Nginx can't open the socket |
listen.owner / listen.group / listen.mode, or SELinux |
connect() to unix:... failed (11: Resource temporarily unavailable) |
FPM's listen backlog is full |
pm.max_children reached |
upstream prematurely closed connection |
Backend took the request and hung up | OOM kill, worker timeout, segfault |
upstream sent too big header |
Response headers overflowed the buffer |
fastcgi_buffer_size / proxy_buffer_size
|
Two follow-ups cover most of the rest:
sudo grep max_children /var/log/php8.3-fpm.log
sudo dmesg -T | grep -i -E "killed process|out of memory"
Apache: when mod_proxy or proxy_fcgi can't connect to the backend at all, Apache usually returns 503, not 502, and logs something like AH01114: HTTP: failed to make connection to backend. So on Apache, a "backend is down" problem often looks like a 503.
SELinux: on Red Hat-family systems, Nginx can't connect to arbitrary ports like 3000 or 8000 by default. The fix is sudo setsebool -P httpd_can_network_connect 1, not disabling SELinux.
Socket mismatches, pool sizing and the Cloudflare side are covered in detail in how to fix a 502 Bad Gateway error.
504 Gateway Timeout: the backend was too slow
A 504 also comes from a proxy, but this time nothing came back before the timer ran out. The server showing the error is almost never the slow one.
Nginx, first two checks:
sudo grep "upstream timed out" /var/log/nginx/error.log | tail
curl -s -o /dev/null -w "connect %{time_connect} tls %{time_appconnect} first-byte %{time_starttransfer} total %{time_total} code %{http_code}\n" https://example.com/slow-page
The log line includes the request: (which URL) and the upstream: (which backend). The curl timing separates network from processing: if connect and TLS take milliseconds and first byte takes 60 seconds, the delay is in the application.
Apache: a proxy timeout logs (70007)The timeout specified has expired. The limit is ProxyTimeout, which falls back to the main Timeout directive; distributions set that differently, so check your config instead of assuming.
PHP-FPM: turn on the slow log (request_slowlog_timeout = 5s and a slowlog path in the pool file), reload, reproduce, and read the backtrace. It tells you whether the script was stuck in a database call, a curl_exec to some API, or a loop. Pair it with the MySQL slow query log; fixing slow MySQL queries covers that side.
Know which timer fires first. Nginx's fastcgi_read_timeout and proxy_read_timeout default to 60 seconds, and they measure the gap between two reads from upstream, not the whole response. PHP's max_execution_time defaults to 30 seconds but on Linux doesn't count time spent waiting on streams and system calls. If PHP is allowed 300 seconds and Nginx waits 60, the visitor gets a 504 at 60 while the script keeps running. Also: if PHP-FPM's request_terminate_timeout kills the worker, Nginx usually reports a 502, not a 504.
Raise a timeout only for the one path that legitimately needs it, and move imports, exports and bulk jobs into a queue or a cron job. More in how to find and fix a 504 Gateway Timeout.
Cloudflare's 52x look-alikes
When Cloudflare can't get a usable answer from your origin, it uses its own 52x codes instead of the standard ones. They map onto the classic errors like this:
| Cloudflare code | What happened | Closest classic error | First check |
|---|---|---|---|
| 520 | Origin returned an empty, unknown or unexpected response | 502 | Origin error log at that time; curl --resolve straight to the origin |
| 521 | Origin refused the connection | 502 (connection refused) | Is the web server running? Is the firewall rejecting Cloudflare's IP ranges? |
| 522 | Connection to the origin timed out before it was established | 504-ish, but at the network level | Firewall silently dropping Cloudflare's IPs, wrong IP in the DNS record, overloaded server |
| 523 | Origin unreachable | None (routing) | The origin IP in your DNS record; is the server online at all? |
| 524 | Connected, but no HTTP response within 125 seconds (default) | 504 | The same slow-request hunt as a 504 |
| 525 | TLS handshake with the origin failed | None (TLS) | Is the origin serving HTTPS on 443 with SSL mode Full or Full (strict)? |
| 526 | Origin certificate invalid (Full strict mode) | None (TLS) | Expired, self-signed or wrong-hostname certificate on the origin |
And on Cloudflare specifically:
- A Cloudflare-branded 502 or 504 means your origin returned that code. Debug the origin.
- A plain, unbranded 502 or 504 on a proxied site came from Cloudflare itself, not your origin.
- A slow origin usually shows up as a Cloudflare-branded 504, not a 524: your own Nginx or load balancer gives up at 60 seconds, long before Cloudflare's 125. If Cloudflare had timed out first, you'd see a 524.
When you open a ticket with Cloudflare, include the Ray ID, the time with time zone, the URL, and the output of https://example.com/cdn-cgi/trace.
One change that makes all of this faster
Most of the guesswork above disappears if Nginx logs upstream timing and status next to each request. Add a log format in the http block:
log_format timed '$remote_addr [$time_local] "$request" $status '
'rt=$request_time urt=$upstream_response_time ust=$upstream_status';
access_log /var/log/nginx/access.log timed;
Now a 500 with ust=500 means the app sent it, and a 504 with urt=60.001 tells you exactly which timer fired. Test with sudo nginx -t and reload.
With the default combined format, this one-liner shows which URLs are producing 5xx errors right now:
awk '$9 ~ /^5/ {print $9, $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head
Where this applies on our side
We run hosting in India, and these four codes come up so often in hosting support that each has its own guide linked above. On a KVM VPS you have root, so every command here works as written, and the out-of-band console gets you back in if a config change locks you out. On Web & cPanel hosting, sites run on LiteSpeed, the error log is under Metrics > Errors, and the server side is looked after for you; if you're stuck, send support the URL and the time it failed (WhatsApp any time, phone during IST hours).
Whatever you run on, the order is the same: find out who sent the error, read that layer's log, and only then change something.
Disclosure: written by the team at Bigdomainhost, an Indian hosting provider. We sell the hosting linked above.
Top comments (0)