Nginx和Daphne提供502 Bad Gateway

时间:2019-03-23 07:51:41

标签: django nginx django-channels daphne

由于Django频道和uwsgi出现问题,我决定用daphne替换uwsgi。在遵循this教程之后。我在启用站点的方式中以这种方式配置了Nginx。我遇到的其他大多数示例都没有使用daphne,因此我无法与它们建立联系。

 server {
        # the port your site will be served on
        listen      80;
        server_name .MyDomain.com;
        charset     utf-8;

        # max upload size
        client_max_body_size 75M;   # adjust to taste

        # Django media
        location /media  {
            # your Django project's media files - amend as required
            alias /home/ec2-user/MyDomainVenv/MyDomainWeb/media;
        }

        location /static {
            # your Django project's static files - amend as required
            alias /home/ec2-user/MyDomainVenv/MyDomainWeb/static;
        }


            location / {
                proxy_pass http://0.0.0.0:8001;
                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";

                proxy_redirect     off;
                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-Host $server_name;
            }
    }

我以这种方式启动了达芙妮

daphne main.asgi:channel_layer

和使用的工作线程

python manage.py runworker

这是我的asgi.py

import os
from channels.asgi import get_channel_layer

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "main.settings")

channel_layer = get_channel_layer()

关于可能出什么问题的任何建议?

我尝试访问我的网站,这就是我所得到的

==> /var/log/nginx/error.log <==
2019/03/23 07:13:21 [error] 22191#0: *4 connect() failed (111: Connection refused) while connecting to upstream, client: 71.231.182.18, server: MyDomain.com, request: "GET /admin/ HTTP/1.1", upstream: "http://0.0.0.0:8001/admin/", host: "www.MyDomain.com"

==> /var/log/nginx/access.log <==
71.231.182.18 - - [23/Mar/2019:07:13:21 +0000] "GET /admin/ HTTP/1.1" 502 575 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"

这是我开始使用达芙妮后得到的

daphne main.asgi:channel_layer
Starting server at tcp:port=8000:interface=127.0.0.1, channel layer main.asgi:channel_layer.
HTTP/2 support not enabled (install the http2 and tls Twisted extras)
Using busy-loop synchronous mode on channel layer
Listening on endpoint tcp:port=8000:interface=127.0.0.1

1 个答案:

答案 0 :(得分:0)

daphnenginx之后运行。因此,您需要将其作为上游添加到您的nginx conf中。

假设daphne在端口8001上运行,而django应用程序在80上运行

upstream channels-backend {
    server 0.0.0.0:8001;
}

并更新为

proxy_pass http://channels-backend;