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

This comment has been minimized.

Copy link

@phonicmouse phonicmouse commented Jan 18, 2017

Very useful!

@K0rell

This comment has been minimized.

Copy link

@K0rell K0rell commented May 8, 2017

I agree !

@tortechnocom

This comment has been minimized.

Copy link

@tortechnocom tortechnocom commented Jul 27, 2017

It works. *****

@isaackg

This comment has been minimized.

Copy link

@isaackg isaackg commented Aug 10, 2017

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

@stdhi

This comment has been minimized.

Copy link

@stdhi stdhi commented Nov 15, 2017

Very useful +1!

@papac

This comment has been minimized.

Copy link

@papac papac commented Nov 20, 2017

+1 Greeeeeaaaaaaaaat!!!!!

@CoreyCole

This comment has been minimized.

Copy link

@CoreyCole CoreyCole commented Jul 17, 2018

@isaackg wondering this too. Did you find out?

@aidik

This comment has been minimized.

Copy link

@aidik 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

This comment has been minimized.

Copy link

@akatrevorjay 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

This comment has been minimized.

Copy link

@ms4720 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

This comment has been minimized.

Copy link

@Maciejszuchta Maciejszuchta commented Dec 12, 2018

Should it be put inside http {} block ?

@Quentinb

This comment has been minimized.

Copy link

@Quentinb 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

This comment has been minimized.

Copy link

@ArpanKIIT2017 ArpanKIIT2017 commented May 26, 2019

Thanks for saving my day

@PierreChambon

This comment has been minimized.

Copy link

@PierreChambon PierreChambon commented Jul 11, 2019

INCREDIBLE thank you a lot

@stevendesu

This comment has been minimized.

Copy link

@stevendesu 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

This comment has been minimized.

Copy link

@stevendesu 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

This comment has been minimized.

@stevendesu

This comment has been minimized.

Copy link

@stevendesu 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

This comment has been minimized.

Copy link

@mlakes-sigsci mlakes-sigsci commented Jan 9, 2020

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