title: Use one app for several uwsgi instances
body: <p>I have found a lot of examples explaining how we can use uwsgi and emperor mode to achieve multiple apps deployment. This means for me: several apps folder with one vassal (ini, socket, application.py) per app.</p>
  
  <p>I was not yet able to find examples of configurations where there were only one app folder with multiple vassals. This should allow me to serve a multi-tenants app (each customer has its own database). I tested this with two instances. This "seems" to work well. </p>
  
  <ol>
  <li>Is this a good practice or do you have similar setup ? </li>
  <li>Does this provide complete isolation between vassals instances ?</li>
  </ol>
  
  <p>Here is my setup. I am using nginx/uwsgi/python stack (I make use of <a href="http://uwsgi-docs.readthedocs.io/en/latest/ImperialMonitors.html#pg-scan-a-postgresql-table-for-configuration" rel="nofollow">emperor_pg module</a>). This setup allows uwsgi to spawn one vassal per customer A and B. Customers are using url : customerN.mydomain.com/fe1/web</p>
  
  <p><strong>Nginx config :</strong></p>
  
  <pre><code># This virtual host catches all incoming traffic from port 80 (security should be considered if not talking on local
  # network)
  server {
          listen 80;
          # We capture here the subdomain. It is used to designate a customer entity.
          server_name ~^(?&lt;subdomain&gt;.+)\.mydomain\.fr$;
  
  
          # We use a pattern for creating sockets name and path.
          # This allows to spawn vassals automatically by detecting changes in the vassals pg table (emperor_pg)
          # Pattern used is : /tmp/$subdomain$appname.sock
  
  
          location ~favicon\.ico$ {
              root /opt/app-current/web/;
          }
  
          location ~^\/(?&lt;app_name&gt;.+)\/web\/ {
  
              root /opt/web-content/$subdomain/;
          }
  
          location ~^\/(?&lt;app_name&gt;.+)\/ {
  
              # Routing to socket designated by standard pattern
              include uwsgi_params;
              uwsgi_pass unix://tmp/$subdomain$app_name.sock;
  
          }
  
          # When calling root of entity's subdomain, we launch the default app by routing traffic to index.socket
          location / {
              include uwsgi_params;
              uwsgi_pass unix://tmp/$subdomain.sock;
          }
  }
  </code></pre>
  
  <p><strong>The emperor upstart script</strong> using emperor_pg :</p>
  
  <pre><code># Emperor configuration upstart script in `/etc/init/uwsgi.conf` :
  # uWSGI - Manage uWSGI Application Server
  
  description "uWSGI Emperor Mode"
  
  start on (filesystem and net-device-up IFACE=lo)
  stop on runlevel [!2345]
  
  respawn
  
  
  # We use pg mode. This allows to scan a postgresql database.
  # requires sudo apt-get install uwsgi-plugin-emperor-pg
  
  exec /usr/bin/uwsgi --uid www-data --gid www-data --plugin emperor_pg --emperor "pg://host=dbserver.com.com user=saasautomator dbname=saasautomator;SELECT name,config,ts,uid,gid,socket FROM vassals" --logto /var/log/uwsgi.log
  </code></pre>
  
  <p><strong>And an example of vassals</strong> conf files in database. They are using the same application folder :</p>
  
  <pre><code>saasautomator=&gt; SELECT * FROM vassals;
   idvassals |              name               |                             config                             |         ts          |  uid  |  gid  |                socket                 
  -----------+---------------------------------+----------------------------------------------------------------+---------------------+-------+-------+---------------------------------------
           3 | customerAfe1.ini  | [uwsgi]                                                       +| 2004-10-19 10:23:54 | uwsgi | uwsgi | /tmp/customerAfe1.sock
             |                                 |  master = true                                                +|                     |       |       | 
             |                                 |  vaccum = true                                                +|                     |       |       | 
             |                                 |  chdir = /opt/app/                       +|                     |       |       | 
             |                                 |  plugins = python                                             +|                     |       |       | 
             |                                 |  wsgi-file = /opt/app/fe1/application.py +|                     |       |       | 
             |                                 |  processes = 4                                                +|                     |       |       | 
             |                                 |  threads = 2                                                  +|                     |       |       | 
             |                                 |  stats = 127.0.0.1:9191                                        |                     |       |       | 
           4 | customerBfe1.ini | [uwsgi]                                                       +| 2004-10-19 10:23:55 | uwsgi | uwsgi | /tmp/customerBfe1.sock
             |                                 |  master = true                                                +|                     |       |       | 
             |                                 |  vaccum = true                                                +|                     |       |       | 
             |                                 |  chdir = /opt/app/                       +|                     |       |       | 
             |                                 |  plugins = python                                             +|                     |       |       | 
             |                                 |  wsgi-file = /opt/app/fe1/application.py +|                     |       |       | 
             |                                 |  processes = 4                                                +|                     |       |       | 
             |                                 |  threads = 2                                                  +|                     |       |       | 
             |                                 |  stats = 127.0.0.1:9192                                        |                     |       |       | 
  (2 rows)
  </code></pre>
  
  <p>Thanks !</p>