Solve the 502 Bad gateway problem caused by the false death of php-cgi

Source: Internet
Author: User
Tags fpm php code php script socket time limit

If the php-cgi process is insufficient, the php execution time is long, or the php-cgi process is dead, a 502 error will occur.

1). Long php execution time

If the server performance is good enough and the bandwidth resources are sufficient, you can directly set "request_terminate_timeout" to 0 s if the PHP script does not have loops or bugs.
Php-fpm.conf around 103 lines

The code is as follows: Copy code

<Value name = "request_terminate_timeout"> 0 s </value>

0 s means to keep the PHP-CGI running without a time limit.
And if you can't do that, that is to say, your PHP-CGI may have a BUG, or if your bandwidth is insufficient or your PHP-CGI is suspended for other reasons, you are advised to assign a value to "request_terminate_timeout", which can be set based on the performance of your server. In general, the better the performance, the higher you can set, 20 minutes-30 minutes can be done. Because my PHP script on the server takes a long time to run, sometimes it may take more than 10 minutes because I set it to 900 seconds,
<Value name = "request_terminate_timeout"> 900 s </value>
This will not cause the PHP-CGI to die with the 502 Bad gateway error

2) insufficient php-cgi process count

The default number of php-cgi connections is 502 if the number of connections is large.
About 79 rows php-fpm.conf

The code is as follows: Copy code
<Value name = "max_children"> 256 </value>

These tips may be useful to you if your high-load website manages FastCGI using a PHP-FPM :)

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

2. Increas PHP FastCGI child number to 100 and even more. Sometime, 200 is OK! (On 4 GB memory server );
2. Adjust the number of PHP FastCGI sub-processes to 100 or above and 200 on the 4G memory server.
Note: for my 1G testing machine, 64 is the best. We recommend that you use stress testing to obtain the best value.

3. Using socket php FastCGI, and put into/dev/shm on Linux;
3. Use socket to connect to FastCGI. The linux operating system can be stored in/dev/shm.
Note: set <value name = "listen_address">/tmp/nginx in the php-fpm.cnf. socket </value> can connect to FastCGI through socket./dev/shm is the memory file system, and it will be faster to put it in the memory.

4. Increase Linux "max open files", using the following command (must be root ):
# Echo 'ulimit-HSn 65536 & prime;>/etc/profile
# Echo 'ulimit-HSn 65536>/etc/rc. local
# Source/etc/profile
4. Increase the number of files opened in the Linux kernel. You can use these commands (the root account must be used)
Echo 'ulimit-HSn 65536 & prime;>/etc/profile
Echo 'ulimit-HSn 65536 & prime;>/etc/rc. local
Note: I modified/etc/rc. local and added it to 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 1024 to 4096 or higher number.
Restart PHP-FPM.
5. Added restrictions on opening file descriptors for PHP-FPM:
# Vi/path/to/php-fpm.conf
Find "<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 and XCache. On the linux platform, you can point 'cache _ dir' to/dev/shm.

Add the memory overhead of nginx + php-cgi:

The number of processes in each Nginx process is about 15 MB. The worker_processes parameter is used to determine the number of processes to be enabled. Generally, 8 processes can be enabled.
Each php-cgi process consumes about 20 MB of memory. Depending on the situation, it generally costs 128 or 256, and the memory is 512 or higher.
This value can be configured based on the machine performance and the system consumed by php execution.

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: 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.