How to configure and optimize FastCGI in Nginx

10-30-2023

fastcgi:

Fastcgi is developed and improved from cgi. The main disadvantage of the traditional cgi interface is poor performance, because every time the http server encounters a dynamic program, it needs to restart the script parser to perform parsing, and then the result is returned to the http server. This is almost unavailable when dealing with high concurrent access. In addition, the traditional cgi interface is also very poor in security, and it is rarely used now.

Fastcgi interface adopts c/s structure, which can separate the http server from the script parsing server and start one or more script parsing daemons on the script parsing server at the same time. Every time the http server encounters a dynamic program, it can directly deliver it to the fastcgi process for execution, and then return the results to the browser. In this way, the http server can handle static requests exclusively or return the results of the dynamic script server to the client, which greatly improves the performance of the whole application system.

Nginx does not support direct calling or parsing of external programs, and all external programs (including php) must be called through fastcgi interface. The fastcgi interface is a socket under linux (this socket can be a file socket or an ip socket). In order to call cgi programs, a wrapper of fastcgi (which can be understood as a program for starting another program) is needed, and this wrapper is bound to a fixed socket, such as a port or a file socket. When nginx sends a cgi request to this socket, the wrapper accepts the request through the fastcgi interface, and then derives a new thread, which calls an interpreter or an external program to process the script and read the returned data; Then, the wrapper passes the returned data to nginx; along the fixed socket through the fastcgi interface; Finally, nginx sends the returned data to the client, which is the whole operation process of nginx+fastcgi. Detailed process, as shown in the figure.

Example:

An example of optimal configuration of fastcgi parameters in nginx

After configuring nginx+fastcgi, in order to ensure the high-speed and stable operation of php environment under nginx, some fastcgi optimization instructions need to be added. An optimization example is given below, and the following code is added to the http hierarchy in the nginx master configuration file.

fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=test:10m inactive=5m; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; fastcgi_cache test; fastcgi_cache_valid 200 302 1h; fastcgi_cache_valid 301 1d; fastcgi_cache_valid any 1m;

The meaning of the above code:

In the first line of code, the file path, directory structure level, keyword area storage time and inactive data deletion time of fastcgi cache are set.

Fastcgi_connect_timeout specifies the timeout for connecting to the backend fastcgi.

Fastcgi_send_timeout specifies the timeout for transmitting requests to fastcgi, which is the timeout for transmitting requests to fastcgi after two handshakes have been completed.

Fastcgi_read_timeout specifies the timeout for receiving fastcgi reply, which is the timeout for receiving fastcgi reply after two handshakes have been completed.

Fastcgi_buffer_size is used to specify how large a buffer is needed to read the first part of the fastcgi response. This value indicates that a 64kb buffer will be used to read the first part of the response (response header), which can be set to the buffer size specified by the fastcgi_buffers option.

Fastcgi_buffers specifies how many and how large buffers are needed locally to buffer fastcgi's reply request. If the page size generated by a php script is 256kb, four 64kb buffers will be allocated for caching. If the page size is larger than 256kb, the part larger than 256kb will be cached in the path specified by fastcgi_temp, but this is not a good method because the data processing speed in memory is faster than that in hard disk. Generally, this value should be the middle value of the page size generated by php scripts in the site. If the page size generated by most scripts in the site is 256kb, then this value can be set to 16 16k, 4 64k and so on.

The default value of fastcgi_busy_buffers_size is twice that of fastcgi_buffers.

Fastcgi_temp_file_write_size indicates how large the data block is when writing to the cache file, and the default value is twice that of fastcgi_buffers.

Enabling and naming the fastcgi cache refers to the role of fastcgi_cache. Opening the cache is very useful, which can effectively reduce the load of cpu and prevent the occurrence of 502 errors, but opening the cache will also cause many problems, depending on the specific situation.

Fastcgi_cache_valid and fastcgi are used to specify the cache time of response codes. The values in the example indicate that 200 and 302 responses are cached for one hour, 301 responses are cached for one day, and other responses are cached for one minute.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us