Craig
Craig

Reputation: 326

How to serve a flask app out of a sub directory with nginx and uwsgi

A problem I ran into recently was how to run a flask app out of a subdirectory. For example, you might want mysite.com/myapp to run one flask app and mysite.com/some_other to run another script entirely. There are a number of good tutorials on the web for how to run a flask app out of mysite.com/, but when I went to solve the subdirectory problem I found some out of date info.

Upvotes: 3

Views: 2731

Answers (1)

Craig
Craig

Reputation: 326

When I first started looking into this I found a number of sites advocating that I should put uwsgi_param SCRIPT_NAME /mysubdir and uwsgi_modifier1 30 in the nginx config file. Apparently, this is out of date information as of 2017 (nginx nginx/1.10.3 and uwsgi 2.0.15).

The config file below is all that is needed for a sub dir.

server {
    listen 80;
    server_name wf.idt.com;

    location /mysubdir {
        include           /etc/nginx/uwsgi_params;
        uwsgi_pass        unix:///var/python/myapp/myapp.sock;
    }
}

Next you need to add a few items to the uwsgi ini file. Mine is stored in the same dir as the python files. These are the lines to add.

## Settings to deal with the subdirectory
manage-script-name = true
mount=/mysubdir=wsgi.py

So the full .ini file now looks like this

[uwsgi]
module = wsgi:application
#location of log files
logto = /var/log/uwsgi/app/%n.log
master = true
processes = 5
## Settings to deal with the subdirectory
manage-script-name = true
mount=/myapp=wsgi.py
socket = myapp.sock
chmod-socket = 660
vacuum = true
die-on-term = true

Upvotes: 4

Related Questions