与Daphne的Nginx给出了502 Bad Gateway

问题描述 投票:2回答:1

我决定用daphne取代uwsgi,因为我遇到了Django频道和uwsgi的问题。之后关注this教程。我在启用网站的过程中以这种方式配置了我的nginx。我遇到的大多数其他例子都没有使用达芙妮,所以我无法与它们联系起来。

 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
django nginx django-channels daphne
1个回答
0
投票

daphne落后于nginx。因此,您需要将其添加为nginx conf的上游。

假设daphne在801上运行端口8001和django应用程序

upstream channels-backend {
    server 0.0.0.0:8001;
}

并更新为

proxy_pass http://channels-backend;
© www.soinside.com 2019 - 2024. All rights reserved.