cbcol
cbcol

Reputation: 479

Unable to connect to Websocket Server with Nginx reverse proxy

I want to set up a websocket server with a reverse proxy. To do so I create a docker-compose with a simple websocket server in python and a nginx reverse proxy.

SETUP:

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 server:

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()

Simple ws client that helps to run tests (I also used a Chrome extension):

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

PROBLEM:

When I try to connect (with the client or using the Chrome extension) the following error appeared:

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

enter image description here

Is there anything wrong in my steps?

More info:

Thanks!

Upvotes: 7

Views: 13170

Answers (1)

cbcol
cbcol

Reputation: 479

After some research I finally got what was wrong: I mapped my local nginx configuration to the wrong file on the container.

So to fix it a changed the volume in my docker-compose.yml

From:

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

To:

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

And also removed de http from the nginx.conf:

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;
        }
    }

Upvotes: 5

Related Questions