本文介绍了Nginx和Daphne提供了502 Bad Gateway的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I decided to replace uwsgi with daphne since I was having issues with Django Channels and uwsgi. After following this tutorial. I configured my nginx this way in sites-enabled. Most of the other examples I have come across are not using daphne so I could not relate to them.

 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;
            }
    }

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

I started Daphne using this way

daphne main.asgi:channel_layer

和使用的工作线程

python manage.py runworker

这是我的asgi.py

This is my asgi.py

import os
from channels.asgi import get_channel_layer

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

channel_layer = get_channel_layer()

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

any suggestions on what might be going wrong ?

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

I tried accessing my website and this is what i got

==> /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" "-"

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

This is what I get when I start daphne

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

推荐答案

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

daphne is running behind nginx. So, you need to add that as an upstream to your nginx conf.

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

Assuming that daphne is running on port 8001 and django application on 80

upstream channels-backend {
    server 0.0.0.0:8001;
}

并更新为

proxy_pass http://channels-backend;

这篇关于Nginx和Daphne提供了502 Bad Gateway的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-29 07:48