【问题标题】:Bad Request (400) and 502 error: Nginx, gunicorn, django错误请求 (400) 和 502 错误:Nginx、gunicorn、django
【发布时间】:2015-06-17 09:28:01
【问题描述】:

我正在尝试使用 nginx、gunicorn 和 django 部署我的网站。

当我首先运行 gunicorn 并加载页面时,我收到 502 Bad gateway 错误,然后我将服务器名称切换为我服务器的 IP 地址,现在我收到 Bad Request 400 错误或域无法成为成立。

我一直在遵循Test Driven Development 的这些步骤。

我昨晚意识到我正在使用我的临时服务器来更新我的实时域而不是临时域。所以我创建了一个暂存域作为活动域的子域,并为它创建了一个单独的目录,然后 git 拉下了我之前所做的工作,但它不起作用。

我的 nginx 配置文件:

 server {
     listen 80;
     server_name my-server-ip-address;

     location / {
          proxy_set_header Host $host;
          proxy_pass http://unix:/tmp/mysitename.socket;
     }

      location /static {
         autoindex on;
         root /home/cmac/sites/mysitename/;
     }

 }

Nginx 错误日志:

2015/04/11 18:59:16 [错误] 18650#0: *494 connect() 到 unix:/tmp/mysitename.socket 失败(111:连接被拒绝)而 连接上游

我的设置.py:

 DEBUG = False

 TEMPLATE_DEBUG = DEBUG

 ALLOWED_HOSTS = [mysitename]

当我运行 gunicorn 时:

 [2015-04-11 20:40:39 +0000] [4174] [INFO] Starting gunicorn 19.3.0
 [2015-04-11 20:40:39 +0000] [4174] [INFO] Listening at:      http://127.0.0.1:8000 (4174)
 [2015-04-11 20:40:39 +0000] [4174] [INFO] Using worker: sync
 [2015-04-11 20:40:39 +0000] [4177] [INFO] Booting worker with pid: 4177

在我决定切换域之前一切正常。

编辑整个 nginx.conf 文件

user  cmac;
worker_processes  1;


error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    include       /etc/nginx/sites-enabled/mysitename;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        #keepalive_timeout  0;
        keepalive_timeout  65;

        #gzip  on;

        # Load modular configuration files from the /etc/nginx/conf.d directory.
        # See http://nginx.org/en/docs/ngx_core_module.html#include
        # for more information.
        include /etc/nginx/conf.d/*.conf;

        index   index.html index.htm;

        server {
            listen       80;
            server_name  localhost;
            root         /usr/share/nginx/html;

            #charset koi8-r;

            #access_log  /var/log/nginx/host.access.log  main;

location / {
        }

        # redirect server error pages to the static page /40x.html
        #
        error_page  404              /404.html;
        location = /40x.html {
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    root         html;


 #    location / {
#    }
#}


# HTTPS server
#
#server {
#    listen       443;
#    server_name  localhost;
#    root         html;

#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers   on;

#    location / {
#    }
#}

包含文件(来自 /etc/nginx/sites-enabled/mysitename):

server {
    listen 127.0.0.1;
    server_name my-server-ip-address;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://unix:/tmp/mysitename.socket;

    }

     location /static {
        autoindex on;
        root /home/cmac/sites/mysitename/;
    }

}
~                                                                                                                               
~ 

【问题讨论】:

  • 尝试将unix://... 更改为您的本地主机和端口,如您的gunicorn 服务的127.0.0.1:8000,看看它是如何进行的
  • @Anzel 仍然收到错误请求 400 并且 nginx 日志中没有任何内容
  • 你确定你的 gunicorn 和 django 运行正常吗?
  • @Anzel 我想我知道。我对此很陌生。当我运行独角兽时,我得到: [2015-04-11 20:40:39 +0000] [4174] [INFO] 开始 gunicorn 19.3.0 [2015-04-11 20:40:39 +0000] [4174] [ INFO] 收听:127.0.0.1:8000 (4174) [2015-04-11 20:40:39 +0000] [4174] [INFO] 使用工作人员:同步 [2015-04-11 20:40:39 +0000] [ 4177] [INFO] 使用 pid 引导工作人员:4177
  • 检查 server_name 设置...您确定您已将所有其他设置更改为相同,例如 www.example.com 但不是 staging.example.com

标签: python django nginx gunicorn


【解决方案1】:

mysitename 中,您需要监听端口80,并将server_name 作为您的暂存域,如staging.example.com,目前也不要使用unix sock,将http://127.0.0.1:8000 放入proxy_passgunicorn 服务的地方。也可以尝试在您的nginx.conf 中注释掉服务器块,它与您的mysitename 有冲突。

另外,您确定用户cmac 在您的目录/文件下拥有权限吗?通常它在www-data 上运行。

希望这会有所帮助。

【讨论】:

  • @drakoen,您可以尝试将 tcp/ip 替换为套接字,看看它是否有效。我要求您更换它以确保减少 1 个错误。一般来说,套接字比 tcp/ip 运行得更快,但没有那么可扩展。我认为这在您的用例中并不重要,而且 tcp/ip 也更直接:)
猜你喜欢
  • 1970-01-01
  • 2014-02-19
  • 2017-10-01
  • 1970-01-01
  • 2012-05-28
  • 2017-05-17
  • 2018-12-05
  • 2015-03-31
  • 2016-06-23
相关资源
最近更新 更多