无法使用 Nginx 反向代理连接到 Websocket 服务器

Unable to connect to Websocket Server with Nginx reverse proxy

我想设置一个带有反向代理的 websocket 服务器。为此,我在 python 中创建了一个 docker-compose,其中包含一个简单的 websocket 服务器和一个 nginx 反向代理。

设置:

docker-compose.yml:

version: '2.4'
services: 
    wsserver:
        restart: always
        ports: 
            - 8765:8765
        build: 
            context: ./server
            dockerfile: Dockerfile   
    ngproxy:
        image: nginx
        ports: 
            - 8020:80
            - 5000:5000
        restart: always
        depends_on:
            - wsserver
        volumes:
            - ./nginx/nginx.conf:/etc/nginx/conf.conf

nginx.conf:

http {
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
 
    upstream websocket {
        server  wsserver:8765;
    }
 
    server {
        listen 5000;

        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }
}

Websocket 服务器:

Dockerfile:

FROM python:3
RUN pip install websockets
RUN pip install asyncio
COPY server.py /
CMD [ "python", "./server.py" ]

server.py:

import asyncio
import websockets
import os


async def echo(websocket, path):
    async for message in websocket:
        print(message)
        await websocket.send(message)


asyncio.get_event_loop().run_until_complete(
    websockets.serve(echo, '0.0.0.0', 8765))
asyncio.get_event_loop().run_forever()

有助于 运行 测试的简单 ws 客户端(我还使用了 Chrome 扩展):

client.py:

import asyncio
import websockets


async def hello(uri):
    async with websockets.connect(uri) as websocket:
        await websocket.send("Hello world!")
        resp = await websocket.recv()
        print(resp)


asyncio.get_event_loop().run_until_complete(
    hello('ws://localhost:5000'))
# Without reverse proxy -> ws://localhost:8765

问题:

当我尝试连接(使用客户端或使用 Chrome 扩展)时出现以下错误:

WebSocket connection to 'ws://localhost:5000/' failed: Connection closed before receiving a handshake response

我的步骤有问题吗?

More info:

  • When I try connect to the websocket without the reverse proxy it works.
  • No messages appears in my nginx error logs.
  • All the configuration were based on nginx docs: nginx websocket
  • I have already checked this threads: Docker NGINX Proxy not Forwarding Websockets, Configure NGINX reverse proxy with browser WebSocket and docker-compose and

谢谢!

经过一番研究,我终于明白了问题所在:我将本地 nginx 配置映射到容器上的错误文件。

因此,为了修复它,我更改了 docker-compose.yml

中的音量

发件人:

volumes:
            - ./nginx/nginx.conf:/etc/nginx/conf.conf

收件人:

volumes:
            - ./nginx/nginx.conf:/etc/nginx/conf.d/default.conf:ro

并且还从 nginx.conf 中删除了 de http:

nginx.conf

 map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
 
    upstream websocket {
        server  wsserver:8765;
    }
 
    server {
        listen 5000;

        location / {
            proxy_pass http://websocket;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
        }
    }