How To Fix 502 Bad Gateway Error on Nginx

Fix 502 Bad Gateway Error on Nginx

If you run a Nginx web server you may have already encountered the annoying 502 bad gateway errors. This is pretty common error, are generated most probably by the PHP or  FastCGI buffer and timeouts settings. This tutorial shows you how to fix nginx 502 bad gateway on the nginx webserver. This post shows how to fix this problem, and the configuration option to prevent it occurring again on reboot.

This article assumes you have at least basic knowledge of linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo’ to the commands to get root privileges. I will show you through the step by step solve 502 bad gateway error on Nginx web server.

In this tutorial we will show you how to fix 502 bad gateway error on Nginx web server.

Fix 502 Bad Gateway Error on Nginx

Method 1. Changes in Nginx Config

#nano /etc/nginx/nginx.conf

http {
    ...
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    ...
}

Method 2. Change PHP-FPM to listen on a unix socket or TCP socket.

#nano /etc/php-fpm.d/www.conf

listen = /var/run/php5-fpm.sock

To:

listen = 127.0.0.1:9000

If you are configuring php-fpm to listen on a Unix socket, you should also check that the socket file has the correct owner and permissions.

chmod 0660 /var/run/php5-fpm.sock
chown www-data:www-data /var/run/php5-fpm.sock

Method 3. Disable APC.

APC caching can cause 502 Bad Gateway issues under particular environments causing segmentation faults. I highly suggest using Memcache(d), but XCache is also a good alternative.

Congratulation’s! You have successfully solved 502 bad gateway issues. Thanks for using this tutorial to fix 502 bad gateway issues on Linux system. For additional help or useful information, we recommend you to check the official Nginx web site.

You Might Also Like: How To Hide Nginx Server Default Header

How To Install Lighttpd on CentOS

install lighttpd on centos

In this tutorial we will install and configuration of Lighttpd on CentOS Linux server.

Lighttpd is a fast and secure web-server which has been optimized for high-performance environments. With a small memory footprint compared to other web-servers, effective management of the cpu-load, and advanced feature set (FastCGI, SCGI, Auth, Output-Compression, URL-Rewriting and many more) lighttpd is the perfect solution for every server that is suffering load problems. Here’s a brief tutorial that shows you how to install lighttpd web server on centOS.

Install Lighttpd on CentOS

Step 1. First add EPEL yum repository your system.

 rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Step 2. Install Lighttpd using yum package manager

 yum install lighttpd lighttpd-fastcgi

Step 3. Start Lighttpd Server

 service lighttpd start

Step 4. Testing Lighttpd

To make sure everything installed correctly we will now test Lighttpd to ensure it is working properly. Open up any web browser and then enter the following into the web address:

 http://localhost/  or  http://your.ip.addr.ess

Note: Lighttpd’s default document root is /srv/www/lighttpd, you can use this or change it by editing the configuration file /etc/lighttpd/lighttpd.conf.

Congratulation’s! You have successfully installed lighttpd. Thanks for using this tutorial for installing lighttpd web server on CentOS system. For additional help or useful information, we recommend you to check the official lighttpd web site.

You Might Also Like:

How To Install Lighttpd With PHP And MariaDB on Ubuntu 16.04 LTS