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;
}
}

2 Answers 2

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/;
    }
}
Sign up to request clarification or add additional context in comments.

10 Comments

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!
Append a slash to http://backend1 and http://backend2, see more info here. I have updated my answer.
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.
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
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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.