I have hosted a django application on a remote server using Linode. I am using nginx, gunicorn and daphne (for websockets).
I have tried multiple things such as allowing traffic on various ports like port 8001. (which was successful)
But no matter what I do, websocket connections on my site have been failing non stop ever since I got my SSL certificate.
The backend code like the actual code after the web socket connection is established works perfectly as it was working in development and also when the site was deployed but did not
have an SSL certificate. (just an IP address)
The following is my nginx config:
server {
listen 443 ssl;
server_name www.aniconnect.org aniconnect.org;
ssl_certificate /etc/letsencrypt/live/www.aniconnect.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.aniconnect.org/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/gamedeveloper/anime_chat/anime_chat_app/static/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
location /ws/ {
proxy_pass http://127.0.0.1:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
}
}
server {
if ($host = www.aniconnect.org) {
return 301 https://$host$request_uri; }
if ($host = aniconnect.org) {
return 301 https://$host$request_uri; }
listen 80;
server_name www.aniconnect.org aniconnect.org 194.195.119.237;
return 404;
}
The following is my gunicorn_config.py:
import multiprocessing
import os
import sys
sys.path.append("/home/gamedeveloper/anime_chat/anime_chat_app")
bind = "unix:/home/gamedeveloper/run/gunicorn.sock"
accesslog = "/home/gamedeveloper/gunicorn/access.log"
errorlog = "/home/gamedeveloper/gunicorn/error.log"
worker_class = "geventwebsocket.gunicorn.workers.GeventWebSocketWorker"
workers = multiprocessing.cpu_count() * 2 + 1
pythonpath = "/home/gamedeveloper/anime_chat"
os.environ["DJANGO_SETTINGS_MODULE"] = "anime_chat_app.settings"
preload_app = True
timeout = 120
The following is my daphne.service:
[Unit]
Description=Daphne ASGI Server for Your Django Project
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/gamedeveloper/anime_chat/anime_chat_app
ExecStart=/home/gamedeveloper/venv/bin/daphne -e ssl:8001:privateKey=/etc/letsencrypt/live/www.aniconnect.org/privkey.pem:certKey=/etc/letsencrypt/live/www.aniconnect.org/fullchain.pem anime_chat_app.asgi:application
Environment="DJANGO_SETTINGS_MODULE=anime_chat_app.settings"
Restart=always
[Install]
WantedBy=multi-user.target
I checked the daphne status and it is also working fine.
I did some debugging and found this in my Nginx error logs:
2023/10/21 21:10:19 [error] 25251#25251: *91 upstream prematurely closed connection while reading response header from upstream, client: 49.36.144.168, server: www.aniconnect.org, request: "GET /ws/chat/AUdZzo8slKKwKvzmUrnmLRfnVkuAYfJj/ HTTP/1.1", upstream: "http://127.0.0.1:8001/ws/chat/AUdZzo8slKKwKvzmUrnmLRfnVkuAYfJj/", host: "www.aniconnect.org"
I really have to get this working so please help me out!