Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;                                                                                        
like image 995
Riko Avatar asked Mar 05 '23 02:03

Riko


2 Answers

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.

like image 65
r4v1 Avatar answered Apr 27 '23 13:04

r4v1


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;
} 
like image 32
v25 Avatar answered Apr 27 '23 13:04

v25