How to host multiple flask apps under a single domain hosted on nginx?

后端 未结 3 1956
一整个雨季
一整个雨季 2020-12-24 14:28

What I am trying to achieve:

I have an nginx web server hosting mydomain.com. When someone types my domain.com into their client I would like my server to serve inde

3条回答
  •  情深已故
    2020-12-24 14:42

    The problem is here: both flask apps have no idea that they were served from subdirectory in your domain. Nginx is passing full URL to them, not URL relative to that directory, so every URL is dispatched relative to root. Let's see that in example:

    Assume that you have 3 views in your flaskapp, served on urls /one/, /two/ and /three/. So when you're trying to access view on URL /one/, you are typing address: http://yourdomain.com/flaskapp/one/. Flask will receive from nginx URL /flaskapp/one/ but there is no such view in that app, so it will send 404 in response.

    What you can do is inform each flaskapp that they aren't served on domain root, but on particular subdirectory. You can achieve this by sending SCRIPT_NAME uwsgi_param with subdirectory location in value, so your nginx config will have:

    location /flaskapp {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/flaskapp.sock;
        uwsgi_param SCRIPT_NAME /flaskapp;
    }
    
    location /flaskapp2 {
        include uwsgi_params;
        uwsgi_pass unix:/tmp/flaskapp2.sock;
        uwsgi_param SCRIPT_NAME /flaskapp2;
    }
    

提交回复
热议问题