与NGINX,Gunicorn,Django的502 Bad Gateway

时间:2017-02-18 03:59:14

标签: django nginx gunicorn

我尝试使用此guide与NGINX和Gunicorn建立项目。但是,我一直收到502 Bad Gateway错误。

这是我的服务器conf:

server {
    listen 80;
    server_name nexus-staging.chop.edu;
    access_log /webapps/nexus/logs/nginx/nexus-staging.chop.edu.access.log main;
    error_log /webapps/nexus/logs/nginx/nexus-staging.chop.edu.error.log warn;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /webapps/nexus/static_cdn;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_pass http://unix:/webapps/nexus/nexus.sock;
    }
}

这是我的gunicorn服务文件:

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
EnvironmentFile=/etc/sysconfig/nexus
User=svc_dgdnexus
Group=dgd_svc
WorkingDirectory=/webapps/nexus
ExecStart=/webapps/nexus/pyvenv/bin/gunicorn --workers 4 --bind unix:/webapps/nexus/nexus.sock django_config.wsgi:application --name nexus --access-logfile /webapps/nexus/logs/gunicorn/nexus-staging.chop.edu.access.log --error-logfile /webapps/nexus/logs/gunicorn/nexus-staging.chop.edu.error.log

[Install]
WantedBy=multi-user.target

这是nginx错误:

2017/02/17 22:55:23 [error] 36168#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 10.249.27.89, server: nexus-staging.chop.edu, request: "GET / HTTP/1.1", upstream: "http://unix:/webapps/nexus/nexus.sock:/", host: "nexus-staging.chop.edu"

这是枪声错误:

[2017-02-17 22:53:59 -0500] [36143] [INFO] Booting worker with pid: 36143
[2017-02-17 22:53:59 -0500] [36145] [INFO] Booting worker with pid: 36145
[2017-02-17 22:55:23 -0500] [36137] [CRITICAL] WORKER TIMEOUT (pid:36140)
[2017-02-18 03:55:23 +0000] [36140] [INFO] Worker exiting (pid: 36140)
[2017-02-17 22:55:23 -0500] [36202] [INFO] Booting worker with pid: 36202

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:

这是我的服务器conf:

server {
    listen 80;
    server_name nexus-staging.chop.edu;
    access_log /webapps/nexus/logs/nginx/nexus-staging.chop.edu.access.log main;
    error_log /webapps/nexus/logs/nginx/nexus-staging.chop.edu.error.log warn;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        alias /webapps/nexus/static_cdn/;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect off;
        proxy_pass http://127.0.0.1:8000;
    }
}

这是gunicorn服务文件:

[Unit]
Description=gunicorn daemon
After=network.target
After=syslog.target

[Service]
EnvironmentFile=/etc/sysconfig/nexus
User=svc_dgdnexus
Group=dgd_svc
WorkingDirectory=/webapps/nexus
ExecStart=/webapps/nexus/pyvenv/bin/gunicorn django_config.wsgi --workers 4 --access-logfile /webapps/nexus/logs/gunicorn/nexus-staging.chop.edu.access.log --error-logfile /webapps/nexus/logs/gunicorn/nexus-staging.chop.edu.error.log
Restart=on-failure

[Install]
WantedBy=multi-user.target

确保您在django设置中的ALLOWED_HOSTS类似于:.nexus-staging.chop.edu.对其工作至关重要。