Riko
Riko

Reputation: 131

Serving multiple Django applications with Nginx and Gunicorn under same domain

Now I have one Django project in one domain. I want to server three Django projects under one domain separated by / .For example: www.domain.com/firstone/, www.domain.com/secondone/ etc. How to configure nGinx to serve multiple Django-projects under one domain? How configure static-files serving in this case?

My current nGinx config is:

server {                                                                                                                             
    listen 80;                                                                                                                       
    listen [::]:80;                                                                                                                  
    server_name domain.com www.domain.com;                                                                               
    return 301 https://$server_name$request_uri;                                                                                     
}                                                                                                                                    

server {                                                                                                                             
    listen       443 ssl;                                                                                                            
    server_name domain.com www.domain.com;                                                                               

    ssl_certificate /etc/nginx/ssl/Certificate.crt;                                                                                  
    ssl_certificate_key /etc/nginx/ssl/Certificate.key;                                                                              

    ssl_session_cache    shared:SSL:1m;                                                                                              
    ssl_session_timeout  5m;                                                                                                         

    ssl_ciphers  HIGH:!aNULL:!MD5;                                                                                                   
    ssl_prefer_server_ciphers  on;                                                                                                   


    root /home/admin/web/project;                                                                                               

    location /static {                                                                                                               
        alias /home/admin/web/project/static;                                                                                   
    }                                                                                                                                
    location /media {                                                                                                                
        alias /home/admin/web/project/media;                                                                                    
    }                                                                                                                                
    location /assets {                                                                                                               
        alias /home/admin/web/project/assets;                                                                                   
    }                                                                                                                                

    location / {                                                                                                                     
        proxy_pass_header Server;                                                                                                    
        proxy_set_header Host $http_host;                                                                                            
        proxy_redirect off;                                                                                                          
        proxy_set_header X-Real-IP $remote_addr;                                                                                     
        proxy_set_header X-Scheme $scheme;                                                                                           
        proxy_set_header X-Forwarded-Proto https;                                                                                    
        proxy_connect_timeout 75s;                                                                                                   
        proxy_read_timeout 300s;                                                                                                     
        proxy_pass http://127.0.0.1:8000/;                                                                                           
        client_max_body_size 100M;                                                                                                   
    }                                                                                                                                
# Proxies                                                                                                                            
#    location /first {                                                                                                                
#        proxy_pass http://127.0.0.1:8001/;                                                                                          
#    }                                                                                                                               
#                                                                                                                                    
#    location /second {                                                                                                                
#        proxy_pass http://127.0.0.1:8002/;                                                                                          
#    }                                                                                                                               

  error_page 500 502 503 504 /media/50x.html;                                                                                        

Upvotes: 5

Views: 3542

Answers (3)

v25
v25

Reputation: 7656

As @prof.phython correctly states, you'll need to run a separate gunicorn process for each of the apps. This results in you having each app running on a separate port.

Next create a separate upstream block, under http for each of these app servers:

  upstream app1 {
    # fail_timeout=0 means we always retry an upstream even if it failed
    # to return a good HTTP response

    # for UNIX domain socket setups
    #server unix:/tmp/gunicorn.sock fail_timeout=0;

    # for a TCP configuration
     server 127.0.0.1:9000 fail_timeout=0;
  }

Obviously change the title, and port number for each upstream block accordingly.

Then, under your http->server block define the following for each:

location @app1_proxy {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;
  # we don't want nginx trying to do something clever with
  # redirects, we set the Host: header above already.
  proxy_redirect off;
  proxy_pass http://app1; 
}

Make sure the last line there, points at what you called the upstream block (app1) and @app1_proxy should be specific to that app also.

Finally within the http->server block, use the following code to map a URL to the app server:

location /any/subpath {
  # checks for static file, if not found proxy to app
  try_files $uri @app1_proxy;
} 

Upvotes: 4

Mehran
Mehran

Reputation: 1304

What prof.phython said should be correct. I'm not an expert on this but I saw a similar situation with our server as well. Hope the shared nginx.conf file helps!

server {
    listen 80;
    listen [::]:80;
    server_name alicebot.tech;
    return 301 https://web.alicebot.tech$request_uri;
}

server {
    listen 80;
    listen [::]:80;
    server_name web.alicebot.tech;
    return 301 https://web.alicebot.tech$request_uri;
}
server {
    listen 443 ssl;
    server_name alicebot.tech;
    ssl_certificate /etc/ssl/alicebot_tech_cert_chain.crt;
    ssl_certificate_key /etc/ssl/alicebot.key;

    location /static/ {
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        proxy_ignore_headers "Set-Cookie";
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/html/alice/alice.sock;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

server {
    listen 443 ssl;
    server_name web.alicebot.tech;
    ssl_certificate /etc/letsencrypt/live/web.alicebot.tech/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/web.alicebot.tech/privkey.pem; # managed by Certbot


    location /static/ {
        autoindex on;
        alias /var/www/html/static/;
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        proxy_ignore_headers "Set-Cookie";
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/var/www/alice_v2/alice/alice.sock;
        proxy_set_header X-Forwarded-Host $server_name;
        proxy_set_header X-Real-IP $remote_addr;
        add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }

}
server {
    listen 8000 ssl;
    listen [::]:8000 ssl;
    server_name alicebot.tech;
    ssl_certificate /etc/ssl/alicebot_tech_cert_chain.crt;
    ssl_certificate_key /etc/ssl/alicebot.key;

    location /static/ {
        autoindex on;
        alias /var/www/alice_v2/static/;
        expires 1M;
        access_log off;
        add_header Cache-Control "public";
        proxy_ignore_headers "Set-Cookie";
    }

    location / {
         include proxy_params;
         proxy_pass http://unix:/var/www/alice_v2/alice/alice.sock;
    }
}

As you can see we had different domain names here, which you wouldn't be needing. So you'll need to change the server names inside the server {...}

Upvotes: 1

r4v1
r4v1

Reputation: 365

You have to run your projects on different ports like firsrone on 8000 and secondone on 8001. Then in nginx conf, in place of location /, you have to write location /firstone/ and proxy pass this to port 8000 and then write same location object for second one as location /secondone/ and proxy pass it to port 8001.

For static files and media, you have to make them available as /firstone/static and same for secondone. Other way is to specify MEDIA_ROOT and STATIC_ROOT same for both the projects.

Upvotes: 5

Related Questions