3

So the problem is I have 1 ip (127.0.0.1 for example)

on my server lives 2 different django applications

1. /api     0.0.0.0:8000
2. /data    0.0.0.0:8090
3. /        this will go to default pages served up by nodejs

I need to figure out the nginx configuration of how to deploy these separate services, each with its own database.

when navigating if an endpoint is hit, it will be routed to the appropriate app otherwise it will default to the nodejs app.

extra info: when logging into /api/admin/ it gets routed to /admin and fails please take into consideration redirections made by django. I have tried a lot of things including setting Host, or Location

This will be a bounty in 2 days so happy hunting.

current nginx

upstream app1 {
    server 0.0.0.0:8000;
}

upstream app2 {
    server 0.0.0.0:8090;
}

server {
    listen       80;
    server_name  localhost;


    location / {
        error_log /var/log/webapp/error.log debug;
        access_log /var/log/webapp/access.log;

        proxy_pass http://0.0.0.0:3000;
        proxy_redirect off;
    }

    location /api {
        location /api/static {
            alias /var/tmp/app1/static;
            #autoindex on;
        }
        proxy_pass http://app1;
        proxy_redirect off;
    }

    location /data {
        location /data/static {
            alias /var/tmp/app2/static;
        }
        proxy_pass http://app2;
        proxy_redirect off;
    }
}
10
  • Are you using docker? Commented Sep 28, 2021 at 13:17
  • can you share the full nginx config file Commented Sep 28, 2021 at 13:18
  • not using docker Commented Sep 28, 2021 at 13:21
  • I will sweeten the deal.... after 2 days, this question becomes eligible for a bounty. The accepted answer will get 75 points. Commented Sep 28, 2021 at 13:34
  • check this: serverfault.com/questions/956888/… Commented Sep 28, 2021 at 13:44

1 Answer 1

2
+50

Are you using gunicorn as WSGI server ?

In case, try adding this to the existing nginx configuration:

location /api {
    proxy_set_header SCRIPT_NAME /api;
    ...

location /data {
    proxy_set_header SCRIPT_NAME /data;
    ...
...

In principle, SCRIPT_NAME will be passed to gunicorn, which in turn should use it as a prefix in all addresses.

I never tried this in a real project, though.

Sign up to request clarification or add additional context in comments.

5 Comments

I have been traveling but will award the points and check your answer a little later. Thanks for the answer
Thank you. Let me know if this helps. If not, I'ld be interested on discussing this further
Hey Mario. Superb answer. I wish I could give you more kudos. I have setup a repo with the working example. github.com/bensialih/django_routing
Im looking into traefik as a possible solution as well
Glad it helped ;) Thank you for sharing a working example :clap:

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.