1

Is it possible to configure Nginx to proxy to a uwsgi server (uwsgi_pass), falling back to a standard http proxy (proxy_pass) if the uwsgi server is not available?

My development environment runs as a standard HTTP server on a given port, but in production it will be running under a uWSGI socket. I would like to be able to develop my app, then (still on my development machine) launch the the app under a uwsgi server for testing without having to switch my nginx config as well each time. If nginx could be configured to try with the uwsgi_pass, but if it got an error (because the uwsgi server is not running) fall back to the proxy_pass, that would be ideal.

I could also see this being handy in a production environment: normally, just proxy to the local uwsgi socket, but if for some reason that's not working (for some arbitrary definition of not working), proxy to some secondary http server somewhere else. However, for me for now it's just to make development more convenient.

2
  • Do you mean an http response error? or just an error page from the app – Drifter104 Aug 26 '16 at 16:39
  • You could probably argue for either/both, however specifically for my use case I'm talking total failure: the uswsgi server is not responding, because it is not running. I'll update my question to make this clear. – ibrewster Aug 26 '16 at 17:05
4

Assuming you're going to get a 502 timeout error from the uWSGI backend the following should do what you are looking for.

uwsgi_intercept_errors on;
error_page 502 = @fallback;

location / {
    uwsgi_pass uwsgi://192.168.0.1:9000;
    <rest of your config>
    }

location @fallback {
    proxy_pass http://192.168.0.2;
    <rest of your config>
    }

Obviously substitute in your own values but these are the important parts. If you find you get a different http response you can edit as needed.

1
  • Looks like it's a 502 - bad gateway error. Otherwise, this appears to work. Thanks! – ibrewster Aug 29 '16 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.