The solution summary of Nginx 502 Bad Gateway

Source: Internet
Author: User
Tags nginx reverse proxy

Yesterday's own machine old tips 502 Bad Gateway error, the following small series to give you a summary of the Nginx appeared 502 the solution, the wrong gateway, have encountered such problems of friends can refer.

Cause of Occurrence

1, PHP fastcgi process number is not enough
The number of PHP fastcgi processes is not guaranteed when there is a huge amount of concurrent access to the site, because CGI is a single-threaded, multi-process operation, meaning that CGI needs to process a page before continuing on to the next page. If the number of processes is not enough, when the access is huge, the CGI is queued to process the previous request, and the subsequent request is only discarded. This time the Nginx will occasionally appear 502 errors.

2, PHP fastcgi memory is not enough

This problem generally does not occur when Nginx returns to a static page because Nginx does not require the processing of the PHP CGI to return to the static page directly. But when a Web page needs to handle a lot of complicated PHP operations, such as executing API capture, or collecting pages, the PHP requirement is quite high, and if you configure him with too little memory, it can easily cause PHP to crash.


1, first determine whether the PHP fastcgi process is sufficient.
Netstat-anpo | grep "php-cgi" | Wc-l
If the number of "fastcgi processes" actually used is close to the preset number of fastcgi processes, then the number of "fastcgi processes" is not sufficient and needs to be increased. However, it is not useful to increase the number of processes if the physical machine memory is not large enough to calculate whether your memory is sufficient to support more processes.
2, some PHP program execution time exceeds the Nginx wait time, can appropriately increase nginx.conf configuration file fastcgi timeout time, as follows:
http
{
......
Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
......
}
......
PHP.ini in Memory_limit set low will error, modified php.ini memory_limit for 64M, restart Nginx, found good, the original is the memory of PHP is insufficient.
If the above method still does not solve the problem, please try to optimize your PHP program, as far as possible to reduce the collection and database operations, speed up its response, sometimes because their own PHP program response too slow caused.


3: The current LNMP one-click installation package more problem is 502 bad Gateway, most of the time because the script before the installation of some LIB package may not be installed, resulting in PHP did not compile and install successfully.
Workaround:

You can try to install it manually based on the script in the LNMP one-click installation package to see what errors are causing it, search the Web, or send the error message. We'll give you an analysis of the cause of the error.

4:

In PHP.ini, the Eaccelerator configuration item must be placed before the Zend optimizer configuration, otherwise it may also cause 502 bad Gateway

The third reason:
502 problems occur during the installation, generally because the default php-cgi process is 5, possibly because the phpcgi process is not enough to cause 502, need to modify/usr/local/php/etc/php-fpm.conf Max_ The children value is increased appropriately. (generally 1 php-cgi occupy 20M of memory, please set this value according to memory)
It is also possible that the max_requests value is not sufficient.

5:

PHP execution timed out, modify/usr/local/php/etc/php.ini to change Max_execution_time to 300

Fifth reason:
Insufficient disk space, such as MySQL log takes up a lot of space

6:

To see if the php-cgi process is running

Can be viewed through the # Top command.


Nginx Reverse proxy solution a little different

The Nginx error log open, found "Pstream sent too big header while reading response headers from upstream" error, look at the information, the effect is the Nginx buffer There is a bug caused by the page consumption of our site may be too large buffer

Apache returned to the header too large nginx processing but it is caused by.

The code is as follows Copy Code


server {
Listen 80;
server_name *.xywy.com;

Large_client_header_buffers 4 16k;

#charset Koi8-r;

# Access_log off;

Location/{

#添加这3行,
Proxy_buffer_size 64k;
Proxy_buffers 32k;
Proxy_busy_buffers_size 128k;

Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;

Set $baiduspider ';

if ($http _user_agent ~ baiduspider) {
Set $baiduspider Baidu;
}

............


If it's nginx+phpcgi, that's it.
Fastcgi_connect_timeout 60;
Fastcgi_send_timeout 180;
Fastcgi_read_timeout 180;
Fastcgi_buffer_size 128k;
Fastcgi_buffers 4 256k;
Fastcgi_busy_buffers_size 256k;
Fastcgi_temp_file_write_size 256k;
Fastcgi_intercept_errors on
011/01/07 11:12:57 [ERROR] 10770#0: *38585340 upstream timed out (110:connection timed off) while reading response header From upstream, client:116.22.131.154, server: *.xywy.com, Request: "get/ysmp/index.php?did=124994 http/1.0", upstream: "http://127.0.0.1:8080/ysmp/index.php?did=124994", Host: "Xywy.yn16.com"

Then it turned out that the mistake had gone wrong.


Upstream timed out?

The code is as follows Copy Code

server {
Listen 80;
server_name *.xywy.com;
Large_client_header_buffers 4 16k;
Client_max_body_size 300m;
Client_body_buffer_size 128k;
Proxy_connect_timeout 600;
Proxy_read_timeout 600;
Proxy_send_timeout 600;
Proxy_buffer_size 64k;
Proxy_buffers 4 32k;
Proxy_busy_buffers_size 64k;
Proxy_temp_file_write_size 64k;
#charset Koi8-r;

# Access_log off;

Request_terminate_timeout

If this is the case when there are several post or database operations, but not common in static page operations, you can look at one of the php-fpm.conf settings:

Request_terminate_timeout

This value is Max_execution_time, which is the fast-cgi execution script time.

0s

0s is closed, is infinite execution goes on. (When the outfit was not carefully looked at the change of a number) problem solved, the implementation of a long time will not be wrong. Optimization fastcgi, you can also change the value of 5s to see the effect.

The solution summary of Nginx 502 Bad Gateway

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.