Hosting multiple python apps different url mount points

Fri 22 December 2017 by Thejaswi Puthraya

If you host multiple (virtual hosting) python web apps (django, flask etc) behind nginx on a single server, you have two options:

  1. Using subdomains
  2. On different URL mount points

Using subdomains, you will have to update your DNS records for every app that you deploy. While the deployment is cleaner (no sharing of cookies etc), this requires a little more effort to setup.

With the second method ie different URL mounts, you don't have to update any DNS record but there is a chance of cookies getting overwritten if you aren't careful.

The gunicorn server handles multiple URL mount points through the SCRIPT_NAME header set by the reverse proxy (in our case nginx).

Here's an nginx config snippet of hosting two different apps A at /abc/ and B at /def/. So when you visit say /abc/about/ and /def/about/, you are visiting the /about/ URL on the apps A and B respectively.

server {
   ...

   location /abc {
        proxy_pass http://localhost:8001;
        include proxy_params;
        proxy_set_header SCRIPT_NAME /abc;
   }

   location /def {
        proxy_pass http://localhost:8002;
        include proxy_params;
        proxy_set_header SCRIPT_NAME /def;
   }
 }