Proxymiddleware

A shim for wsgi applications behind nginx

View the Project on GitHub Kellel/ProxyMiddleware

ProxyMiddleware

A collection of WSGI application wrappers

ReverseProxied

A shim for wsgi applications behind nginx

A common problem that I have ran into when hosting wsgi applications behind a nginx reverse proxy is the rewriting of urls being returned from the upstream server.

This little bit of wsgi middleware will rewrite the script name variable based on a variable set in your nginx config.

Add this line to your nginx config:

proxy_set_header X-SCRIPT-NAME /path;

And this line to your wsgi file:

application = ReverseProxied(application)

Note: Variations of this code exist in other places on the internet. I do not claim to be the person who came up with this. I just love it and need somewhere I can easily find it.

Trailing Slash

This module attempts to solve the problem of web apps expecting a trailing slash, but not receiving them. The module does the following in this exact order:

  1. Try the original route. If the route returns anything but a 404 return the response
  2. If the original route does return a 404 then append a slash.
  3. Try the new route. If it succeeds return the response.
  4. If the new route fails then we restore the original environment variables and continue on.

And this line to your wsgi file:

application = TrailingSlash(application)