PHP 502 Solution appears

Source: Internet
Author: User
Tags connection reset server memory

Nginx+php appear 502 Bad gateway, generally this is not nginx problem, but because of fastcgi or PHP problems caused, common have the following several.

1. PHP.ini Memory_limit is too small (this must be noted if there are individual PHP program processes that require significant memory usage)

2. php-fpm.conf in the Max_children or max_requests set unreasonable (set too small will because there is not enough CGI process requests, set up the General Assembly in a moment to respond normally, wait a long time to respond to the situation, In general, children is calculated according to memory, such as 1G setting 64,2g128. This is self-adjusting according to the actual situation. Also see if the current PHP fastcgi process is sufficient for the command: Netstat-anpo |grep “php-cgi” | Wc-l if the actual use of “ FastCGI processes ” close to preset “ fastcgi process number ”, then, description “ FASTCGI process Count ” not enough, need to increase. )

3. Check the Nginx error log and discover Pstream sent too big header while reading response Headerfrom upstream, check client head buffer,fastcgi Buffer size is too small to be set to 32K.

4. PHP program execution time is too long and timeout, check nginx and fastcgi in the various timeout settings. (Nginx in the Fastcgi_connect_timeout 300;fastcgi_send_timeout 300:fastcgi_read_timeout300; Keepalive_timeout; Max_execution_time in the Request_terminate_timeout,php.ini in PHP-FPM)

5. PHP-FPM has a parameter max_requests that indicates how many requests each children will be shut down after processing. In a large number of processing requests, if the value is set too small can cause children frequent suicide and build and waste a lot of time, if all the children almost all at this time suicide, then the reconstruction will not children response request, then appear 502. You can set this value to a larger or 0[infinity].

The above is almost the more common 502 of the problem causes and solutions, in fact, the best way to solve the problem is to see the Nginx and fastcgi errorlog.

Finally borrow the online balm to make a summary: php-cgi process is not enough, PHP execution time is long, or the php-cgi process is dead, there will be 502 errors.

502 error is all operators who run PHP with Nginx do not want to see

Nginx 502 There are many reasons, but most of the reasons can be attributed to insufficient resources, that is, the backend php-fpm processing problems, Nginx sent the correct client request to the back-end of the PHP-FPM process, but because the PHP-FPM process problems caused by the incorrect parsing The PHP code eventually returned to the client 502 error.

Server 502 Because the connection time-out we send a request to the server because the server is currently too many links, causing the server can not give a normal response, resulting in such an error

Therefore, if your server concurrency is very large, it can only increase the machine first, and then the following way to optimize the results will be better; But if you do not have a small but 502, generally can be attributed to the configuration problem, the script timeout problem.

1.PHP-FPM process count is not enough

Using 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 adjust, according to the server memory situation, you can set the number of PHP-FPM sub-process to 100 or more, on the 4G memory of the server 200 can be.

2. Increase the number of open files in high-speed Linux kernel

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-out

If the script is not returned 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 the main is 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 Fromupstream

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

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

preferably set into request_terminate_timeout=0;

Because this parameter will directly kill the PHP process, 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 website 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 15SIGTERM after 90.227060 seconds from start
May 10:50:58.046818 [NOTICE] [pool www] child 4082 started

Say 1000 10,000 the most important is the program to control the timeout, gethostbyname, curl, file_get_contents and other functions to set the time-out period.

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

6, I encountered 502 of the solution:

Adjust the backlog of PHP and nginx to increase.

PHP-FPM High-load solutions

Postedon 2011/09/02

This is only the introduction of the PHP-FPM optimization method, but in general and nginx combination when used, individually optimize one of the words, the role is not particularly large, but also need to optimize nginx. Nginx method of Procedure reference: http://blog.haohtml.com/archives/6213. The above optimization and optimization of the diagram, see the gap between the front and back is particularly large.

PHP-CGI (FASTCGI) leading to Nginx 502 bad Gateway

NGINX frequency explosion 502 Bad GATEWAY error, read the online tutorial, still not completely solved.
At present, I summarize the way to solve 502 bad GATEWAY: 1. Depending on the performance of the server, increase the value of Max_children in the php-fmp.conf, I am currently using the

15.

2.

Load the PHP-FPM with the reload parameter at timed time. The main reason is that PHP script execution time is too long, overload PHP-FPM can eliminate this problem. How to completely solve the php-cgi script takes up a lot of memory and causes 502 errors to be further explored, the current practice is a good way.
Specifically, use crontab to let PHP-FPM smooth restart, which does not affect the operation of PHP script.

*/10* * * */USR/LOCAL/PHP/SBIN/PHP-FPM Reload

=================== Optimization Settings =========================


When you running a highload websitewith php-fpm via FastCGI, the following tips is useful to you:)

These tips may be useful if you have a high-load site that uses PHP-FPM to manage FastCGI:)

1.Compile PHP's modules as less as possible, and the simple and the best (fast);
1. Install PHP modules as little as possible, the simplest is the best (fast)

2. Increas PHP FastCGI Child number to + even more. Sometime, ok! (on 4GB memory server);

2. Set your PHP FastCGI sub-process number to 100 or above, on the 4G memory server 200 can
Note: My 1g test machine, open 64 is the best, recommended to use the pressure test to obtain the best value

3.Using SOCKET PHP FastCGI, and put Into/dev/shm on Linux;
3. Using the socket connection FastCGI, the Linux operating system can be placed in the/DEV/SHM
Note: setting <valuename= "Listen_address" in the php-fpm.cnf can be FastCGI through the socket connection,/dev/ SHM is a memory file system, which is sure to be fast in memory. Remember this time also in the Nginx configuration to make changes, to keep consistent.

location~. *\. (PHP|PHP5)? $

{

#

will be Nginx with the FastCGI mode of communication by TCP instead Unixsocket . TCP under high concurrent access Unixsocket stable, but Unix sockets faster than TCP come on.

Fastcgi_pass Unix:/tmp/php-cgi.sock;

#fastcgi_pass 127.0.0.1:9000;

Fastcgi_index index.php;

Include fcgi.conf;

}

4. Increase Linux "Max open files", using the following command (must be root):

# echo ' ULIMIT-HSN 65536′>>/etc/profile

# echo ' ULIMIT-HSN 65536 >>/etc/rc.local

# Source/etc/profile


4. Increase the number of open files in the Linux kernel, 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


Note: I am modifying/etc/rc.local, adding Ulimit-shn 51200

5.Increase php-fpm Open File Description rlimit:

# vi/path/to/php-fpm.conf

Find "<value name=" Rlimit_files ">1024</value>"

Change 1024x768 to 4096 or higher number.

Restart php-fpm.


5. Increase the limit of PHP-FPM open file descriptors

:

# vi/path/to/php-fpm.conf

Found it

"<value name=" Rlimit_files ">1024</value>"

Change 1024 to 4096 or higher

.

Restart PHP-FPM.

6. Using PHP Code accelerator,e.g eaccelerator, XCache. and set "Cache_dir" To/dev/shm on Linux.
6. Use PHP code accelerators, such as Eaccelerator, XCache. On the Linux platform you can point ' cache_dir ' to/dev/shm

PHP 502 Solution appears

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.