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

5
我希望您能够设置一个带有反向代理的Websocket服务器。为此,我创建了一个包含简单Python Websocket服务器和Nginx反向代理的Docker Compose。

设置:

enter image description here

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

enter image description here

我的步骤有什么问题吗?

更多信息:

谢谢!

1个回答

5
经过一番研究,我终于找到了问题所在:我将本地的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中删除了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;
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接