无法使用gunicorn和nginx在本地部署Django

时间:2017-07-05 19:26:15

标签: python django nginx gunicorn

我正在尝试部署我的第一个django网络应用程序。我在Web服务器上的知识接近0.在配置完所有内容后,我在尝试访问我的页面时得到502 Bad Gateway

Gunicorn命令

sudo gunicorn website.wsgi --log-level debug -u root -group root。这允许我在localhost:8000上访问我的应用,但媒体和静态无效。

Nginx配置:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    server{
        listen 80;
        server_name localhost.kevin.lo;
        charset utf-8;

        access_log /var/log/nginx/oktomus.access.log;
        error_log /var/log/nginx/oktomus.error.log;

        location /media {
            alias /home/oktomus/workspace/code/web/website-prod/media;
        }

        location /static {
            alias /home/oktomus/workspace/code/web/website-prod/static;
        }

        location / {
            include fastcgi_params;
            proxy_set_header X-Forwarded-Host $server_name;
            proxy_set_header X-Real-IP $remote_addr;
            fastcgi_pass localhost:8000;

        }

    }
}

Nginx错误

当我尝试访问端口80上的应用程序时的错误日志。

2017/07/05 21:06:20 [error] 9767#9767: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost.kevin.lo, request: "GET / HTTP/1.1", upstream: "fastcgi://[::1]:8000", host: "localhost"
2017/07/05 21:07:00 [error] 9767#9767: *7 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost.kevin.lo, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:8000", host: "localhost"
2017/07/05 21:07:31 [error] 9767#9767: *7 upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost.kevin.lo, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:8000", host: "localhost"
2017/07/05 21:07:31 [error] 9767#9767: *7 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost.kevin.lo, request: "GET / HTTP/1.1", upstream: "fastcgi://[::1]:8000", host: "localhost"

Gunicorn输出

当我尝试访问应用程序然后在几秒钟后停止浏览器时,这是输出。

[10024] [DEBUG] Ignored premature client disconnection. No more data after: b'\x01\x01\x00\x01\x00\x08\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x01\x04\x00\x01\x03\x06\x02\x00\x0c\x00QUERY_STRING\x0e\x03REQUEST_METHODGET\x0c\x00CONTENT_TYPE\x0e\x00CONTENT_LENGTH\x0b\x01SCRIPT_NAME/\x0b\x01REQUEST_URI/\x0c\x01DOCUMENT_URI/\r\x0fDOCUMENT_ROOT/etc/nginx/html\x0f\x08SERVER_PROTOCOLHTTP/1.1\x0e\x04REQUEST_SCHEMEhttp\x11\x07GATEWAY_INTERFACECGI/1.1\x0f\x0cSERVER_SOFTWAREnginx/1.12.0\x0b\tREMOTE_ADDR127.0.0.1\x0b\x05REMOTE_PORT37886\x0b\tSERVER_ADDR127.0.0.1\x0b\x02SERVER_PORT80\x0b\x12SERVER_NAMElocalhost.kevin.lo\x0f\x03REDIRECT_STATUS200\t\tHTTP_HOSTlocalhost\x0fDHTTP_USER_AGENTMozilla/5.0 (X11; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0\x0b?HTTP_ACCEPTtext/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\x14\x0eHTTP_ACCEPT_LANGUAGEen-US,en;q=0.5\x14\rHTTP_ACCEPT_ENCODINGgzip, deflate\x0bJHTTP_COOKIEcsrftoken=bOeYkxXdfepIfgwiNnNMeT21NznPM3y1fAR07wp8MyCJtAAbrXdiVfHZxbmuSx0l\x08\x01HTTP_DNT1\x0f\nHTTP_CONNECTIONkeep-alive\x1e\x01HTTP_UPGRADE_INSECURE_REQUESTS1\x00\x00\x01\x04\x00\x01\x00\x00\x00\x00\x01\x05\x00\x01\x00\x00\x00\x00'

如果我不停止浏览器,我会登陆502 nginx Bad Gateway,并且gunicorn输出此信息。

[2017-07-05 21:29:04 +0200] [10135] [CRITICAL] WORKER TIMEOUT (pid:10146)

我已经阅读了nginx beginer的指南,这有助于我了解一些事情,但我仍然不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

我只是按照太老的教程。我引用了gunicorn文档,它完美无缺。