nginx + gunicorn 502 网关错误

Posted

技术标签:

【中文标题】nginx + gunicorn 502 网关错误【英文标题】:nginx + gunicorn 502 bad gateway 【发布时间】:2014-12-07 07:22:12 【问题描述】:

我正在尝试通过 nginx 和 gunicorn 在 VPS 服务器上部署一个 django 项目,但是当我尝试打开我的页面 rollservice-rostov.ru 时出现 nginx 错误“502 bad gateway”

我的 Nginx 配置有问题,但我不知道是什么问题。

/etc/nginx/sites-enabled/rollservice.conf:

upstream rollservice-rostov.ru 
    server 127.0.0.1:8010 fail_timeout=0;


server 
    listen 80;
    server_name  www.rollservice-rostov.ru;
    rewrite ^/(.*) http://rollservice-rostov.ru/$1 permanent;
    client_max_body_size 4G;
    access_log /home/django/logs/rollservice/nginx.access.log;
    error_log /home/django/logs/rollservice/nginx.error.log;

    location /static/ 
        alias   /home/django/projects/rollservice/static/;
    

    location / 
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        if (!-f $request_filename) 
            proxy_pass http://rollservice-rostov.ru;
            break;
        
    

    error_page 500 502 503 504 /500.html;
    location = /500.html 
        root /home/django/projects/rollservice/templates/;
    

/etc/supervisor/conf.d/rollservice.conf:

[program:rollservice]
command=/home/django/projects/rollservice/run/run.sh
directory=/home/django/projects/rollservice
user=django
autostart=true
autorestart=true
stderr_logfile=/home/django/logs/rollservice/gunicorn_errors.log
stdout_logfile=/home/django/logs/rollservice/gunicorn_access.log

/home/django/projects/rollservice/run/run.sh:

#!/bin/bash

NAME="rollservice"
SRCDIR=rollservice
HOMEDIR=/home/django

PROJECTDIR=$HOMEDIR/projects/$NAME
VIRTUALENV=$HOMEDIR/.envs/$NAME
SOCKFILE=$HOMEDIR/tmp/$NAME.sock

USER=django
GROUP=django
NUM_WORKERS=3
DJANGO_SETTINGS=rollservice.settings
DJANGO_WSGI_MODULE=rollservice.wsgi

echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $PROJECTDIR
source $VIRTUALENV/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS
export PYTHONPATH=$PROJECTDIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec $VIRTUALENV/bin/gunicorn $DJANGO_WSGI_MODULE:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --bind=127.0.0.1:8010 \
  --log-level=debug \
  --log-file=$HOMEDIR/logs/

虽然,我的页面通过

成功打开
lynx http://127.0.0.1:8010

这是最后的错误日志:

 rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:18:34 [error] 11425#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:29:02 [error] 11423#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
2014/10/12 23:29:02 [error] 11423#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /modules/404.php HTTP/1.0", upstream: "http://185.20.226.216:81/modules/404.php", host: "rollservice-rostov.ru"
2014/10/12 23:33:10 [error] 11905#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:33:12 [error] 11905#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 141.105.68.10, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
[vps]rollservice> sudo tail /var/log/nginx/error.log
2014/10/12 23:13:27 [error] 11106#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:16:46 [error] 11295#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:18:34 [error] 11425#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:29:02 [error] 11423#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
2014/10/12 23:29:02 [error] 11423#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 67.148.203.206, server: rollservice-rostov.ru, request: "GET /modules/404.php HTTP/1.0", upstream: "http://185.20.226.216:81/modules/404.php", host: "rollservice-rostov.ru"
2014/10/12 23:33:10 [error] 11905#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 185.20.226.216, server: rollservice-rostov.ru, request: "GET / HTTP/1.0", upstream: "http://185.20.226.216:81/", host: "rollservice-rostov.ru"
2014/10/12 23:33:12 [error] 11905#0: *3 connect() failed (111: Connection refused) while connecting to upstream, client: 141.105.68.10, server: rollservice-rostov.ru, request: "GET /ccs.php?input HTTP/1.0", upstream: "http://185.20.226.216:81/ccs.php?input", host: "rollservice-rostov.ru"
2014/10/12 23:35:22 [error] 11905#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET / HTTP/1.1", upstream: "http://185.20.226.216:81/", host: "185.20.226.216"
2014/10/12 23:35:22 [error] 11905#0: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET /favicon.ico HTTP/1.1", upstream: "http://185.20.226.216:81/favicon.ico", host: "185.20.226.216"
2014/10/12 23:35:22 [error] 11905#0: *9 connect() failed (111: Connection refused) while connecting to upstream, client: 93.72.37.129, server: rollservice-rostov.ru, request: "GET /favicon.ico HTTP/1.1", upstream: "http://185.20.226.216:81/favicon.ico", host: "185.20.226.216"

【问题讨论】:

尝试在 nginx conf 中添加 proxy_pass http://127.0.0.1:8001;location / 【参考方案1】:

问题似乎在于 proxy_pass 由于上游的命名而传递给 FQDN。

if (!-f $request_filename) 
    proxy_pass http://rollservice-rostov.ru;
    break;

它直接传递给那个。改一下upstream的名字应该没问题。

【讨论】:

【参考方案2】:

我认为它与 Django 配置相关: 如果您已将 DEBUG 设置为 False ,则使用 ALLOWED_HOSTS

允许任何域名

ALLOWED_HOSTS = ['*',]

只允许您的域名

ALLOWED_HOSTS = ['rollservice-rostov.ru',]

配置文件 settings.py 可能看起来:

...
DEBUG = False
ALLOWED_HOSTS = ['rollservice-rostov.ru',]
...

要包含子域,请添加“.”开始时

ALLOWED_HOSTS = ['.rollservice-rostov.ru',]

此外,在 Django 1.7 及更高版本中

ALLOWED_HOSTS = [
   '.rollservice-rostov.ru',  # Allow domain and subdomains
   '.rollservice-rostov.ru.',  # Also allow FQDN and subdomains
]

查看 Django 文档 https://docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts

【讨论】:

ALLOWED_HOSTS未配置时,响应不是502。当从nginx向django发送请求时出现问题时会给出502错误,例如django进程不存在或未正确配置为与 nginx“对话” 我在生产中使用 uwsgi,但作者的问题是关于 gunicorn :) 对不起,我知道,你能粘贴日志文件的内容吗,你检查了吗? 感谢您的提示,不幸的是错误还没有消失。你能建议我如何找到 nginx 错误日志吗? error_log /home/django/logs/rollservice/nginx.error.log 为空 尝试按照本教程进行操作。里面描述了你需要的主要配置:digitalocean.com/community/tutorials/…问题是因为nginx出了问题,你需要监听端口或套接字。

以上是关于nginx + gunicorn 502 网关错误的主要内容,如果未能解决你的问题,请参考以下文章

NGINX 502 坏网关 gunicorn 超时

Django + nginx + gunicorn 给出 502 错误。日志信息很少[关闭]

502 错误 django + nginx + gunicorn - connect() recv()

django、gunicorn 和 Nginx 的 502 错误

使用 nginx+gunicorn+django 的间歇性 502s

Nginx 声明“502 错误网关”