As a website visitor or owner, seeing the â502 Bad Gatewayâ error can be confusing because the specific problem is unclear. This is a generic and popular HTTP status code. Assuming your internet connection is working properly, displaying this message most likely means there is a problem communicating with the server. And the result is that you don't see the webpage you requested.
Although the 502 error is usually a server-side issue, sometimes the cause can be on the client side. In this article, we'll help you unravel this mystery by trying some simple client-side tricks. You will eliminate the problem or get closer to the cause.
502 Bad Gateway Meaning
Whenever you try to access a web page, your browser basically asks the web server to access the page. The web server will then process your request and return the resource you requested with an HTTP header and an HTTP status code.
But if there is a server problem, you might receive one of several different 500 error codes. All indicate a problem with the server. Therefore, the "502 Bad Gateway" error - the request from the client (your browser) - was good but the server could not resend the requested resource.
502 Fix incorrect gateway
Here are some tips you can try from your web browser to hopefully clear the "502 Bad Gateway" error and display your web page.
1. Try reloading the page
The error can be caused by a problem with the server as opposed to a crash, and server connectivity issues can be resolved fairly quickly in most cases.
Start by reloading the page â click the circular arrow near the URL address bar. In Chrome and Safari it's on the left, in Firefox it's on the right. Wait about a minute before reloading the page. If the webpage appears, the problem is gone.
Another quick thing you can try is opening a new browser session. Close all open browser windows, then open a new window and try navigating to the webpage again.
2. Try clearing your browser cache
Your web browser saves information from every website you visit in the browser cache. Each time you visit a website again, the pages load faster because your browser uses what is stored in the cache.
However, this configuration is not always a good thing. If your browser's cache contains an outdated or corrupted version of the website, this can cause the 502 error. Consider clearing your browser's cache to get the most recent version of the web pages you are trying to reach. Here's how it's done:
Chrome
- In your Chrome browser window, click on the three-dot menu at the top right.
- Select the âMore toolsâ option.
- Select âClear browsing dataâŠâ
- In the "Clear browsing data" pop-up, make sure only "Cached images and files" is checked, decide the time range, then "Clear data".
Firefox
- In your Firefox browser window, click the hamburger icon in the top right.
- Click on âHistoryâ.
- Select âClear recent historyâŠâ
- In the pop-up window, in the "Time range to erase" drop-down menu, choose "All".
- Make sure only 'Cache' is checked, then 'Clear now'.
Safari
To empty the cache of your Safari browser, the "Develop" menu must be activated. To do this:
- Go to âPreferencesâ then âAdvancedâ.
- Check the box "Show development menu in menu bar".
- Go to âDevelopâ then âEmpty cachesâ.
502 Bad gateway in NGINX
PHP-FastCGI Process Manager (PHP-FPM) is a background process for handling web server requests for PHP applications. NGINX forwards web requests to PHP-FPM worker processes that run the PHP application. NGINX returns a "502 Bad Gateway" error when it cannot successfully delegate a request to PHP-FPM or if PHP-FPM is not responding.
Here are the most common causes for NGINX to return a 502 error:
- PHP-FPM is not running.
- NGINX is unable to communicate with PHP-FPM.
- PHP-FPM expire.
Now let's see how to confirm if any of the above is the problem and how to fix it.
1. Check if PHP-FPM is running
When PHP-FPM is not running, NGINX returns a 502 error to all requests intended to reach the PHP application. You can use a "ps" command through a Linux host to verify that PHP-FPM processes are running. The command is:
« sudo ps aux | grep 'php'â.
If the results of this command show no PHP-FPM pools or core processes, PHP-FPM should be running to fix 502 errors. To make your PHP application more reliable, consider using "systemd" to manage PHP-FPM as a service in your production environment. PHP-FPM's background process will automatically start serving your PHP application whenever a new instance opens or your server starts.
Since PHP-FPM is built into PHP code, you can add it as a "systemd" service when you configure PHP. Once the project is configured as a service, use this command for an automatic start:
« sudo systemctl enable php7.2âfpm.serviceâ.
2. Check if NGINX has access to the socket
Once PHP-FPM starts, it generates Unix or TCP sockets to connect to the NGINX web server. PHP-FPM worker processes need sockets to listen to NGINX requests. Make sure PHP-FPM and NGINX are configured to use the same socket
PHP-FPM uses a different configuration file per process pool. To find these files, go to:
"/etc/php/7.2/fpm/pool.d/".
Pool sockets are described in a "listen" statement in its configuration file, for example the following "listen" statement configures a pool called "mypool" to use a Unix socket found at: « /run/php/mypool.sock ":
« listen = /run/php/mypool.sockâ.
If NGINX cannot access a pool's socket, you can determine which worker pool it is by looking at which socket is specified in the NGINX error log. If PHP-FPM did not start the compute node pool "mypool", for example, NGINX would throw a 502 error and the log entry would look like this:
"Connect() to unix::/run/php/mypool.sock failed (2: no such file or directory)".
3. Check if PHP-FPM expires
When your application takes too long to respond, users receive a timeout error. If the PHP-FPM timeout â set in the pool configuration statement (ârequest_terminate_timeoutâ) â is less than the NGINX timeout, NGINX will return a 502 error.
You can increase the PHP-FPM timeout setting in the pool configuration file. However, this could cause another problem: NGINX could time out before getting a response from PHP-FPM.
The NGINX default timeout is 60 seconds. If you increased the PHP-FPM timeout by more than 60 seconds and your PHP application did not have time to respond, NGINX will issue a "504 Gateway Timeout" error. Avoid this by increasing your NGINX timeout setting.
502 Bad Gateway in Cloudflare
Cloudflare returns a 502 error when it cannot establish a valid connection to your website's source web server or if the Cloudflare service is unavailable or configured incorrectly. The most common causes include:
- The source server is overloaded with too many server loads triggering server downtime
- WordPress websites with low PHP memory limits or too many MYSQL connections can slow server loading
- Incorrect DNS records, plugin or theme conflicts in WordPress, and service failures, e.g. PHP-FPM and caching services
Try the following tips to resolve Cloudflare's "502 Bad Gateway" error:
1. Reload the page and clear your cache
Reloading the webpage should be the first thing you try for any error message. The 502 error, in particular, can be the result of server overload causing temporary connection issues. Wait about a minute before reloading the page; this usually solves it.
If the 502 error persists, clear your browser cache and try again. Long press Ctrl + F5 for Windows and Linux browsers. For Chrome and Safari on Mac, it's Cmd + Shift + R.
2. Check your plugin/theme for conflicts
If you are using WordPress, the 502 error may be due to a misconfigured plugin. To determine if this is the cause, deactivate your plugins â no data will be lost.
- Log into your WordPress account.
- Select "Plugins" from the sidebar.
- Apply a "Disable" to all your plugins.
If the 502 error is resolved, then you need to find out which plugin is causing the problem. To do this, re-enable the plugins individually. After re-enabling each of them, reload the web page. When the Cloudflare error recurs, you will know which plugin is the culprit. You can ask the plugin developer for help by posting a ticket in WordPress.
Make sure your themes and plugins are the latest versions and your PHP version is supported.
3. Temporarily Disable Your Firewalls and CDNs
The issue may be caused by the Content Delivery Network (CDN) or firewalls. This is a common issue with providers that have additional firewall layers like Cloudflare. You can either check Cloudflare's online status page for information on any issues with their service or contact them.
If it is confirmed that the problem is with your firewalls or CDN, try disabling them temporarily and then enabling them afterwards.
4. Check your DNS servers
Sometimes your Domain Name System (DNS) servers may return the 502 error because your website's domain may not be pointing to the correct IP address. If you recently moved your website to another hosting service, you will need to wait at least 24 hours for the internet propagation to complete.
If not, to clear IP addresses and other DNS records from your local DNS cache, you can try clearing it, here's how:
- Launch a command window.
- Enter âipconfig /flushdnsâ.
Or via a Mac:
- Open a terminal window.
- Enter âdscacheutil -flushcacheâ.
502 Bad gateway resolved!
The "502 Bad Gateway" error message is a standard response when there is a problem with the communications between the servers involved in resending your web request.
The 500 error message usually means that there is a problem with the server side of the exchange. However, sometimes the cause can be related to the user's local computer equipment and/or network configuration. Luckily, this is a very common error, and there are many things you can try yourself to view your web page, or at least get closer to the root cause.
What other error messages have you encountered while visiting web pages? Has the problem resolved? Let us know in the "Comments" section.