UPDATE: It may be closed, remember to always check code copied from outside... The problem was with quote marks in Daphne Service
I want to deploy my Django app (Rest API + React on frontend) on AWS. I use nginx, gunicorn (for http handling) and daphne (for async websockets - Im using django channels for chat application). I was following THIS tutorial.
It looks like I configured well nginx and gunicorn (page is normally loading, I can handle sync request to rest API) but I guess there are some problems with daphne and/or asgi. I use systemctl services for server. All 3 (nginx, gunicorn, daphne) statuses show 'active'. Everything works fine on my development server on local.
On deployment server when I enter website, I see in console
Firefox can’t establish a connection to the server at ws://PATH_TO_WEBSOCKET
Is Daphne connected well with Nginx?
I use Redis for CHANNEL_LAYERS. I have installed channels-redis and redis-server on Ubuntu server. I think it works fine as I get respond
> redis-cli
> ping
**PONG**
project tree
.
├── 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()
settings.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)],
},
},
}
daphne service worker
[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 config file at /etc/nginx/sites-available/ (our websockets endpoints start with /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;
}
}