Bad Request (400) and 502 error: Nginx, gunicorn, django

Bad Request (400) and 502 error: Nginx, gunicorn, django

我正在尝试使用 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 [error] 18650#0: *494 connect() to unix:/tmp/mysitename.socket failed (111: Connection refused) while connecting to upstream

我的settings.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/;
    }

}
~                                                                                                                               
~ 

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

此外,您确定用户 cmac 拥有您 directory/files 的权限吗?通常它在 www-data.

上运行

希望这对您有所帮助。