V
V
vikholodov2018-05-29 13:04:42
Nginx
vikholodov, 2018-05-29 13:04:42

How to deploy websocket to wss?

Hello.
Chat deployment on websockets has turned into some kind of wild hemorrhoid. You are our last hope, friends.
Django (Python) project. For the source, I took this repository https://github.com/Bearle/django-private-chat
I start the websocket server:

class Command(BaseCommand):
    help = 'Starts message center chat engine'

    def handle(self, *args, **options):
        asyncio.async(
            websockets.serve(
                handlers.main_handler,
                '0.0.0.0',
                '5002'
            )
        )
        logger.info('Chat server started')
        asyncio.async(handlers.new_messages_handler(channels.new_messages))
        asyncio.async(handlers.users_changed_handler(channels.users_changed))
        asyncio.async(handlers.gone_online(channels.online))
        asyncio.async(handlers.check_online(channels.check_online))
        asyncio.async(handlers.gone_offline(channels.offline))
        asyncio.async(handlers.is_typing_handler(channels.is_typing))
        asyncio.async(handlers.read_message_handler(channels.read_unread))
        loop = asyncio.get_event_loop()
        loop.run_forever()

added to the main nginx configs:
map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
  server {
        listen 5002;
        location / {
            proxy_pass http://ip хоста:5002;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }

Main nginx config
server {

    server_name домен;
    listen 80;
    listen 443 default_server ssl;
    ssl_certificate         /home/dev/c.pem;
    ssl_certificate_key     /home/dev/k.key;

    location /static/ {
        root /home/dev/gamemode_prj/gamemode/;
    }
    location /media/ {
        root /home/dev/gamemode_prj/gamemode/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/dev/gamemode_prj/gamemode/gamemode/gamemode.sock;
   }

I specify my domain on the front
websocket = new WebSocket(wss://domen + '{{ request.session.session_key }}/' + opponent_username);

The response is 404
WebSocket connection to 'wss://domen/yl8xetcvg6sbbqg2cdu15qkjhshvf0cr/Vasya' failed: Error during WebSocket handshake: Unexpected response code: 404

error.log nginx is silent, access:
162.158.91.162 - - [29/May/2018:05:50:53 -0400] "GET /yl8xetcvg6sbbqg2cdu15qkjhshvf0cr/Vasya HTTP/1.1" 404 1508 "-" "Mozilla/5.0 (Windows NT 10.0; W$
162.158.91.162 - - [29/May/2018:05:50:57 -0400] "GET /static/js/scrollMonitor.js.map HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) $

scrollMonitor.js performs decorative functions, I don't know why 304 pops up.
The maximum that I managed to do was to achieve operability by specifying ws: // ip of the host: port on the front.
It is necessary to wss...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
W
Wexter, 2018-05-29
@vikholodov

You have done proxying to a websocket in the wrong server section, add it to the section where port 443 and ssl are registered

A
Alexander Aksentiev, 2018-05-29
@Sanasol

Well, you didn't make a wss(https) server for the socket...
Either connect the certificate to the server itself or to nginx before proxying to the socket.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question