Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/;
}
}
@phonicmouse
Copy link

phonicmouse commented Jan 18, 2017

Very useful!

@K0rell
Copy link

K0rell commented May 8, 2017

I agree !

@tortechnocom
Copy link

tortechnocom commented Jul 27, 2017

It works. *****

@isaackg
Copy link

isaackg commented Aug 10, 2017

Why are you using http:// protocol instead of ws:// for the proxy_pass direction URL?

@houming818
Copy link

houming818 commented Nov 15, 2017

Very useful +1!

@papac
Copy link

papac commented Nov 20, 2017

+1 Greeeeeaaaaaaaaat!!!!!

@CoreyCole
Copy link

CoreyCole commented Jul 17, 2018

@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

Maciejszuchta commented Dec 12, 2018

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.

@ArpanKIIT2017
Copy link

ArpanKIIT2017 commented May 26, 2019

Thanks for saving my day

@PierreChambon
Copy link

PierreChambon commented Jul 11, 2019

INCREDIBLE thank you a lot

@stevendesu
Copy link

stevendesu commented Dec 4, 2019

@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

stevendesu commented Dec 4, 2019

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

mlakes-sigsci commented Jan 9, 2020

@stevendesu
Copy link

stevendesu commented Jan 9, 2020

@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

mlakes-sigsci commented Jan 9, 2020

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