My FastCGI (PHP-FPM) application may encounter a situation where it needs some time to heal itself up. I'd like to tell to nginx that it should wait a few seconds and then resend the request to the FastCGI backend.
I have already experimented a hacky setup where nginx is configured with fastcgi_next_upstream http_503
(see docs) with an upstream with same fastcgi configuration twice:
upstream php {
server 127.0.0.1:9000;
server 127.0.0.1:9000;
}
location ~ \.php(/|$) {
fastcgi_pass php;
fastcgi_next_upstream http_503
}
The PHP application will reply with 503
when it needs some time and space, making nginx to "move forward" to the next upstream that is apparently the same server. Unfortunately nginx does second call in milliseconds.
I'd like to delay nginx second call by a few seconds to make sure that PHP backend is fully up and running after the erroneous situation. So, how to add proper delay before the second try?
Behind the scenes, I need to recycle the whole PHP-FPM process due how MongoDB driver handles replica set failover. That's why I can't handle the case fully at PHP level but need to release the PHP process for a short period.