The Battle of Moscow PHP-502 bad Gateway

Source: Internet
Author: User
Tags php error php script dedicated server high cpu usage

Objective


In fact, this tabloid is wrong before the glitch but also can cause a murder, so I took a change php-fpm.conf configuration nginx.conf configuration is also set accordingly, so today introduce PHP error. Because of the university's leisure time like military research, so this section of fault for the name of the Moscow defense!


The error is as follows:

650) this.width=650; "Src=" https://s4.51cto.com/wyfs02/M01/00/D9/wKiom1meT1miexyKAABdEfRvuY8108.png-wh_500x0-wm_ 3-wmp_4-s_2593476146.png "title=" 2017-08-24_111147.png "alt=" Wkiom1met1miexykaabdefrvuy8108.png-wh_50 "/>


php-fpm5.4 Content Detailed

Now all with php-fpm5.6 7.0 This 5.4 php-fpm is also considered as a summary of the previous!


After the PHP5.4 is installed, the default profile for FPM


/usr/local/php/etc/php-fpm.conf


Vim/usr/local/php/etc/php-fpm.conf


650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/9F/89/wKioL1meUm6AAyI4AABlWXybcoM792.png-wh_500x0-wm_ 3-wmp_4-s_4097565232.png "title=" 2017-08-24_121328.png "alt=" Wkiol1meum6aayi4aablwxybcom792.png-wh_50 "/>


php-fpm.conf configuration Details: pm = dynamic How to control the child process, the options are static and dynamic, the default is dynamic; If static is selected, the Pm.max_ Children specifies the number of fixed child processes.

If dynamic is selected, it is determined by the following parameters:
Pm.max_children, maximum number of child processes
Pm.start_servers, number of processes at startup
Pm.min_spare_servers, the minimum number of idle processes is guaranteed, and if the idle process is less than this value, a new child process is created
Pm.max_spare_servers, the maximum number of idle processes is guaranteed, and if the idle process is greater than this value, the cleanup

For a dedicated server, the PM can be set to static. Pm.max_requests sets the number of requests for services before each child process is reborn. is useful for third-party modules that may have a memory leak. If set to ' 0′, the request is always accepted. Set to 500 (default 0).

pm.min_spare_servers = 8  pm.max_spare_servers = 32   after the php-fpm-t   or sbin directory appears as follows: notice:configuration File/ Usr/local/php/etc/php-fpm.conf test is successful indicates correct.

Test configuration file is normal, no problem, kill the current FPM process /usr/local/php/sbin/php-fpm or Systemctl restart PHP-FPM start

Sometimes, the Linux server running Nginx, php-cgi (PHP-FPM) Web services, suddenly the system load up, use the top command to view, many php-cgi process CPU utilization is close to 100%. Later, I tracked the discovery that this kind of situation is closely related to PHP's file_get_contents () function.

  

In large and medium-sized Web sites, API interface calls based on the HTTP protocol are commonplace. PHP programmers like to use the simple and convenient file_get_contents ("http://example.com/") function to get the return content of a URL, but if http://example.com/the site responds slowly, file _get_contents () will always be stuck there and will not time out.

  

We know that in php.ini , there is a parameter max_execution_time can set the maximum execution time for PHP scripts, but in php-cgi (PHP-FPM), this parameter will not work. The real ability to control the maximum execution time for PHP scripts is the following parameters in the php-fpm.conf configuration file:

<value name= "Request_terminate_timeout" >0s</value>


Attention! The format of the new PHP-FPM in the configuration file is:

request_terminate_timeout=0s


The default value is 0 seconds, which means that the PHP script will continue to execute. This way, when all the php-cgi processes are stuck in the file_get_contents () function, the nginx+php WebServer can no longer process the new PHP request, and Nginx will return "502 bad Gateway" to the user. To modify this parameter, it is necessary to set the maximum execution time for a PHP script, but the symptom is not a cure. For example, to 30s, if file_get_contents () to get a slow page content, which means that 150 php-cgi process, only 5 requests per second, WebServer also difficult to avoid "502 bad Gateway."

  

To do a thorough solution, only PHP programmers can get rid of the habit of directly using file_get_contents ("http://example.com/"), but slightly modified, add a time-out, in the following way to implement an HTTP GET request. If you feel trouble, you can encapsulate the following code as a function yourself.

Of course, the reason that causes the php-cgi process CPU 100% is not the only one, then, how to determine the file_get_contents () function is caused?

First, open PHP <value name= "request_slowlog_timeout" >3s</value> record slow execution log

New PHP-FPM want to go to the file format is:

Slowlog=/tmp/slow.log

Request_slowlog_timeout=3s


The number of code lines that are slow to execute is printed in the log.

  

First, use the top command to see the php-cgi process with high CPU usage.

Find the PID of one of the CPU 100% php-cgi processes to follow the command:

Strace-p 10747

  

If the screen displays:

650) this.width=650; "Src=" https://s3.51cto.com/wyfs02/M01/9F/86/wKioL1meOySjBeP0AAAOa2sHbu0239.png-wh_500x0-wm_ 3-wmp_4-s_3129928619.png "title=" 2017-08-24_103504.png "alt=" Wkiol1meoysjbep0aaaoa2shbu0239.png-wh_50 "/>


PHP-CGI (PHP-FPM) uses libevent, while libevent above the Linux 2.6 kernel uses the epoll I/O model to process FastCGI network requests instead of Select/poll. In the number of lines of code for slow logging, including file_get_contents and other functions, file_get_contents, such as the Client initiating an HTTP request, uses the Select/poll model, which means that only file_get _contents, such as the network operation function that satisfies the "TCP request does not timeout by default, uses the Select/poll model, process CPU 100%", causes the strace-p to see this situation.


Summary: Not tired of high, the sea does not tire deep

This article from "Li Shilong" blog, declined reprint!

The Battle of Moscow PHP-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.