1

I am following the guide at https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04 to setup my NGINX / UWSGI / FLASK application however it only documents the instructions on getting the application to work when navigating to root (/). So far everything is working how the tutorial describes, but how do I get this working for additional routes such as /tool/project. If I try to add /tool/project location to nginx config using the same parameters as root, it doesn't work and just returns a 404. What am I missing?

I have a symlink in /usr/share/html/nginx/tools to the templates folder of which contains index.html of project

user@vm:/usr/share/nginx/html/tools$ ls -l
total 0
lrwxrwxrwx 1 root root 47 Sep 23 14:32 project -> /home/username/git/project/templates/

My NGINX config /etc/nginx/sites-enabled/project:

server {
        listen 80;
        server_name 192.168.0.x;

        location / {
                include         uwsgi_params;
                uwsgi_pass      unix:/home/username/git/project/project.sock;
        }

        location /tools/project/ {
                include uwsgi_params;
                uwsgi_pass unix:/home/username/git/project/project.sock;
        }
}

WSGI config for project:

[Unit]
Description=uWSGI instance to serve project
After=network.target

[Service]
User=username
Group=www-data
WorkingDirectory=/home/username/git/project
Environment="PATH=/home/username/git/project/env/bin"
ExecStart=/home/username/git/project/env/bin/uwsgi --ini project.ini

[Install]
WantedBy=multi-user.target

Flask Code:

app = Flask(__name__, static_url_path='',
static_folder='/usr/share/nginx/html/'
)
resource = '/usr/share/nginx/html/Common/custom/template/v1.0.0'

##############################################################################
@app.route("/")
def build_form() -> Response:
    return render_template('index.html')

##############################################################################

if __name__ == "__main__":
    app.run(host='0.0.0.0')
~                           
Jukebox
  • 93
  • 14

1 Answers1

1

I'm no expert but I'd say think of it as ngnix serving your flask application (through uwsgi) and passing the route handling to it. So instead of creating another location in your ngnix config, handle all the internal routing from within your flask app, like so:

@app.route("/tools/projects")
def do_smthg_re_projects():
    return "something"

But if there is a path through which you want to serve something other than your flask app, then you might configure it in your ngnix config as a separate location (as in serving static files through a route /static)

selmanbey
  • 318
  • 1
  • 8
  • 1
    This is indeed a working solution. However I still am curious on how to get this to work via nginx. It will help run multiple tools deployed to the same server by maintaining it through one config file. Thank you for your response! – Jukebox Sep 24 '20 at 18:36
  • If you are going to run multiple apps, serving on multiple locations (or even domains) then I think you need different sockets and different `.ini` files (a uwsgi server for each app). In that scenario, your config could look something like the way described here: https://stackoverflow.com/questions/34692600/how-to-host-multiple-flask-apps-under-a-single-domain-hosted-on-nginx – selmanbey Sep 24 '20 at 18:55