The chat feature works fine on Development, however, when deployed to production, everything on the web app works but the chat. I get the following error
WebSocket connection to 'wss://app.myapp.com/ws/chat/c580d91acbcc4f4fb699e1b6db69249f/' failed: Error during WebSocket handshake: Unexpected response code: 500
I’m using Daphne with Apache Server
This is my Asgi file content
import os
import django
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')
django.setup()
application = get_asgi_application()
This is my Daphne.service file content
[Unit]
Description=daphne daemon for myapp chat
After=network.target
[Service]
Type=simple
User=root
WorkingDirectory=/home/myusername/myapp/
ExecStart=/home/myusername/virtualenv/myapp/bin/python3.9 /home/myusername/virtualenv/myapp/bin/daphne -b 0.0.0.0 -p 8001 myproject.asgi:application
Restart=on-failure
[Install]
WantedBy=multi-user.target
Daphne is running
Dec 12 15:46:44 server1.mydomain.com python3.9[744788]: 2023-12-12 16:46:44,459 INFO Listening on TCP address 0.0.0.0:8001
[root@server1 ~]# systemctl status daphne.service
● daphne.service - daphne daemon for myapp chat
Loaded: loaded (/etc/systemd/system/daphne.service; disabled; vendor preset: disabled)
Active: active (running) since Tue 2023-12-12 15:46:43 UTC; 6s ago
Main PID: 744788 (python3.9)
Tasks: 1 (limit: 12317)
Memory: 45.7M
CGroup: /system.slice/daphne.service
└─744788 /home/myusername/virtualenv/myapp/bin/python3.9 /home/myusername/virtualenv/myapp/bin/daphne -b 0.0.0.0 -p 8001 myproject.asgi:application
Dec 12 15:46:43 server1.mydomain.com systemd[1]: daphne.service: Succeeded.
Dec 12 15:46:43 server1.mydomain.com systemd[1]: Stopped daphne daemon for myapp chat.
Dec 12 15:46:43 server1.mydomain.com systemd[1]: Started daphne daemon for myapp chat.
Dec 12 15:46:44 server1.mydomain.com python3.9[744788]: 2023-12-12 16:46:44,458 INFO Starting server at tcp:port=8001:interface=0.0.0.0
Dec 12 15:46:44 server1.mydomain.com python3.9[744788]: 2023-12-12 16:46:44,458 INFO HTTP/2 support enabled
Dec 12 15:46:44 server1.mydomain.com python3.9[744788]: 2023-12-12 16:46:44,459 INFO Configuring endpoint tcp:port=8001:interface=0.0.0.0
Dec 12 15:46:44 server1.mydomain.com python3.9[744788]: 2023-12-12 16:46:44,459 INFO Listening on TCP address 0.0.0.0:8001
Same with Redis.
This is my apache config
<VirtualHost 10.10.11.11:80>
ServerName app.myapp.com
ServerAlias www.app.myapp.com mail.app.myapp.com
Redirect permanent / https://app.myapp.com/
</VirtualHost>
LoadModule wsgi_module /home/myusername/virtualenv/myapp/lib64/python3.9/site-packages/mod_wsgi/server/mod_wsgi-py39.cpython-39-x86_64-linux-gnu.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
<VirtualHost 10.10.11.11:443>
ServerName app.myapp.com
ServerAlias www.app.myapp.com mail.app.myapp.com
DocumentRoot /home/myusername/myapp
ErrorLog /home/myusername/myapp/error.log
CustomLog /home/myusername/myapp/access.log combine
<Directory /home/myusername/myapp/myproject>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
Alias /static /home/myusername/app.myapp.com/myapp/static
<Directory /home/myusername/app.myapp.com/myapp/static>
Require all granted
</Directory>
WSGIDaemonProcess myapp python-home=/home/myusername/virtualenv/myapp python-path=/home/myusername/myapp:/home/myusername/virtualenv/myapp/lib64/python3.9/site-packages
WSGIProcessGroup myapp
WSGIScriptAlias / /home/myusername/myapp/myproject/wsgi.py
WSGIApplicationGroup %{GLOBAL}
# WebSocket Proxy
#ProxyPass /ws/ ws://app.myapp.com:8001/
#ProxyPassReverse /ws/ ws://app.myapp.com:8001/
#ProxyPass / http://0.0.0.0:8001/
#ProxyPassReverse / http://0.0.0.0:8001/
#ProxyPreserveHost On
#ProxyRequests Off
#ProxyPassMatch ^/(ws(/.*)?)$ ws://0.0.0.0:8001/$1
ProxyPass /ws/ ws://127.0.0.1:8001/
ProxyPassReverse /ws/ ws://127.0.0.1:8001/
ProxyPreserveHost On
ProxyVia On
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://0.0.0.0:8001%{REQUEST_URI} [P,QSA,L]
SSLEngine on
SSLCertificateFile /var/webuzo/users/myusername/ssl/app.myapp.com-combined.pem
</VirtualHost>
Javascript code
<script>
const chatMessages = document.querySelector('#chat-messages');
const scrollToBottomBtn = document.getElementById('scrollToBottomBtn');
const roomName = JSON.parse(document.getElementById('room-name').textContent);
// Correctly decide between ws:// and wss://
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
{% if debug_mode %}
var ws_path = ws_scheme + '://' + window.location.host + '/ws/chat/' + roomName + "/"; // development
{% else %}
var ws_path = ws_scheme + '://' + window.location.host + '/ws/chat/' + roomName + "/"; // production
{% endif %}
const chatSocket = new WebSocket(ws_path);
// the rest of the working code here
</script>
P.S: I’m using Namecheap VPS hosting.
I’d appreciate if anyone can help me figure what the problem is.