操作系统 Ubuntu 20 LTS
我在生产中的项目有问题,我不明白哪里出了问题,出了什么问题,这些是文件和响应:
000-default.conf:
<VirtualHost *:80>
.....
RewriteEngine on
RewriteCond %{SERVER_NAME} =site.addns.org [OR]
RewriteCond %{SERVER_NAME} =www.site.addns.org
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* wss://%{SERVER_NAME}%{REQUEST_URI} [P,QSA,L]
ProxyPass /wss/ wss://127.0.0.1:8000/
ProxyPassReverse /wss/ wss://127.0.0.1:8000/
</VirtualHost>
<VirtualHost *:443>
.....
ProxyPreserveHost On
SSLProxyEngine on
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC,OR]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://127.0.0.1:8000%{REQUEST_URI} [P,QSA,L]
ProxyPass /wss/ wss://127.0.0.1:8000/
ProxyPassReverse /wss/ wss://127.0.0.1:8000/
</VirtualHost>
asgi.py:
import os
from django.urls import re_path, path
from django.core.asgi import get_asgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "produzione.settings")
django_asgi_app = get_asgi_application()
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.routing import get_default_application
from principale import consumers
application = ProtocolTypeRouter({
"http": django_asgi_app,
"websocket": AuthMiddlewareStack(
URLRouter([
path(r'wss/$', consumers.EchoConsumer.as_asgi()),
path(r'ws/chat/$', consumers.EchoConsumer.as_asgi()),
])
),
})
消费者.py:
import json
from channels.generic.websocket import WebsocketConsumer
from channels.consumer import AsyncConsumer
class EchoConsumer(WebsocketConsumer):
def connect(self):
self.accept()
设置.py:
...
ASGI_APPLICATION = "produzione.asgi.application"
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [('127.0.0.1', 8000)],
},
},
}
...
.js 文件:
var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
var chat_socket = new WebSocket(ws_scheme + '://' + window.location.host + '/wss/');
var chatSocket = new WebSocket(
'wss://'
+ '127.0.0.1:8000'
+ '/wss'
);
js回复:
此错误会立即出现在浏览器控制台上
WebSocket connection to 'wss://site.addns.org/wss/' failed: Error during WebSocket handshake: Unexpected response code: 404
此错误会立即出现在 error.log 文件中:
Not Found: /wss/
这在浏览器控制台上几秒钟后
WebSocket connection to 'wss://127.0.0.1:8000/wss' failed: WebSocket opening handshake timed out
达芙妮命令:
sudo daphne -e ssl:8001:privateKey=privkey.pem:certKey=cert.pem produzione.asgi:application -b 127.0.0.1
午饭后达芙妮回应:
2021-02-11 14:03:42,587 INFO Starting server at ssl:8001:privateKey=privkey.pem:certKey=cert.pem, tcp:port=8000:interface=127.0.0.1
2021-02-11 14:03:42,587 INFO HTTP/2 support enabled
2021-02-11 14:03:42,588 INFO Configuring endpoint ssl:8001:privateKey=privkey.pem:certKey=cert.pem
2021-02-11 14:03:42,592 INFO Listening on TCP address 0.0.0.0:8001
2021-02-11 14:03:42,592 INFO Configuring endpoint tcp:port=8000:interface=127.0.0.1
2021-02-11 14:03:42,593 INFO Listening on TCP address 127.0.0.1:8000