nginx - multiple django apps same domain different

2019-08-27 15:48发布

问题:

I want to serve multiple django projects (actually django rest API apps) On one domain but serve each of them on seperated url. like this:

  1. http://test.com/app1/...
  2. http://test.com/app2/...

and so on. I will be using nginx to config it. But i'm facing some problems that wants your helps:

  1. These apps should have different cookie for each other. cause they have different auth system. so token and cookie in one is not valid for another. How to handle this?
  2. What nginx configs you recommend.

Note:

I don't want full detail cause i know concepts. just some hints and usefull commands will do.

Update:

For example i have a django app which has a url test. and i want this path to be served on server with /app1/test. The problem is that when is send request to /app1/test, Django doesn't recognize it as /test, instead as /app1/test and because /app1 is not registered in urls.py will give 404 error.

here is a sample of my nginx config:

server {
listen 80;
server_name test.com;

location /qpp1/ {
    include uwsgi_params;
    proxy_pass http://unix://home//app1.sock;
}

location /qpp2/ {
    include uwsgi_params;
    proxy_pass http://unix://home//app2.sock;
}
}

回答1:

You can try to play with proxy_cookie_path directive:

server {

    ...

    location /app1/ {
        proxy_cookie_path / /app1/;
        proxy_pass http://backend1/;
    }

    location /app2/ {
        proxy_cookie_path / /app2/;
        proxy_pass http://backend2/;
    }
}

Update

Here is another variant of configuraion to test.

upstream qpp1 {
    server unix:/home/.../app1.sock;
}

upstream qpp2 {
    server unix:/home/.../app2.sock;
}

server {
    listen 80;
    server_name test.com;

    location /qpp1/ {
        include uwsgi_params;
        proxy_cookie_path / /qpp1/;
        proxy_pass http://qpp1/;
    }

    location /qpp2/ {
        include uwsgi_params;
        proxy_cookie_path / /qpp2/;
        proxy_pass http://qpp2/;
    }
}