The nginx docs say that keepalive for fastcgi cannot work if the fastcgi_keep_conn is not on. However, I can't find any further details on this. I already have keepalive enabled server-wide. I am using php-fpm. Are there any issues about using fastcgi_keep_conn that I should be aware of?
2 Answers
Theoretically fastcgi_keep_conn
does increase performance, since the socket between nginx and PHP-FPM stays open after request has been processed. Therefore the time spent opening the socket is saved.
Overall, this would save some milliseconds on each request, so it has not such a big effect.
The documentation simply means that you need to have both an upstream
block with keepalive
option and fastcgi_keep_conn
option in your PHP script block for the connection keepalive to work.
Basically, fastcgi_keep_conn
option tells nginx to make PHP-FPM to keep the connection open after the request, and the keepalive
connection tells nginx to keep the connection open.
-
2The performance difference on a local connection is tiny, so it's worthless if nginx and php-fpm run on the same machine, but if you run php-fpm on a remote host, then it is definitely worth doing this. Jul 14, 2014 at 11:50
You also need to set the keepalive_requests
option of your upstream to the same number as your php-fpm's pm.max_requests
, otherwise you might run into this problem:
https://stackoverflow.com/a/51457613/10115267
Basically you can get one of the following errors every time a php-fpm child process restarts due to pm.max_requests
being reached while nginx is still connected to it:
readv() failed (104: Connection reset by peer) while reading upstream and recv() failed (104: Connection reset by peer) while reading response header from upstream