项目在本地已经配置做,可以通过channels实现即时聊天,在搬运到服务器的时候出现了问题,目前还没有使用supervisor来启动daphne,而是直接手动启动daphne,想试下效果,但没有成功
Nginx.conf的相关配置如下
```bash
server {
listen 80;
server_name 8.217.113.224 www.hvacsalestool.com;
charset utf-8;
location / {
uwsgi_pass 127.0.0.1:8000;
include /etc/nginx/uwsgi_params;
}
location /static {
# 指定静态文件存放的目录
alias /var/openai/openai_wechat/static;
}
location /media {
# 指定静态文件存放的目录
alias /var/openai/openai_wechat/media;
}
location /wss {
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;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
在django项目目录下启动daphne -p 8001 -b 127.0.0.1 openai_wechat.asgi:application
启动成功如下图

然后再开一个远程启动Nginx
前端是微信小程序访问wss://www.hvacsalestool.com/websocket/chat/建立通讯,没有成功

而Nginx显示就是没找到

看样子应该是没有正确映射到daphne的端口,但应该怎么修改呢?谢谢。
_________________________________________________________________________________________________
我尝试直接在服务器上启动django,使用python manage.py runserver 0.0.0.0:8000,然后利用ws://ip:8000/websocket/chat/去建立连接,没有问题,这么看来肯定是Nginx的配置出现了问题,
我又尝试了channel的官方配置
```bash
upstream channels-backend {
server 127.0.0.1:8001; # 将此处的地址和端口设置为您自己的 Channels 后端服务器地址和端口
}
server {
...
location / {
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_pass http://channels-backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
...
}
依然是无法找到连接
具体报错发给我看一下,应该缺少很多依赖
启动django服务:
因为asgi相当于wsgi的扩展 并且我们在asgi.py文件中分别配置了http和websocket服务,所以直接用asgi启动就行了。
daphne 你的项目名.asgi:application -b 0.0.0.0 -p 8030
或者后台运行 nohup daphne 你的项目名.asgi:application -b 0.0.0.0 -p 8030 &
启动nginx
nginx
根据你提供的信息,可以看出你的Nginx配置存在一些问题。首先,你在Nginx配置中使用了uwsgi_pass指令来将请求转发给Django的uWSGI服务器,这是正常的。然而,对于WebSocket连接,你需要将请求转发给Daphne服务器而不是uWSGI服务器。
在你的配置中,你将WebSocket请求的转发位置设置为/wss。然而,根据你提供的信息,你尝试使用wss://www.hvacsalestool.com/websocket/chat/来建立连接。这意味着你的WebSocket请求应该被转发到/websocket/chat/而不是/wss。因此,你需要相应地修改Nginx配置。
创建一个新文件chat/templates/chat/room.html,并添加以下内容
<!-- chat/templates/chat/room.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Chat Room</title>
</head>
<body>
<textarea id="chat-log" cols="100" rows="20"></textarea><br>
<input id="chat-message-input" type="text" size="100"><br>
<input id="chat-message-submit" type="button" value="Send">
{{ room_name|json_script:"room-name" }}
<script>
const roomName = JSON.parse(document.getElementById('room-name').textContent);
const chatSocket = new WebSocket(
'ws://'
+ window.location.host
+ '/ws/chat/'
+ roomName
+ '/'
);
chatSocket.onmessage = function(e) {
const data = JSON.parse(e.data);
document.querySelector('#chat-log').value += (data.message + '\n');
};
chatSocket.onclose = function(e) {
console.error('Chat socket closed unexpectedly');
};
document.querySelector('#chat-message-input').focus();
document.querySelector('#chat-message-input').onkeyup = function(e) {
if (e.keyCode === 13) { // enter, return
document.querySelector('#chat-message-submit').click();
}
};
document.querySelector('#chat-message-submit').onclick = function(e) {
const messageInputDom = document.querySelector('#chat-message-input');
const message = messageInputDom.value;
chatSocket.send(JSON.stringify({
'message': message
}));
messageInputDom.value = '';
};
</script>
</body>
</html>
# chat/consumers.py
import json
from channels.generic.websocket import AsyncWebsocketConsumer
class ChatConsumer(AsyncWebsocketConsumer):
async def connect(self):
self.room_name = self.scope["url_route"]["kwargs"]["room_name"]
self.room_group_name = f"chat_{self.room_name}"
# Join room group
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
await self.accept()
async def disconnect(self, close_code):
# Leave room group
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
# Receive message from WebSocket
async def receive(self, text_data):
text_data_json = json.loads(text_data)
message = text_data_json["message"]
# Send message to room group
await self.channel_layer.group_send(
self.room_group_name, {"type": "chat.message", "message": message}
)
# Receive message from room group
async def chat_message(self, event):
message = event["message"]
# Send message to WebSocket
await self.send(text_data=json.dumps({"message": message}))
根据你后面提供的错误来看,报了一个GET /websocket/chat/ HTTP/1.1" 404的错误,检查下你的Nginx是否配置了转发,如果配置了nginx反向代理响应webSocket请求,则需要在代理的请求配置中加入下面的配置:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
同时在Nginx配置中配置转发路径:
location /sell/ {
proxy_pass http://127.0.0.1:8081/sell/;
}
location /sell/webSocket {
proxy_pass http://127.0.0.1:8081/sell/webSocket;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
这里的配置的意思是/sell下的请求转到8081端口处理,把/sell/webSocket请求转发到指定的请求接口
Nginx 没有正确配置反向代理,导致找不到/websocket/chat/ 这个路由的处理程序。
根据您提供的信息,您的Nginx配置中存在问题,导致无法正确映射到Daphne的端口。以下是可能的解决方案:
netstat -tln
检查端口的监听状态。proxy_pass
指令正确指向Daphne的地址和端口。根据您提供的信息,应该是proxy_pass http://127.0.0.1:8001;
。sestatus
命令检查SELinux的状态,并根据需要调整其设置。/var/log/nginx/error.log
,以获取有关连接问题的更多详细信息。错误日志中可能会显示与代理传递或连接问题相关的错误消息。通过检查和调整以上步骤,您应该能够解决Nginx无法正确映射到Daphne的问题,并成功建立与微信小程序的通信。