我正在为我的项目使用网络套接字。我将python aiohttp用于服务器和客户端。在本地,通过ngrok,它运行完美。但是当我将它与Nginx一起放在服务器上时遇到了一些问题。
我已按照https://www.nginx.com/blog/websocket-nginx/
的指示进行了所有操作map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
server_name example.com;
listen 80;
root /var/www/html;
error_log /var/log/nginx/error.log debug;
...
location /ws {
include proxy_params;
proxy_pass http://127.0.0.1:5402/ws;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
}
}
当我连接到example.com/ws时,它可以按计划工作。我从Websocket服务器获得了下一个信息:
<Request GET /ws >
<WebSocketResponse Switching Protocols GET /ws > #HTTP 101 Switching Protocols
Nginx日志接下来显示给我:
GET /ws HTTP/1.0
Host: example.com
X-Real-IP: ip
X-Forwarded-For: ip
X-Forwarded-Proto: http
Upgrade: WebSocket
Connection: upgrade
Sec-WebSocket-Version: 13
Sec-WebSocket-Key: 8LiAQ/oYM6PCsJ60brbQww==
Accept: */*
Accept-Encoding: gzip, deflate
User-Agent: Python/3.7 aiohttp/3.6.2
HTTP/1.1 101 Switching Protocols
Server: nginx/1.16.1
Date: Tue, 03 Mar 2020 16:59:49 GMT
Content-Type: application/octet-stream
Connection: upgrade
Upgrade: websocket
Sec-WebSocket-Accept: 7revI3OKdLXh8X/mtb/4h/3AVn8=
但是当我尝试通过客户端发送一些数据时,我的连接挂起了。我收到超时错误。我的后端服务器未收到任何请求。 Nginx记录下一条消息:
[info] 43386#43386: *1 upstream timed out (110: Connection timed out) while proxying upgraded connection, client: ip, server: example.com, request: "GET /ws HTTP/1.1", upstream: "http://127.0.0.1:5402/ws", host: "example.com"
PS,ngrok隧道服务器的后端日志消息(当所有按计划工作时):
<Request GET /ws >
<WebSocketResponse Switching Protocols GET /ws > #HTTP 101 Switching Protocols
WSMessage(type=<WSMsgType.TEXT: 1>, data=some_str)
您知道如何解决此问题吗?