Recently, an nginx WEB server was built under centos, but some friends said that the following errors often occur:
504 Gateway Time-out
The server didn't respond in time.
The lnmp configuration in CentOS basically uses the same configuration file and has never encountered any problems. After installing the same environment on a vps recently, when the website is online for more than 10 people, the open speed is very slow. Several times, it has reached the maximum timeout of 300 seconds for the script set in nginx, as a result, nginx sent an error code of 504 Gateway Time-out to the client browser. After analyzing the error code, it changed several configuration files and finally avoided this situation.
From the error code, it can be basically determined that it has nothing to do with nginx, mainly because the request submitted to php-fpm fails to provide correct feedback. Generally, when submitting a dynamic request, nginx will directly forward the request to php-fpm, while php-fpm will distribute the php-cgi process to process the related requests, and then return them in sequence, finally, nginx sends the result to the client browser. However, my vps currently runs a pure php application. In fact, all users' requests are php requests, and some of them take a long time, the php-cgi process is always full, and the configuration file of php-fpm itself only opens 10 php-cgi processes, in this way, if there are a few online users, the requests will not be processed properly and errors will occur.
After analyzing the cause, it is easier to do the following: First, modify the configuration of php-fpm:
Change max_children from the previous 10 to the current 30, so that sufficient php-cgi processes can be used;
Change request_terminate_timeout from the previous 0 s to 60 s, so that the timeout time for the php-cgi process to process the script is 60 seconds, which can prevent the process from being suspended and improve the utilization efficiency.
Then, modify several nginx configuration items to reduce the number of FastCGI requests and try to keep buffers unchanged:
Change fastcgi_buffers from 4 64 k to 2 256 k;
Fastcgi_buffer_size is changed from 64 k to 128 K;
Fastcgi_busy_buffers_size is changed from 128 K to 256 K;
Fastcgi_temp_file_write_size is changed from 128 K to 256 K.
Okay, reload the configuration of php-fpm and nginx, and test again. So far, No 504 Gateway Time-out has been found in two weeks.
In addition, the default static Processing Method of php-fpm will make the php-cgi process occupy the memory for a long time and cannot be released, which is also one of the causes of nginx errors, therefore, you can change the php-fpm Processing Method to the apache mode.
<Value name = "style"> apache-like </value>
Tests from the change to the present show that the above method is still very effective, and no Nginx502 bad gateway or 504 Gateway Time-out error was found. Of course, if your VPS or server performance is good enough, you do not need to make any necessary changes based on the actual situation.