How to enable PHP-FPM connection keep-alive and reuse in Nginx?

To enable PHP-FPM connection keep-alive and reuse in Nginx, follow these steps:

  1. Open your Nginx configuration file. This file is typically located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default.

  2. Locate the http block in the configuration file.

  3. Inside the http block, add or modify the following parameters:

    http { # ... fastcgi_keep_conn on; fastcgi_pass_header Connection "keep-alive"; fastcgi_buffers 256 4k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; # ... }

    These parameters enable keep-alive connections for PHP-FPM and set the buffer sizes for handling incoming requests.

  4. Save the changes and exit the configuration file.

  5. Restart or reload Nginx to apply the changes:

    sudo service nginx restart

By enabling these options, Nginx will reuse the same connection to the PHP-FPM server for multiple requests, reducing the overhead of establishing a new connection for each request and improving performance.