September 30, 2014

Nginx: pass request to location B if location A fails with 404

If you need to make Nginx to pass request to location B if location A fails with 404 error, then I've got a solution for you.

It turns out to be a rather simple task. All you need is:

  • Define location A.
  • Define location B.
  • Point A's error_page to B.
  • Allow interception of errors from A.

Here is an example:

Here we have a uWSGI worker listening requests on 9001 port. In my case it's a Django application running under Vagrant. If a requested page will not be found, request will be passed to some another application listening on port 5000.

If you need to deal with something different from uWSGI, you might change names from "uwsgi_*" to "proxy_*", just like in case of 'fallback' location.


You can handle errors with other status codes too.


Feel free to dig around full example of Nginx config. Maybe it will become useful too. Good luck!

No comments:

Post a Comment