与nginx-502 Bad Gateway的Django站点错误

时间:2016-08-10 12:37:16

标签: django nginx

我试图将Django网站设置如下:

python manage.py runserver 127.0.0.1:1234
...
August 10, 2016 - 12:10:50
Django version 1.9, using settings 'mysite_config_api.settings'
Starting development server at http://127.0.0.1:1234/
Quit the server with CONTROL-C.

当我尝试通过 mysite.com/admin 访问该网站时,我得到了:

[10/Aug/2016 12:16:58] code 400, message Bad request syntax ('\x01\x01\x00\x01\x09\x00\x01\x00\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\t\x0cPATH_INFO/favicon.ico\x0e\x03REQUEST_METHODGET\x0c\x00QUERY_STRING\x0c\x00CONTENT_TYPE\x0e\x00CONTENT_LENGTH\x0b\x0cSERVER_ADDR172.30.1.244\x0b\x02SERVER_PORT80\x0b\x16SERVER_NAMEmysite.com\x0f\x08SERVER_PROTOCOLHTTP/1.1\t\x16HTTP_HOSTmysite.com\x0f')
[10/Aug/2016 12:16:58] "
                    PATH_INFO/favicon.icoREQUEST_METHODGET
                                                          QUERY_STRING
                                                                      CONTENT_TYPECONTENT_LENGTH

                                                                                                SERVER_ADDR172↓3????244
                                                                                                                       SERVER_PORT8?
                                                                                                                                    SERVER_NAMEde┴↓?e????±?↓c??┬d│↓cSERVER_PROTOCOLHTTP/1.1    HTTP_HOSTmysite.com" 400 -

我的nginx配置:

server {
    listen 80;

    server_name mysite.com;

    location /static {
            alias /home/ubuntu/mysite/mysite_api/staticfiles;
    }

    location / {
            fastcgi_pass 127.0.0.1:1234;
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param QUERY_STRING $query_string;
            fastcgi_param CONTENT_TYPE $content_type;
            fastcgi_param CONTENT_LENGTH $content_length;
            fastcgi_pass_header Authorization;
            fastcgi_intercept_errors on;
            fastcgi_param SERVER_ADDR $server_addr;
            fastcgi_param SERVER_PORT $server_port;
            fastcgi_param SERVER_NAME $server_name;
            fastcgi_param SERVER_PROTOCOL $server_protocol;
    }
}

发生了什么事?我试图更改端口(现在是6000,现在是1234)并检查没有其他应用程序正在运行。有什么问题?

3 个答案:

答案 0 :(得分:1)

runserver不是fcgi ...使用nginx背后的开发服务器没什么意义,但如果你想这样做,你必须使用nginx proxy_pass而不是fastcgi_pass

location / {
    proxy_pass http://127.0.0.1:1234;
    proxy_set_header Host $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;
}

答案 1 :(得分:1)

尝试使用以下配置。服务器可以保留端口80(在我的情况下,这发生在谷歌虚拟机上)。如果它没有被使用(您已经检查过),那么您可以简单地用44替换443.

server {
  listen 443;
  server_name yourserver.com;

location / {
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-NginX-Proxy true;
                proxy_pass http://localhost:1234/;
                proxy_ssl_session_reuse off;
                proxy_set_header Host $http_host;
                proxy_cache_bypass $http_upgrade;
                proxy_redirect off;
}

答案 2 :(得分:0)

我使用以下nginx配置(取自that post)解决了这个问题:

server {
listen 80;
server_name mysite.com;

location /static {
    alias /home/ubuntu/path/to/staticfiles;

location / {
    proxy_pass http://127.0.0.1:1234;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}