nginx recv() failed?
小程序的聊天室功能,后端是django+redis+nginx搭建的,但是在设置好后访问一直报错,查看nginx的日志是这样的:
recv() failed (104: Connection reset by peer) while reading response header from upstream,
no live upstreams while connecting to upstream,
小程序报错如下:
nginx设置如下:
upstream websocket {
server 服务器ip:8000;
}
server {
listen 443 ssl;
server_name 域名;
ssl_certificate *****;
ssl_certificate_key *****;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #使用此加密套件。
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #使用该协议进行配置。
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:8000;
proxy_http_version 1.1;
proxy_read_timeout 14400;
}
location /wss {
proxy_pass http://localhost:8000;#代理到上面的地址去,
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
绑定的域名如下
request合法域名 https://www.***.top:8000
socket合法域名 wss://www.***.top
前端请求如下
webSocket: function () {
// 创建Socket
SocketTask = wx.connectSocket({
url: 'wss://www.***.top' + app_data.chat_url + 'room/wss',
data: 'data',
header: {
'content-type': 'application/json'
},
method: 'POST',
success: function (res) {
console.log('WebSocket连接创建', res)
},
fail: function (err) {
wx.showToast({
title: '网络异常!',
})
console.log(err)
},
})
}
是因为nginx的转发做错了吗 我看到有人说是和redis有关但是 redis重启后还是没有用
3 回复
后端代码是这样的
class ChatConsumer(AsyncJsonWebsocketConsumer):
chats = dict()
async def connect(self):
self.group_name = self.scope['url_route']['kwargs']['group_name']
await self.channel_layer.group_add(self.group_name, self.channel_name)
try:
ChatConsumer.chats[self.group_name].add(self)
except:
ChatConsumer.chats[self.group_name] = {self}
await self.accept()
async def disconnect(self, close_code):
await self.channel_layer.group_discard(self.group_name, self.channel_name)
ChatConsumer.chats[self.group_name].remove(self)
await self.close()
async def receive_json(self, message, **kwargs):
print(message)
length = len(ChatConsumer.chats[self.group_name])
await self.channel_layer.group_send(
self.group_name,
{
'type': 'chat.message',
'user_name': message.get('user_name'),
'user_id': message.get('user_id'),
'content': message.get('content'),
'time': message.get('time')
},
)
async def chat_message(self, event):
await self.send_json({
'user_name': event['user_name'],
'user_id': event['user_id'],
'content': event['content'],
'time': event['time']
})