Nginx – How to insert a delay in response with nginx

nginx

I have a fast-cgi application that do some work at http://myapp.com/do?arg=x. The job may take a few minutes. So I want to keep redirecting the users to the same URL (with 307) until the job is done, and serve the results with a 200 OK. I would like to slow done the response rate, so that it takes the client 30s (for example) to receive a 307. Obviously, I don't want to do in the back-end because I need to handle other jobs. It is possible to tell nginx to insert a delay, or rate limit the response for a particular URL?

I've looked at limit_req, but I don't think it can be used to rate limit unique URLS (http://myapp.com/do?arg=x versus http://myapp.com/do?arg=y) but rather unique IP addresses.

Best Answer

You might want to restructure your application to better handle the situation. I would have two scripts: one is the web page the user hits. The second is a back end script that dose the job.

Script two can be a daemon, or you can use non-blocking forking if your environment supports it from script one. Script one should initiate the job in script two, then push data to the user using something like EventStream (or WebSockets, although thats a major overkill here) updating them of the status. You can send a 200 or some other indication when your done.