When website access fails, many people will see various 5xx Error Code, such as 521, 502, and 504, all look like the server is down, but the reason behind each status code is actually not the same.
If you have access to Cloudflare or use a reverse proxy, you are more likely to encounter these errors. This article will talk about, 521, 502 and 504 actually represent what, when the respective problems mean, and how to determine where the error is.
![Image [1] - What is the difference between a 521 error and a 502 or 504? Figure out the difference between the three](https://www.361sale.com/wp-content/uploads/2025/05/20250607150540804-ef1807b29eb64085d9da65db13220094.jpg)
What is a 521 error?
![Image [2] - What is the difference between a 521 error and a 502 or 504? Figure out the difference between the three](https://www.361sale.com/wp-content/uploads/2025/05/20250523181739459-image.png)
521 is a Cloudflare-specific error code, the full name of which is Web Server Is Down. Meaning:
Cloudflare normal operation
It's trying to connect to your server, but it's not responding.
Common causes include: server downtime, firewall blocking, timeout
This means that Cloudflare initiated the request, but the source site simply "disconnected" and Cloudflare was unable to establish a TCP connection, so the page naturally failed to load.
Scenario: Your site is Cloudflare enabled and the source site is unreachable.
Common Trigger Points:
- Server shutdown or restart in progress
- Firewall not whitelisted for Cloudflare IPs
- Exhaustion of server resources (e.g. PHP-FPM crashes)
What is a 502 error?
502 The official meaning of Bad GatewayThis is also known as a "gateway error".
![Image [3] - What is the difference between a 521 error and a 502 or 504? Figure out the difference between the three](https://www.361sale.com/wp-content/uploads/2025/05/20250523181537828-image.png)
This usually occurs:
- The upstream server returned an invalid response
- Gateway (e.g. Nginx) not connecting to backend services (e.g. PHP, FastCGI, backend API)
Common Trigger Points:
- PHP-FPM not started
- Nginx Can't connect to Apache or FastCGI
- The upstream API returned an error response
Scenario: Your server itself is running, but communication between different components fails.
For example, if PHP is stuck in a Nginx → PHP → MySQL link, then Nginx will give 502.
What is a 504 error?
![Image [4] - What is the difference between a 521 error and a 502 or 504? Figure out the difference between the three](https://www.361sale.com/wp-content/uploads/2025/05/20250523181602569-image.png)
504 The official name of the Gateway TimeoutThis means "gateway timeout".
This error is not a communication failure, but a response that is too slow resulting in a timeout. Example:
Nginx has successfully forwarded the request to PHP, but PHP is not able to do so because of a certaincomprehensive databaseThe query got stuck and delayed in responding, and eventually Nginx gave up waiting and returned 504.
Common Trigger Points:
- Slow Database Queries
- Excessive back-end program execution time
- Lack of caching leads to resource exhaustion under high concurrency
Scenario: The link was established successfully, but the gateway waited too long and "timed out".
How can I tell which error it is?
Here's a quick comparison table:
status code | hidden meaning | error location | Common environments |
---|---|---|---|
521 | The source station cannot be connected | Cloudflare ↔ The Source. | Opened Cloudflare |
502 | Upstream response exception | Nginx ↔ PHP or API | Backend component communication error |
504 | Upstream response timeout | Nginx ↔ Backend programs | Program execution is too slow |
It can be judged simply like this:
- Is it Cloudflare? See if it's 521.
- beserver (computer)What's wrong? See if 502 is a component hang, or if 504 is too slow.
How do I troubleshoot these errors?
521: Check that the server is running and that Cloudflare is not blocked by a firewall
502: Verify that PHP is started and Nginx is configured correctly.
504: See if there are long database queries, PHP scripts executed for too long
521 Usually occurs when the server is overloaded or Cloudflare is not configured properly
502 Mostly occurs in plugin conflicts, PHP service not started
504 Commonly seen in WooCommerce sites where order processing is slow or there are too many inquiries.
summarize
While 521, 502 and 504 look similar, the issues behind them are very different. One is not connecting to the source site, one is a component response error, and one is a response timeout. Troubleshooting should be based on the site architecture and the services used to locate one by one. As long as you understand the meaning of these status codes, you will be able to find the problem faster and restore the site to normal operation.
Link to this article:https://www.361sale.com/en/55944
The article is copyrighted and must be reproduced with attribution.
No comments