- Fix 502 Bad Gateway Nginx Error in Ubuntu – 4 Simple Steps
- 502 Bad Gateway NGINX Error
- 1. NGINX as a proxy for Apache
- 2. NGINX with PHP5-FPM
- 3. NGINX with Services/Apps
- Solutions For 502 Bad Gateway Nginx Error
- Solution 1: Increase Buffer & Timeout Inside http Block
- Solution 2: Ensuring PHP-FPM5 is Listening Properly
- In Ubuntu
- In CentOS
- Using SysV Init scripts directly:
- Using a service wrapper script
- Using Upstart (e.g. Ubuntu)
- Using systemd (newer servers)
- Solution 3: Disable APC Cache
- Solution 4
- Other Error Fixes
- Related
- Share this post:
- About Rajesh Rai
- How to Fix Nginx 502 Bad Gateway Error on PHP-FPM
- How Nginx 502 Bad Gateway Error happens and what is its connection with php-fpm
- Nginx 502 Bad Gateway Error with PHP-FPM
- Php-fpm is not communicating with Nginx and causing a 502 Bad Gateway Error
- 502 Bad Gateway Error due to Nginx is timing out
- 502 Bad Gateway Error due to PHP-FPM is timing out
- Nginx 502 Bad Gateway Error when Nginx as Proxy for Apache
- Nginx with other services/apps and 502 Bad Gateway Error
Fix 502 Bad Gateway Nginx Error in Ubuntu – 4 Simple Steps
While web browsing, you must have noticed websites returning 502 Bad gateway NGINX error. It is really frustrating to keep seeing this error again and again. While sometimes, your website runs well and returns the same error after a couple of hours.
There are a few reasons why you will find this error message. You might see the following error messages due to the customised 502 Bad Gateway error message.
Here is the list of most common 502 error messages:
- “502 Bad Gateway”
- “502 Bad Gateway NGINX”
- 502 bad gateway nginx/1.14.0 (ubuntu
- “502 Service Temporarily Overloaded”
- “502 Proxy Error”
- “Error 502”
- “HTTP Error 502 – Bad Gateway”
- “HTTP 502”
You will find 502 Bad Gateway Error while-
- Nginx running as a proxy for Apache web server.
- Nginx running with PHP5-FPM.
- Nginx running with PHP7-FPM.
- Nginx running with other services as a gateway.
- Bad buffering / timeout configuration.
Such type of error is referred to as an HTTP Status Code Error. It generally occurs at the gateway. Where gateway is an access point, a bridge communicates between the two servers. In this case, the gateway can be a service or application that is working and receiving requests from Nginx web server.
Let’s dive deep into the reasons to understand the 502 Bad Gateway Nginx Error.
502 Bad Gateway NGINX Error
1. NGINX as a proxy for Apache
While using NGINX as a proxy for Apache, there might be a possibility that Apache is not well configured and this might result in 502 Bad Gateway Error. In most of the cases, it can be resolved by just restarting the Apache web server.
2. NGINX with PHP5-FPM
While using NGINX with PHP5-FPM, if it is not well configured, the website will throw 502 Bad Gateway Error. There might be a possibility that PHP5-FPM will get overloaded with too many requests, maybe because of PPC running on that website. In this case, restart PHP5-FPM and check the error logs.
3. NGINX with Services/Apps
You should restart the other services that you are using along with the NGINX with some service or Apps and check the error logs.
These 3 reasons will help you understand the reason for the 502 Bad Gateway Error. It is very important that you know what services you are using and whether it is configured correctly or not. To know the issue, you need to check the log files and then you will get some idea and find the right solution.
Here is the simple command for NGINX that you can use to check your logs and identify the issue:
tail -f /var/log/nginx/error.log
Note: Setting up error logs plays a significant role in identifying server errors. You can check the official documentation of NGINX to configure error logging.
Now, Let’s have a look at the other quick solutions for 502 Bad Gateway Nginx Error:
Solutions For 502 Bad Gateway Nginx Error
Solution 1: Increase Buffer & Timeout Inside http Block
You need to increase buffer and timeout inside nginx.conf file under http block section. Run the following command and edit the nginx.conf .
sudo vim /etc/nginx/nginx.conf
Add the following under http block:
Solution 2: Ensuring PHP-FPM5 is Listening Properly
Ensure that your PHP5-FPM is listening according to what you have configured in NGINX. Open the www.conf file and search for one of these two options:
listen = /var/run/php5-fpm.sock
In Ubuntu
sudo vim /etc/php5/fpm/pool.d/www.conf
In CentOS
sudo vim /etc/php-fpm.d/www.conf
After ensuring what PHP-FPM listening according to NGINX, restart the PHP-FPM service.
You can use one of the following commands to restart PHP-FPM:
Using SysV Init scripts directly:
/etc/init.d/php-fpm restart # typical /etc/init.d/php5-fpm restart # debian-style
Using a service wrapper script
service php-fpm restart # typical service php5-fpm restart # debian-style
Using Upstart (e.g. Ubuntu)
restart php5-fpm # typical (ubuntu is debian-based) restart php-fpm # uncommon
Using systemd (newer servers)
systemctl restart php-fpm.service # typical systemctl restart php5-fpm.service # uncommon
If you are getting confused by seeing this list of commands. I personally advise you to use to get your work done:
sudo service php5-fpm restart
Solution 3: Disable APC Cache
Disable APC Cache if used, instead, try Xcache, APC can cause this kind of errors under particular environments causing segmentation faults.
Solution 4
Once you check the log file, you will find error stating some like this:
php5-fpm.sock failed (13: Permission denied) error.
Most of the people get this error because of the PHP update. PHP fixed a security bug where o had rw permission to the socket file.
Uncomment all permission lines, like:
listen.owner = www-data listen.group = www-datalisten.mode = 0660
And finally, restart the PHP-FPM using the command:
sudo service php5-fpm restart
Note: If your web server is running as a user other than www-data, you will require updating the www.conf file accordingly.
Other Error Fixes
- Error Establishing a Database Connection in WordPress Fix
- PHP5-FPM 502 Bad Gateway Error (connect() to unix:/var/run/php5-fpm.sock failed (2: No such file or directory)
- Your PHP installation appears to be missing the MySQL extension which is required by WordPress
- 5 Simple Steps to Fix 504 Gateway Timeout Nginx Error
If you have any other way to fix this issue, please share them with us in the comment section below or contact me on my email [email protected] .
Related
Share this post:
About Rajesh Rai
Rajesh Rai is an Entrepreneur, Web Developer and a Digital Marketer. He is a founder of SAATATYA, Search Engine Bay, Sharestrap and Website Vidya. He has helped 100+ businesses to grow their presence online.
Connect: Twitter, Instagram, Facebook
Sign up for a WP Engine plan and receive 4 Month Free Hosting
+ Genesis Framework + 35+ StudioPress Themes free!
GO TO WP ENGINE
How to Fix Nginx 502 Bad Gateway Error on PHP-FPM
Nginx is one of the fast performing web servers on the internet. Nginx and php-fpm are the most used combinations on the internet to server PHP websites. Sometimes we came across a very bad Nginx 502 Bad Gateway error generated by Nginx server and it is not very easy to identify the problem and fix. Here are some quick solutions to fix Nginx 502 Bad Gateway error on a web-server.
How Nginx 502 Bad Gateway Error happens and what is its connection with php-fpm
Common 502 errors include the following messages from Nginx server
- “502 Bad Gateway”
- “502 Bad Gateway NGINX”
- “502 Proxy Error”
- “502 Service Temporarily Overloaded”
- “Error 502”
- “HTTP Error 502 – Bad Gateway”
- “HTTP 502 Bad Gateway”
These errors point to the same situation of Nginx server and the error came across below situations
- Nginx server running with php-fpm service
- Nginx is running as a reverse proxy to Apache server
- Nginx running as a gateway to other services like Python, Ruby, etc.
- Bad configuration of cache or timeout.
Now the error says there is a “BAD GATEWAY”. That means Nginx server is trying to communicate to another service through a gateway and it is not getting any answer from that gateway. This is Nginx 502 Bad Gateway error situation.
So Nginx and another service (Apache, php-fpm, other services) running. Both are communicating through a gateway and Nginx is not getting any answer from the gateway. To solve 502 Bad Gateway Error the other service running with Nginx must answer through the gateway.
Usually, this happens due to a misconfiguration in the configuration files of the server. Or the other service is not running properly with Nginx. So to solve this problem a two-step check is required.
- Step 1: Check the other server status, whether it is running perfectly, is it giving the desired output?
- Step 2: Is the configuration correct? Is the second service configured to answer the gateway perfectly?
Nginx 502 Bad Gateway Error with PHP-FPM
The problem occurs due to the following conditions
- PHP-FPM won’t get started
- PHP-FPM not running
- NGINX can’t communicate with PHP-FPM server
- NGINX is timing out
- PHP-FPM is timing out with requests
Check whether php-fpm is running or not
systemctl status php-fpm.service
will tell you about the current situation of php-fpm server
Just restart the php-fpm server to solve this problem
systemctl restart php-fpm.service
for php5 the name of service may be php5-fpm, for PHP 7 it may be php7.0-fpm or php7.1-fpm or php7.2-fpm check accordingly.
Php-fpm is not communicating with Nginx and causing a 502 Bad Gateway Error
This is caused due to the misconfiguration of the php-fpm server or the misconfiguration of Nginx server.
Check the following configuration files in the worker pool configuration (the file is in /etc/php/fpm/pool.d/www.conf , this may vary with installation) and verify these lines ( www-data is the owner of the web server process, that is nginx)
user = www-data group = www-data listen = /var/run/php7-fpm.sock listen.owner = www-data listen.group = www-data
then restart the php-fpm service.
If the Nginx server and php-fpm are running and still getting a 502 Bad Gateway error then it is the problem with communicating the gateway. The problem is happening due to the misconfiguration in the file /etc/nginx/sites-enabled/default , by default or the file name in the name of the website called /etc/nginx/sites-enabled/mywebsite.com.conf
Look at the segment where the fastcgi_pass configuration. It must be the same as the listen in the above configuration
Then restart both Nginx and php-fpm servers. Now check the website whether it is serving properly.
502 Bad Gateway Error due to Nginx is timing out
if php-fpm is running and serving the requests properly then there will be a chance of Nginx is timing out. That is php-fpm do not respond to NGINX in a timely manner will cause a 502 because NGINX couldn’t wait any longer for a response from php-fpm.
In this case, increasing the maximum execution time of the application and NGINX’s timeout window would be the best solution.
To get this we need two changes:
First , increase the maximum execution time of the php-fpm server. To do this open PHP-FPM’s configuration file
sudo nano /etc/php7/fpm/php.ini
find the following line and increase the number to a higher value. Usually 60 or 120.
The value depends upon the expected execution time of the application running in the php-fpm server. Add some extra buffer time to get rid of the situation again.
Exceptionally large execution time will affect the server performance and it may lead to a dead server process in the memory.
Second , edit the NGINX config
sudo nano /etc/nginx/nginx.conf
add the following within the HTTP block to increase timeout windows, buffers, and buffer sizes:
Save the changes to the file and exit. Now restart Nginx and php-fpm
systemctl restart php-fpm.service and
systemctl restart nginx.service
502 Bad Gateway Error due to PHP-FPM is timing out
if the php-fpm server is timing out upon execution of an exceptionally long PHP script or the script is blocked by a large MySQL query process then these solutions may not work. In that situation, you could temporarily extend PHP’s execution time limit by invoking the set_time_limit() function from within the PHP application, but if you are continuously reaching into the execution timeout limit then as a long-term solution like profiling or increasing PHP-FPM’s timeout may be more appropriate to get rid of a 502 error more safely. These things must be done after an extensive research of the execution time of the corresponding PHP function or MySQL query. Take a look at the memory and system load during the execution is a must for a smooth operation of this server.
Nginx 502 Bad Gateway Error when Nginx as Proxy for Apache
In this situation, the gateway service is Apache and the Nginx is a proxy for Apache. If apache server dies or it’s not well configured, it can cause this 502 Bad Gateway error. How to fix the apache error? Most of the times, restarting apache web server will get rid of this, but you must check the log files to know why exactly this was caused.
Nginx with other services/apps and 502 Bad Gateway Error
In this situation, the app is gone not responding. Try restarting the other service behind Nginx server and check the logs created by the application to find the exact reason why a 502 Error happened.
Check the status codes HTTP Status Codes on W3C for more details.