Code Painters The Art of Coding

5Aug/122

WSGI deployment under a subpath using uWSGI and Nginx

It's relatively obvious how to deploy a WSGI application using Nginx, and there are many tutorials out there dealing with this very task. It took me quite a few minutes, however, to figure out how to deploy the application under non-root URL, e.g. http://some.site.com/admin/.

This is typically dealt with by configuring the SCRIPT_NAME CGI parameter, and letting this name be removed from the beginning of the PATH_INFO parameter by the WSGI environment, before the path is passed down to the application. This way the WSGI application routing remains the same, no matter where in the URL tree it is deployed. The application needs to be aware of SCRIPT_NAME parameter only to generate proper URLs to self.

It turned out that Nginx configuration need to contain magical uwsgi_parameter1 30; setting, e.g.:

server {
    // ...
    location /admin/ {
        uwsgi_pass unix:/path/to/uwsgi.socket;
        include uwsgi_params;
        uwsgi_param SCRIPT_NAME /admin;
        uwsgi_modifier1 30;
    }
}

The uwsgi_modifier1 parameter is listed in the Nginx's uWSGI module documentation, but it doesn't really explain what modifier1 is? Why it needs to be 30? The answer can be found in the uWSGI protocol documentation. The modifier1 is simply used to denote the uWSGI protocol's packet type. According to the table, value 30 means:

Standard WSGI request followed by the HTTP request body. The PATH_INFO is automatically modified, removing the SCRIPT_NAME from it.

Packets of type 0 (default) and 30 differ only in how SCRIPT_INFO is handled. I really wish there was a better documentation for this kind of feature. The parameter names used don't help googling, either...

Share:
  • Facebook
  • Digg
  • del.icio.us
  • Twitter
  • LinkedIn
  • Google Bookmarks
  • Reddit
  • StumbleUpon
Comments (2) Trackbacks (0)
  1. Thanks for this!

  2. 10x m8! i’m looking for like 8 days for solution for this stuff, seems like rewrite rules are being reprocessed by nginx since i got the basic hello page when using that… didn’t pass the rewrited address to flask though…
    in any case seems like your solution works so now i can use uwsgi in full python dynamic mode and pass the script names to uwsgi using nginx params per service! 10x!


Leave a comment

No trackbacks yet.