在同一域下使用Nginx和Gunicorn服务多个Django应用程序

时间:2018-11-11 08:44:38

标签: django nginx gunicorn

现在我在一个域中有一个Django项目。我想在一个由/分隔的域下管理三个Django项目。例如:www.domain.com/firstone/,www.domain.com/secondone/等。如何配置nGinx在一个域下服务多个Django项目?在这种情况下如何配置投放的静态文件?

我当前的nGinx配置为:

    service2 = IndustryService.objects.get(title=service.title)
    userserv.title = service2

3 个答案:

答案 0 :(得分:2)

您必须在不同的端口(例如8000上的firsrone和8001上的secondone)上运行项目。 然后在nginx conf中代替location /,您必须编写location /firstone/并将其代理传递给端口8000,然后将第二个相同的位置对象写为location /secondone/并将其传递给代理端口8001。

对于静态文件和媒体,必须使它们以/ firstone / static可用,而对于secondone则相同。 另一种方法是为两个项目指定MEDIA_ROOT和STATIC_ROOT相同。

答案 1 :(得分:0)

弗森教授所说的应该是正确的。我不是这方面的专家,但是我的服务器也遇到了类似情况。希望共享的nginx.conf文件有帮助!

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

如您所见,我们在这里拥有不同的域名,您将不需要这些域名。因此,您需要更改服务器{...}

中的服务器名称

答案 2 :(得分:0)

正如@ prof.phython正确指出的那样,您需要为每个应用程序运行单独的gunicorn进程。这样可以使每个应用程序都在单独的端口上运行。

接下来,在http下为这些应用服务器中的每一个创建一个单独的上游块:

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

显然,相应地更改每个上游模块的标题和端口号。

然后,在您的http->server块下,为每个变量定义以下内容:

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

确保最后一行指向您称为上游区块(app1)的位置,并且@app1_proxy也应特定于该应用。

最后在http->server块中,使用以下代码将URL映射到应用服务器:

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