我已经遵循了一些关于设置 django 频道和阅读 asgi 文档的教程,并且在将我的 websocket 请求代理到必要的 daphne 侦听器时遇到了困难。
这是达芙妮显示的内容:
$ $ daphne -v2 -b 127.0.0.1 -p 8443 asgi:application
2018-10-07 03:48:33,533 INFO Starting server at tcp:port=8443:interface=127.0.0.1
2018-10-07 03:48:33,534 INFO HTTP/2 support not enabled (install the http2 and tls Twisted extras)
2018-10-07 03:48:33,534 INFO Configuring endpoint tcp:port=8443:interface=127.0.0.1
(注意:我也尝试过 SSL 版本,使用$ daphne -e ssl:8444:privateKey=privkey.pem:certKey=fullchain.pem asgi:application -b 127.0.0.1 -p 8443
在我的 apache.conf 文件中:
WSGIScriptAlias / home/david/data-py/wsgi.py
WSGIPythonPath /home/david/lib/python3.6/site-packages
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule .* wss://127.0.0.1:8443%{REQUEST_URI} [P,L]
<VirtualHost *:80>
ServerName d.data.com
<Directory /home/david/data-py>
Order allow,deny
Allow from all
Require all granted
</Directory>
RewriteCond %{SERVER_NAME} =d.data.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
最后,在实际的浏览器中,我正在做:
new WebSocket('wss://d.data.com/ws/registration/asdf1')
# it was working on localhost at "wss://localhost:8000/ws/registration/02488"
并且全部启用:
- a2enmod 重写
- a2enmod 代理
- a2enmod proxy_wstunnel
我在上面的配置中遗漏了什么吗?每次我访问 websocket 页面时它只有 404,所以它显然没有“接收”ws 请求。也许其中一些与使用 https 有关?在我能找到的 django-channels 或 asgi 文档中没有任何相关内容。
最后,如果这有任何用处:
david@instance-3:~$ sudo netstat -ntlp | grep LISTEN
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 400/systemd-resolve
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1010/sshd
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 3713/mysqld
tcp6 0 0 :::80 :::* LISTEN 14853/apache2
tcp6 0 0 :::22 :::* LISTEN 1010/sshd
tcp6 0 0 :::443 :::* LISTEN 14853/apache2