Skip to content

Instantly share code, notes, and snippets.

@uorat
Last active February 11, 2023 16:04
  • Star 61 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save uorat/10b15a32f3ffa3f240662b9b0fefe706 to your computer and use it in GitHub Desktop.
Nginx Reverse Proxy for WebSocket
upstream websocket {
server localhost:3000;
}
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/websocket.access.log main;
location /socket.io/ {
proxy_pass http://websocket;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /socket.io/socket.io.js {
proxy_pass http://websocket;
}
location /sample.html {
root /usr/share/nginx/html/;
}
}
@houming818
Copy link

Very useful +1!

@papac
Copy link

papac commented Nov 20, 2017

+1 Greeeeeaaaaaaaaat!!!!!

@CoreyCole
Copy link

@isaackg wondering this too. Did you find out?

@aidik
Copy link

aidik commented Jul 31, 2018

@isaackg & @CoreyCole Nginx would complain about invalid URL prefix.

nginx: [emerg] invalid URL prefix in /usr/local/etc/nginx/nginx.conf:75

@akatrevorjay
Copy link

akatrevorjay commented Oct 22, 2018

@isaackg @CoreyCole It's because ws:// doesn't mean anything in this context. ws:// is a protocol built on top of http://.

@ms4720
Copy link

ms4720 commented Dec 3, 2018

@isaackg @CoreyCole It's because ws:// doesn't mean anything in this context. ws:// is a protocol built on top of http://.

not according to wikipedia https://en.wikipedia.org/wiki/WebSocket

@Maciejszuchta
Copy link

Should it be put inside http {} block ?

@Quentinb
Copy link

Quentinb commented Apr 9, 2019

anybody experiencing timeouts after 1-2 minutes?
Getting a "The remote party closed the WebSocket connection without completing the close handshake" on the server side behind NGINX. Apparently timeout is required but not seeing a difference.

@arpanpreneur
Copy link

Thanks for saving my day

@PierreChambon
Copy link

INCREDIBLE thank you a lot

@stevendesu
Copy link

@ms4720 ws:// is a valid protocol (according to WikiPedia and IETF) but it's a protocol built on top of HTTP. Just like HTTP is "TCP used in a certain way", WebSockets are "HTTP used in a certain way". Specifically the server sends response headers and then leaves the connection open so that the client and server can pass messages back and forth on the (pre-established) TCP connection without the overhead of a new TCP handshake or additional HTTP headers.

nginx doesn't understand the ws:// scheme. It's designed as an HTTP proxy, so it expects all traffic to be either HTTP or HTTPS. Therefore as far as nginx is concerned, ws:// is just http:// but handled in a funny way and wss:// is just https:// but handled in a funny way. As for the "handled in a funny way" bit, support for that (specifically support for the Upgrade and Connection headers) was added to nginx in v1.13

If nothing else, just think of it as a quirk of nginx's implementation

@stevendesu
Copy link

I wish I had the same luck as everyone else commenting in getting this to work :( I tried setting up an nginx reverse websocket proxy and it kept closing my connection immediately (not after 1 minute, but immediately). I created a StackOverflow post about it, but never got any answers.

@mlakes-sigsci
Copy link

@stevendesu
Copy link

@mlakes-sigsci That's actually where I started with getting it set up :) After that I trolled around the internet and found all sorts of blog posts, forum posts, StackOverflow posts, etc. with tips to get it working, and it never did.

I later spun up a simple NodeJS WebSocket server and it worked fine, so it was some quirk of the web server that was preventing a reverse proxy -- not an issue with nginx. Instead of diagnosing the problem and figuring it out, we ended up ignoring the problem and finding another solution to our actual issue (I was using a reverse proxy to add a valid SSL certificate to a server being managed by someone else - we just got them to install a valid SSL cert so the proxy wasn't necessary)

@mlakes-sigsci
Copy link

@giovannyreyeso
Copy link

Thanks!! work perfectly with cloudflare + aws + nginx
This solves CORS problems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment