Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
502坏网关django+;gunicorn&x2B;nginx配置_Django_Nginx_Gunicorn - Fatal编程技术网

502坏网关django+;gunicorn&x2B;nginx配置

502坏网关django+;gunicorn&x2B;nginx配置,django,nginx,gunicorn,Django,Nginx,Gunicorn,当我试图访问我的网站时,我得到了一个502坏网关。因此,我查看了我的日志,发现当我运行gunicorn脚本时,会收到以下错误消息: 2016/01/14 19:52:22 [error] 25232#0: *1 connect() to unix:/home/dvotedfan/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 130.211.0.242, server

当我试图访问我的网站时,我得到了一个502坏网关。因此,我查看了我的日志,发现当我运行gunicorn脚本时,会收到以下错误消息:

2016/01/14 19:52:22 [error] 25232#0: *1 connect() to unix:/home/dvotedfan/run/gunicorn.sock failed (111: Connection refused) while connecting to upstream, client: 130.211.0.242, server: dvotedfan.com, request: "GET / HTTP/1.1", upstream: "http://unix:/home/dvotedfan/run/gunicorn.sock:/", host: "10.240.0.2"
我的nginx配置文件:

    upstream dvotedfan_app_server {
  server unix:/home/dvotedfan/run/gunicorn.sock fail_timeout=0;
}
server {
    listen 80;
    server_name www.dvotedfan.com dvotedfan.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/dvotedfan/static;
    }
    location /media/ {
        root /home/dvotedfan/media;
    }
    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://dvotedfan_app_server;
            break;
        }
       }
}
我的gunicorn脚本如下所示:

    #!/bin/bash
NAME="dvotedfan"
DJANGODIR=/home/dvotedfan/src
SOCKFILE=/home/dvotedfan/run/gunicorn.sock
USER=dvotedfan

NUM_WORKERS=3

MAX_REQUESTS=1
DJANGO_SETTINGS_MODULE=dvotedfan.settings.production
DJANGO_WSGI_MODULE=dvotedfan.wsgi # WSGI module name

echo "Starting $NAME"

cd $DJANGODIR
source /home/dvotedfan/.virtualenvs/dvotedfan/bin/activate

export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

exec /home/dvotedfan/.virtualenvs/dvotedfan/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--user=$USER \
--workers $NUM_WORKERS \
--max-requests $MAX_REQUESTS \
--bind=unix:/home/dvotedfan/run/gunicorn.sock \
--log-level=error \
--log-file=-
如果我运行ps-ef | grep gunicorn

dvotedf+ 26820 24452  0 21:11 pts/0    00:00:00 nano /home/dvotedfan/scripts/gunicorn.sh
dvotedf+ 26821 24452  0 21:11 pts/0    00:00:00 nano /home/dvotedfan/scripts/gunicorn.sh
dvotedf+ 27003 24452  0 21:16 pts/0    00:00:00 /home/dvotedfan/.virtualenvs/dvotedfan/bin/python /home/dvotedfan/.
virtualenvs/dvotedfan/bin/gunicorn --bind 0.0.0.0:8000 dvotedfan.wsgi:application
dvotedf+ 27008 27003  0 21:16 pts/0    00:00:00 /home/dvotedfan/.virtualenvs/dvotedfan/bin/python /home/dvotedfan/.
virtualenvs/dvotedfan/bin/gunicorn --bind 0.0.0.0:8000 dvotedfan.wsgi:application
dvotedf+ 27199 27123  0 21:20 pts/2    00:00:00 grep --color=auto gunicorn

我没有主意了,我到处找了,找不到解决方案。

您的gunicorn正在侦听端口8000,因此您需要连接到该端口。 你的nginx配置应该是这样的

http {
  upstream django {
      server 127.0.0.1:8000;
  }

  server {
      listen 80;
      ...
      location / {
        uwsgi_pass django;
        include uwsgi_params;
      }
   }
}

:ps-ef | grep gunicorn?的输出是什么?与IP一起工作,但我想知道它为什么与套接字不工作。因为您告诉gunicorn在端口8000上侦听接受来自任何IP的连接
--bind 0.0.0:8000
,顺便说一句,这不是很安全。您应该执行
——绑定127.0.0.1:8000
。要使其侦听本地套接字,您应该执行
--bind unix:/path/To/socket.sock