1

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.

2 Answers 2

1

You could try using the HttpEchoModule to accomplish what you need. Then you could do something like this:

location ~ \.php(/|$) {
    fastcgi_pass php;
    echo_sleep 3;
    fastcgi_next_upstream http_503;
}
1
  • Thanks Nathan, HttpEchoModule looks interesting. Your example snippet delays all requests, also not-forwarded, but I will do some experimenting... Maybe this could be combined with proxy_pass or something. Well, needs some research. ;) Jul 15, 2013 at 7:54
0

Not an nginx answer, but you could put Varnish as a front-end to handle this specific case. Check out Saint mode as a possibility.

https://www.varnish-cache.org/trac/wiki/VCLExampleSaintMode

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .