PHP 502 Bad Gateway Solutions

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

Today, after the upgrade of PHP 502 Bad Gateway error, according to experience is PHP-FPM problem, but see what traffic on the Internet, child process settings, but I do not have any traffic on the server. So you can only look at the configuration file.

[Global]
PID =/usr/local/php/var/run/php-fpm.pid
error_log =/usr/local/php/var/log/php-fpm.log
log_level = Notice
[www]
listen =/tmp/php-cgi.sock
user = www
group = www
pm = dynamic
Pm.max_children =
Pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
request_terminate_timeout = 100

See/tmp/php-cgi.sock Intuitive discovery should be a file, so go to the directory to view permissions, found that the file owner is root:root, and my nginx and PHP-FPM process is run on the WWW user. So it should be a permission issue.

The user = Www,group = www also has a permission set in the configuration file, which is found by Ps-aux|grep PHP, which is the owner of the PHP-FPM process.

Know the source of the problem began to check PHP-FPM configuration, found the Listen.ower listen.group settings.

This is the file after the improvement:

[Global]
PID =/usr/local/php/var/run/php-fpm.pid
error_log =/usr/local/php/var/log/php-fpm.log
log_level = Notice
[www]
listen =/tmp/php-cgi.sock
Listen.owner = www
listen.group = www
user = www
group = www< C9/>PM = dynamic
Pm.max_children =
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_ Servers = 6
request_terminate_timeout = 100

If you do not know to view the configuration you can try the following methods

1, see the current number of PHP fastcgi process is sufficient:

Netstat-anpo | grep "php-cgi" | Wc-l

If the actual number of "fastcgi processes" is close to the preset "fastcgi process", then the "fastcgi process count" is not sufficient and needs to be increased.

2, some of the PHP program execution time than the Nginx wait time, you can appropriately increase the nginx.conf configuration file fastcgi timeout time, such as:

......

HTTP

{

...

.. Fastcgi_connect_timeout;

Fastcgi_send_timeout;

Fastcgi_read_timeout;

......

}

......


Add another article


1.PHP-FPM process count is not enough

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/profi Le

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;

Fastcgi_send_timeout;

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.

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.