3

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;
}
}
Reza Torkaman Ahmadi
  • 2,958
  • 2
  • 20
  • 43

2 Answers2

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/;
    }
}
Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
  • The problem is that django doesn't understand that it's base url is changed. for example when i try to get /app2/swagger it will give 404 error. because it checks from base url / and see that there is no app2 url registered! – Reza Torkaman Ahmadi Dec 08 '18 at 18:42
  • Append a slash to `http://backend1` and `http://backend2`, see more info [here](https://stackoverflow.com/questions/53649885/a-little-confused-about-trailing-slash-behavior-in-nginx). I have updated my answer. – Ivan Shatsky Dec 08 '18 at 19:02
  • No the problem is I guess from django. For example if app1 is a django app, and has a url /test, sending request to /app1/test should serve /test path of django, but django recognize this path as /app1/test which is not available and will give error. – Reza Torkaman Ahmadi Dec 08 '18 at 19:11
  • I guess I found the problem. It seems that i should change base url of all my django Urls too. and add a /app1 for example before them. The nginx config seems fine. let me try this and let you know the answer – Reza Torkaman Ahmadi Dec 08 '18 at 19:50
  • Wait, there is no need to change django apps. It seems you have not tried appending trailing slashes to your backends? Do it and test your config, at least it will pass `/app1/test` as `/test` to your django app. I just tested such configuration (with backend on tcp port, not unix socket) and it works. Change `http://unix://home//app1.sock;` to `http://unix://home//app1.sock/;` and try to test it. – Ivan Shatsky Dec 08 '18 at 19:53
  • I checked on unix. not working. maybe i should test it with http not unix. I'll check all of them tomorrow and share the result here. By the way tnx for helping – Reza Torkaman Ahmadi Dec 08 '18 at 20:00
  • Updated my answer once again. Can you try second variant of configuration please? – Ivan Shatsky Dec 08 '18 at 20:02
  • something weird happened. I changed to http and it seems like is working. :) let me check more. – Reza Torkaman Ahmadi Dec 08 '18 at 20:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184936/discussion-between-reza-torkaman-ahmadi-and-ivan-shatsky). – Reza Torkaman Ahmadi Dec 08 '18 at 20:08
  • Worked like a charm. Thanks bro. I will update your answer with some django configs for better understanding for other. Problem solved. :) – Reza Torkaman Ahmadi Dec 08 '18 at 20:09
0

Because I am not using nginx, django's SESSION_COOKIE_PATH-Variable was my solution.

https://docs.djangoproject.com/en/3.1/ref/settings/#session-cookie-path

In your example, you could set it to:

app1

SESSION_COOKIE_PATH = "/app1/"

app2

SESSION_COOKIE_PATH = "/app2/"

Afterwards clear the cookie cache for the domain in your browser, if you've logged in before.

ralfzen
  • 327
  • 3
  • 8