I am trying to deploy this django app which uses channels. I use Apache for regular HTTP requests and want to forward the web socket requests to Daphne.
Here are some of my important files:
apache config:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://127.0.0.1:8001%{REQUEST_URI} [P,QSA,L]
Alias /static /home/anna/tchan/static
<Directory /home/anna/tchan/static>
Require all granted
</Directory>
Alias /media /home/anna/tchan/media
<Directory /home/anna/tchan/media>
Require all granted
</Directory>
<Directory /home/anna/tchan/tchan>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
WSGIScriptAlias / /home/anna/tchan/tchan/wsgi.py
WSGIDaemonProcess django_app python-path=/home/anna/tchan python-home=/home/anna/tchan/venv
WSGIProcessGroup django_app
</VirtualHost>
Last few lines of settings.py :
ASGI_APPLICATION = 'tchan.routing.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 8001)],
},
},
}
asgi.py
import os
import django
from channels.routing import get_default_application
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tchan.settings')
django.setup()
application = get_default_application()
With these in place, I run daphne -p 8001 tchan.asgi:application
and then sudo service apache2 reload
. Finally, when testing the websocket in my page here's what happens:
websocket.js:4 WebSocket connection to 'ws://192.168.0.57/ws/chat/8/' failed: Error during WebSocket handshake: Unexpected response code: 403
This error 403 happens whether or not I'm running daphne.
What am I doing wrong here? Note: the app works as expected when using Django's development server and docker for the channel layer, the problem is with my Apache config, I think.
I figured it out. To enable apache to redirect, we need to:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_wstunnel
One of these was not enabled which caused the 403 Forbidden.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.