Serving multiple Django applications with Nginx and Gunicorn under same domain











up vote
1
down vote

favorite
1












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;









share|improve this question


























    up vote
    1
    down vote

    favorite
    1












    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;









    share|improve this question
























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      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;









      share|improve this question













      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;






      django nginx gunicorn






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 11 at 8:44









      Riko

      5118




      5118
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote













          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.






          share|improve this answer




























            up vote
            0
            down vote













            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 {...}






            share|improve this answer





















            • Thx! But this config for subdomains
              – Riko
              Nov 11 at 15:26


















            up vote
            0
            down vote













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





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247115%2fserving-multiple-django-applications-with-nginx-and-gunicorn-under-same-domain%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              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.






              share|improve this answer

























                up vote
                2
                down vote













                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.






                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 11 at 10:02









                  prof.phython

                  887




                  887
























                      up vote
                      0
                      down vote













                      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 {...}






                      share|improve this answer





















                      • Thx! But this config for subdomains
                        – Riko
                        Nov 11 at 15:26















                      up vote
                      0
                      down vote













                      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 {...}






                      share|improve this answer





















                      • Thx! But this config for subdomains
                        – Riko
                        Nov 11 at 15:26













                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      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 {...}






                      share|improve this answer












                      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 {...}







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 11 at 10:11









                      Mehran

                      218110




                      218110












                      • Thx! But this config for subdomains
                        – Riko
                        Nov 11 at 15:26


















                      • Thx! But this config for subdomains
                        – Riko
                        Nov 11 at 15:26
















                      Thx! But this config for subdomains
                      – Riko
                      Nov 11 at 15:26




                      Thx! But this config for subdomains
                      – Riko
                      Nov 11 at 15:26










                      up vote
                      0
                      down vote













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





                      share|improve this answer

























                        up vote
                        0
                        down vote













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





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









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





                          share|improve this answer












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






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 12 at 16:00









                          v25

                          1418




                          1418






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53247115%2fserving-multiple-django-applications-with-nginx-and-gunicorn-under-same-domain%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Full-time equivalent

                              さくらももこ

                              13 indicted, 8 arrested in Calif. drug cartel investigation