使用 Nginx 的 Django 项目的多个 Gunicorn 守护进程导致其中一个出现 502 Bad Gateway

使用 Nginx 的 Django 项目的多个 Gunicorn 守护进程导致其中一个出现 502 Bad Gateway

我想在同一台服务器上运行两个 Django 应用程序,分别使用 Nginx 和 Gunicorn。我可以单独运行它们,但不能同时运行它们:当我尝试时,一个可以正常使用,但另一个会收到 502。以下是我的配置文件:

第一个项目的第一个 nginx 配置:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80;
    # listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name domain1.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/domain1/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/domain1/static;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

对于第二个 django 项目:

upstream app_server_2 {
    server 127.0.0.1:8000 fail_timeout=0;
}

server {
    listen 80;
    # listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name domain2.com;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/domain2/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/domain2/static;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server_2;
    }
}

Gunicorn 新贵脚本:

description "Gunicorn daemon for Django project"

start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]

# If the process quits unexpectedly trigger a respawn
respawn

setuid django
setgid django
chdir /home/django

exec gunicorn \
    --name=domain1 \
    --pythonpath=domain1 \
    --bind=127.0.0.1:9000 \
    --config /etc/gunicorn.d/gunicorn.py \
    domain1.wsgi:application

exec gunicorn \
    --name=domain2 \
    --pythonpath=domain2 \
    --bind=127.0.0.1:8000 \
    --config /etc/gunicorn.d/gunicorn.py \
    domain2.wsgi:application

有什么建议么?

更新 2016.01.24

在调试时,我将 Gunicorn upstart 脚本分成两个(每个项目一个),然后重命名它们。我发现我遇到了以下 4 个错误(每个错误 2 个,一个用于启动,一个用于在 Gunicorn 守护进程重启时停止):

/etc/gunicorn.d/gunicorn.py:2: RuntimeWarning: Parent module '/etc/gunicorn.d/gunicorn' not found while handling absolute import
    from os import environ

这是我的 gunicorn.py:

"""gunicorn WSGI server configuration."""
from multiprocessing import cpu_count
from os import environ


def max_workers():
    return cpu_count() * 2 + 1

max_requests = 1000
worker_class = 'gevent'
workers = max_workers()

相关内容