达芙妮的Django部署

达芙妮的Django部署,django,nginx,redis,daphne,Django,Nginx,Redis,Daphne,更新: 它可能已关闭,请记住始终检查从外部复制的代码。。。问题在于Daphne服务中的引号 我想在AWS上部署我的Django应用程序(RESTAPI+React on frontend)。我使用nginx、gunicorn(用于http处理)和daphne(用于异步websockets-Im,使用django频道进行聊天应用程序)。我在学习教程 看起来我很好地配置了nginx和gunicorn(页面通常正在加载,我可以处理到restapi的同步请求),但我猜daphne和/或asgi存在一些问

更新: 它可能已关闭,请记住始终检查从外部复制的代码。。。问题在于Daphne服务中的引号

我想在AWS上部署我的Django应用程序(RESTAPI+React on frontend)。我使用nginx、gunicorn(用于http处理)和daphne(用于异步websockets-Im,使用django频道进行聊天应用程序)。我在学习教程

看起来我很好地配置了nginx和gunicorn(页面通常正在加载,我可以处理到restapi的同步请求),但我猜daphne和/或asgi存在一些问题。我对服务器使用systemctl服务。所有3种(nginx、gunicorn、daphne)状态均显示“活动”。在我的本地开发服务器上一切正常

在部署服务器上,当我进入网站时,我在控制台中看到

Firefox can’t establish a connection to the server at ws://PATH_TO_WEBSOCKET
达芙妮和Nginx的关系好吗

我将Redis用于通道层。我在Ubuntu服务器上安装了通道redis和redis server。当我得到答复时,我认为它工作得很好

> redis-cli
> ping
**PONG**
项目树

.
├── frontend #react files
├── checkers #project dir
│   ├── settings.py
│   ├── urls.py
│   └── asgi.py
├── chat #chat app
│   ├── consumers.py
│   └── routing.py
└── checkers.sock
asgi.py

import os
import django
from channels.routing import get_default_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'checkers.settings')
django.setup()
application = get_default_application()
设置.py


ROOT_URLCONF = 'checkers.urls'
ASGI_APPLICATION = "checkers.routing.application"

CHANNEL_LAYERS = {
    'default': {
        'BACKEND': 'channels_redis.core.RedisChannelLayer',
        'CONFIG': {
            "hosts": [('127.0.0.1', 6379)],
        },
    },
}
达芙妮服务工人

[Unit]
Description=My Daphne Service
After=network.target
[Service]
Type=simple 
User=ams
Group=www-data
WorkingDirectory=/home/ams/production_app  
ExecStart=/home/ams/production_app/production_env/bin/daphne --access-log /home/ams/production_app/log/daphne-access.log -b 0.0.0.0 -p 9001 checkers.asgi:application
[Install]
WantedBy=multi-user.target
Nginx配置文件位于/etc/Nginx/sites available/(我们的websockets端点以/ws/开头)

upstream channels-backend {
    server 0.0.0.0:9001;
}

server {
    listen 80;
    server_name MY_SERVER;
    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/ams/production_app;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/ams/production_app/checkers.sock;
    }

    location /ws/ {
        proxy_pass http://channels-backend;
        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;
    }

}