5

I have an nginx reverse proxy server set up to forward requests to an app running on port 5000 via proxy_pass http://localhost:5000;.

This works, however when I restart the app, for about 10 sec any requests immediately return 502 Bad Gateway, until the app reloads again.

Is there any way to set up Nginx to hold those requests while the app is down, retrying every so often? I've tried

upstream backend {                         
  server localhost:5000 fail_timeout=20s; 
  server localhost:5000 backup;          
}
...
    proxy_pass http://backend;

but that doesn't seem to have an effect.

Dax Fohl
  • 283
  • 1
  • 2
  • 7
  • This was discussed on their mailing-list: https://forum.nginx.org/read.php?2,177,177 They don't seem to believe in the use case either. – remram Jul 09 '21 at 13:50
  • 1
    I finally found a working solution at https://serverfault.com/a/871806/187237 using the Lua module – remram Jul 09 '21 at 15:48
  • 2
    Does this answer your question? [nginx proxy retry while backend is restarting](https://serverfault.com/questions/259665/nginx-proxy-retry-while-backend-is-restarting) – remram Jul 09 '21 at 15:49
  • Hmm, that was three companies ago. I can't remember how we eventually solved it. It was a private API so I think we ended up just retrying on the client. – Dax Fohl Jul 09 '21 at 15:55

1 Answers1

1

You have some options :

  • Search into your upstream doc to find a proper reload signal/api instead of a stop & start sequence.

  • If your pages can be cached, define a proxy cache and serve stale pages until the app goes up again.

  • Scale the backend to 2 instances and restart one at a time, then tune proxy_next_upstream if necessary.

Xavier Lucas
  • 13,095
  • 2
  • 44
  • 50