- 502 Bad Gateway Meaning
- 502 Bad Gateway Solution
- 502 Bad gateway in NGINX
- 1. Check if PHP-FPM is running
- 2.Check if NGINX has access to the socket
- 3. Check if PHP-FPM is timing out
- 502 Bad Gateway in Cloudflare
- 1. Reload the page and clear your cache
- 2. Check your plugin/theme for conflicts
- 3. Temporarily disable your firewalls and CDN
- 4. Check your DNS servers
- 502 Bad Gateway Resolved!
As a visitor or website owner, seeing the “502 Bad Gateway” error can be disconcerting since the specific problem is unclear. This is a popular and generic HTTP status code. Assuming your Internet connection is fine, seeing this message means there is most likely a communication problem with the server. And the result is that you do not see the web page you requested.
Although the 502 error is usually a server-side problem, sometimes the cause can be on the client side. In this article, we will help you get to the bottom of this mystery by trying simple client-side tips. You will either fix the problem or get closer to understanding the cause.
502 Bad Gateway Meaning
Every time you try to access a web page, your browser is basically asking the web server to access the page. The web server will then process your request and return the resource you requested along with an HTTP header and an HTTP status code.
But if there is a problem with the server, you may receive one of several different 500 error codes. All of them indicate a problem with the server. Therefore, the “502 Bad Gateway” error – the request from the client (your browser) – was good but the server was unable to return the requested resource.
502 Bad Gateway Solution
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 could be due to a server failure rather than an outage, and server connectivity issues can be fixed reasonably quickly in most cases.
Start by reloading the page: Click the circular arrow near the URL bar. In Chrome and Safari, it is on the left, in Firefox it is on the right. Wait about a minute before reloading the page. If the web page appears, then the problem is gone.
Another quick thing you can try is to open a new browser session. Close all open browser windows, then open a new window and try to navigate to the web page again.
2. Try clearing your browser cache
Your web browser saves information about each website you visit in the browser cache. Every time you revisit a website, the pages load faster because your browser uses what is stored in the cache.
However, this configuration is not always good. If your browser’s cache has an outdated or corrupted version of the website, this can cause the 502 error. Consider clearing your browser’s cache to get the most up-to-date version of the web pages you are trying to access. This is how you do it:
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 window, make sure only “Cached images and files” option is checked, decide the time range, and then “Clear data”.

Firefox
- In your Firefox browser window, click the hamburger icon at the top right.

- Click “History.”

- Select “Clear recent history…”

- In the pop-up window, from the “Time range to delete” drop-down menu, choose “All”.

- Make sure only the “Cache” box is checked and then “Clear now.”
Safari
To clear the cache in your Safari browser, you need to enable the “Develop” menu. To do this:
- Go to “Preferences” and then “Advanced.”

- Check the “Show development menu in menu bar” box.

- Go to “Develop” and 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 passes 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 unresponsive.
These are the most common causes why NGINX returns a 502 error:
- PHP-FPM is not running.
- NGINX cannot communicate with PHP-FPM.
- PHP-FPM is running out of time.
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 will return a 502 error to all requests destined to reach the PHP application. You can use a “ps” command through a Linux host to verify the execution of PHP-FPM processes. The command is:
“sudo ps aux | grep ‘php’”.
If the results of this command do not show any PHP-FPM groups or parent processes, it is necessary to run PHP-FPM 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. The PHP-FPM background process will automatically start serving your PHP application whenever a new instance opens or your server starts.
Since PHP-FPM is built into the 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 is started, 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 group. To find these files, go to:
“/etc/php/7.2/fpm/pool.d/”.
Pool sockets are described in a “listen” statement in their configuration file; For example, the following “listen” statement configures a pool called “mypool” to use a Unix socket located 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 examining which socket is specified in the NGINX error log. If PHP-FPM did not start the “mypool” worker pool, for example, NGINX would send 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 is timing out
When your app takes too long to respond, users will receive a timeout error. If the PHP-FPM timeout, set in the group 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 receiving a response from PHP-FPM.
The default NGINX timeout is 60 seconds. If you have increased the PHP-FPM timeout by more than 60 seconds and your PHP application has not had time to respond, NGINX will issue a “504 Gateway Timeout” error. Avoid this by increasing the NGINX timeout setting.
502 Bad Gateway in Cloudflare
Cloudflare will return a 502 error when it cannot initiate a valid connection to your website’s origin web server or if the Cloudflare service is unavailable or configured incorrectly. The most common causes include:
- The origin server is overloaded with too many server loads causing server downtime.
- WordPress websites with low PHP memory limits or too many MYSQL connections can cause the server to load slowly
- Incorrect DNS records, plugin or theme conflicts in WordPress and service failures, for example PHP-FPM and caching services
Try the following tips to resolve the Cloudflare “502 Bad Gateway” error:
1. Reload the page and clear your cache
Reloading the website should be the first thing you try when faced with any error messages. The 502 error, specifically, can be the result of a server overload causing temporary connection issues. Wait about a minute before reloading the page; this usually resolves it.
If the 502 error still appears, clear your browser cache and try again. Hold down 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 use WordPress, the 502 error may be caused by a misconfigured plugin. To fix if this is the cause, disable your plugins; no data will be lost.
- Sign in to your WordPress account.
- Select “Plugins” in the sidebar.
- Apply a “Deactivation” to all your plugins.
If the 502 error is resolved, then you will need to find which plugin is the problem. To do this, reactivate the plugins inpidually. After reactivating each one, reload the web page. When the Cloudflare error occurs again, you will know which plugin is the culprit. You can ask the plugin developer for help by posting a ticket on WordPress.
Make sure your themes and plugins are the latest versions and your PHP version is supported.
3. Temporarily disable your firewalls and CDN
The issue could be caused by the content delivery network (CDN) or firewalls. This is a common problem with providers with additional firewall layers like Cloudflare. You can check Cloudflare’s online status page for information about any issues with their service, or you can contact them.
If the problem is confirmed to be with your firewalls or CDN, try disabling them temporarily and then enabling them.
4. Check your DNS servers
Sometimes your domain name system (DNS) servers may return a 502 error because your website’s domain may not point to the correct IP address. If you have recently moved your website to another hosting service, you will need to wait at least 24 hours for the propagation over the Internet to complete.
If this is not the case, to clear IP addresses and other DNS records from your local DNS cache, you can try flushing it. Here’s how to do it:
- Start a command window.
- Get into “
ipconfig /flushdns”.
Or via a Mac:
- Open a window at the terminal.
- Get into “
dscacheutil -flushcache”.
502 Bad Gateway Resolved!
The “502 Bad Gateway” error message is a standard response when there is a problem with communications between the servers involved in returning 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 may be in the user’s local computer equipment and/or network configuration. Fortunately, this is a very common error and there are many things you can try yourself to get your web page to display, or at least so you can understand the root cause.