502 Bad Gateway error resolution for NGINX+PHP-FPM appearance

Source: Internet
Author: User
Tags fpm connection reset server memory

The 502 error is the one that all nginx running PHP don't want to see.

Nginx 502 has many reasons, but most of the reasons can be attributed to the lack of resources, that is, the back-end php-fpm processing problems, nginx the correct client request to the backend PHP-FPM process, However, because of the PHP-FPM process problems caused by the failure to parse the PHP code correctly, finally returned to the client 502 error.

The server appears 502 because of the connection timeout we send requests to the server because the server is currently linked too much, causing the server to not give the normal response, resulting in such an error

So if your server concurrency is very large, then you can only increase the machine first, and then optimize the following way to achieve better results, but if you do not have a small number of 502, the general can be summed up as a configuration problem, the script timeout problem.

insufficient number of 1.PHP-FPM processes

Use Netstat-napo |grep "PHP-FPM" | Wc-l look at the current number of fastcgi processes, if the number is close to the upper limit of the configuration in conf, you need to increase the number of processes.

But also can not endlessly increase, depending on the server memory, you can php-fpm the number of child processes to 100 or more, in 4G memory server 200 on it.

2. High-profile Linux kernel open file number

You can use these commands (must be the root account)

Echo ' ULIMIT-HSN 65536 ' >>/etc/profile

Echo ' ULIMIT-HSN 65536 ' >>/etc/rc.local

Source/etc/profile

3. Script Execution time timed out

If the script does not return for a long time because of some reason, the new request cannot be processed, and the following configuration can be adjusted appropriately.

Nginx.conf inside is mainly as follows

Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;

php-fpm.conf if the following

Request_terminate_timeout = 10s

4. Cache settings are relatively small

Modify or add configuration to nginx.conf

Proxy_buffer_size 64k;
Proxy_buffers 512k;
Proxy_busy_buffers_size 128k;

5. Recv () failed (104:connection reset by peer) while reading response header from upstream

Possible causes the computer room network loses the bag or the computer room has the hardware firewall to prohibit accesses this domain name

But the most important thing is to set a good timeout in the program, do not use PHP-FPM request_terminate_timeout,

preferably set into request_terminate_timeout=0;

Because this parameter will kill the PHP process directly, and then restart the PHP process, so that the front-end nginx will return 104:connection reset by peer. This process is very slow, the overall feeling is that the site is very card.

May 10:50:58.044162 [WARNING] [pool www] child 4074, script '/usr/local/nginx/html/quancha/sameip/detail.php ' Execution timed out (15.129933 sec), terminating
May 10:50:58.045725 [WARNING] [pool www] child 4074 exited on signal sigterm after 90.227060 seconds from start
May 10:50:58.046818 [NOTICE] [pool www] child 4082 started

Say 1000 10,000 The most important thing is to control the timeout in the program, gethostbyname, Curl, file_get_contents and other functions to set the timeout time.

The other is to say more, this thing is to increase the interactivity of the site, but the use of more than a slow response, if your site timed out and used more say yes, you can close it.

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.