Nginx with Daphne gives 502 Bad Gateway

此生再无相见时 提交于 2019-12-11 00:47:19

问题


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

and a worker thread using

python manage.py runworker

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

回答1:


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

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

upstream channels-backend {
    server 0.0.0.0:8001;
}

and update as

proxy_pass http://channels-backend;


来源:https://stackoverflow.com/questions/55311753/nginx-with-daphne-gives-502-bad-gateway

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!