Skip to content

Instantly share code, notes, and snippets.

@uorat
Last active February 11, 2023 16:04
Show Gist options
  • Star 61 You must be signed in to star a gist
  • Fork 21 You must be signed in to fork a gist
  • Save uorat/10b15a32f3ffa3f240662b9b0fefe706 to your computer and use it in GitHub Desktop.
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/;
}
}
@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