Nginx Config for 2 django applications 2 different endpoints 1 server host

Viewed 359

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

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.

Related