To configure Nginx to handle PHP-FPM connection pooling and reuse, you need to make the following changes to the Nginx configuration file:
Open the Nginx configuration file using a text editor. This file is usually located at /etc/nginx/nginx.conf
or /etc/nginx/conf.d/default.conf
.
Inside the http
block, add or modify the upstream
directive to define the PHP-FPM connection pool. For example:
http {
...
upstream php_fpm {
server unix:/var/run/php/php7.4-fpm.sock;
server unix:/var/run/php/php7.3-fpm.sock;
server unix:/var/run/php/php7.2-fpm.sock;
...
}
...
}
In this example, three PHP-FPM sockets are defined as part of the connection pool. You can add or remove sockets as per your PHP-FPM setup.
location
block to route the requests to the PHP-FPM connection pool. For example:server {
...
location ~ \.php$ {
fastcgi_pass php_fpm;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
...
}
Here, the fastcgi_pass
directive is set to the named upstream php_fpm
, which is the connection pool defined earlier.
Save the changes to the configuration file and exit the text editor.
Test the Nginx configuration for any syntax errors:
sudo nginx -t
If there are no errors, proceed to the next step. Otherwise, correct the errors and then test again.
sudo systemctl reload nginx
With these configurations, Nginx will route PHP requests to the available PHP-FPM sockets in a round-robin fashion, effectively managing connection pooling and reuse.