Overcome 502 Bad Gateway on Nginx

End of the week instead of relaxing even busy server. Ha ha. 😀 This time the problem I am experiencing is on WordPress website that uses Nginx and in front there CloudFlare kok even appear HTTP 502 Bad Gateway error.

Though I can still access the site if by IP address or localhost with lynx via SSH. Something is wrong. 🙁 To be sure if you see the Nginx white page with the following error message means the same case:

502 Bad Gateway
502 Proxy Error
502 Bad Gateway NGINX
502 Service Temporarily Overloaded
Error 502
HTTP 502
HTTP Error 502 – Bad Gateway

Before we discuss the solution then we must understand what the purpose of this error message. So Bad Gateway if interpreted is the gate / entrance is problematic.

The point is that the service or application that receives and processes requests from Nginx can not handle it. This usually happens on PHP-FPM and becomes my basis in this case discussion.

Restart PHP-FPM

The easiest and fastest solution, you just type:

service php5-fpm restart

This can be said to be temporary because we are not trying to find out and fix the problem. After the service is active again you must check the contents of PHP-FPM error log to be diagnosed what the problem.

Make sure the PHP-FPM service is listening appropriately
There are two types of ways PHP-FPM services listen and serve requests, via sockets or IP addresses. Please check the configuration (named www.conf) and make sure it contains one of the following, or you can exchange it:

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

or

listen = 127.0.0.1: 9000

After that restart PHP-FPM. Check PHP-FPM permissions Sometimes we forget to set how the PHP-FPM service files will be used by Nginx. Just make sure you can communicate with each other:

  • View the contents of php.ini and www.conf in the user and group sections whether they match the Nginx configuration.
  • Is the /var/run/php5-fpm.sock file usable for Nginx?
  • Termasuk juga /var/lib/php harap diperiksa.

Turn off APC or try another cache system in PHP

APC (Alternative PHP Cache) function is to create cache of PHP code executed so that in the next process that requires the same function will be processed faster. But sometimes this feature resulted in error 502, I suggest to try first disable.

I recommend replacing it to the alternatives only. This consideration is an old system and has been replaced by PHP OPcache, try to use the new and officially included in PHP version 5.5 and above.

Increase buffer size and timeout on Nginx configuration So please edit nginx.conf and place the following code in the http block {}:

http {

    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;

}

Its function is that Nginx process data with a larger container in memory so it does not need to be stored in harddisk for a while. Also the processing time is given a longer time limit so as not directly considered failed. So long until here the discussion, I just know some of the above solutions. Hehe. 😀 And I just realized even though the title is Nginx but the solution there are 4 that utak especial PHP-FPM and only 1 whose business with Nginx. Ha ha. 😀

Pos terkait

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

Situs ini menggunakan Akismet untuk mengurangi spam. Pelajari bagaimana data komentar Anda diproses.