Nginx serve websocket

Add ws.conf file under nginx conf.d directory

etc/nginx/conf.d/ws.conf

server {listen 81;listen [::]:81;location /websocket/ {proxy_http_version 1.1;proxy_set_header Upgrade $http_upgrade;proxy_set_header Connection "upgrade";proxy_pass “http://localhost:87";}location / {}error_page 404 /404.html;location = /40x.html{}error_page 500 502 503 504 /50x.html;location = /50x.html{}}

Restart nginx

systemctl restart nginx

now use python websockets to test connection

pip install websockets

server.py

import asyncioimport websocketsasync def hello(websocket, path):    name = await websocket.recv()    print(f"< {name}")    greeting = f"Hello {name}!"    await websocket.send(greeting)    print(f"> {greeting}")start_server = websockets.serve(hello, "localhost", 87)asyncio.get_event_loop().run_until_complete(start_server)asyncio.get_event_loop().run_forever()

client.py

import asyncioimport websocketsasync def hello():    uri = "ws://localhost:87"    async with websockets.connect(uri) as websocket:        name = input("What’s your name? ")        await websocket.send(name)        print(f"> {name}")        greeting = await websocket.recv()        print(f"< {greeting}")asyncio.get_event_loop().run_until_complete(hello())

python server.py

python client.py

--

--

--

A Senior Software Developer/Body builder . to help others enjoy coding and stay healthy

Love podcasts or audiobooks? Learn on the go with our new app.

Understanding the Rust toolchain when you come from the C/C++ world

Serving up SpaceNet Imagery for Bokeh

The Many Meanings of Event-Driven Architecture — [Notes]

You’ve never used a dart type before.

What happened to Kubernetes in 2021?

How to Install Apache Cassandra on Windows 10

CONFIGURING APACHE WEBSERVER USING ANSIBLE

Reactive approach in mobile development

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
LORY

LORY

A Senior Software Developer/Body builder . to help others enjoy coding and stay healthy

More from Medium

Getting Started with ScyllaDB Cloud Using Node.js Part 1

MySQL VS MongoDB

Monitoring API Call Retries

Setup nginx + flask load balancer