Skip to content

Instantly share code, notes, and snippets.

@benweet
Last active June 28, 2016 22:09
Show Gist options
  • Save benweet/a74c87dc2ef0add10e7aeb83f986f013 to your computer and use it in GitHub Desktop.
Save benweet/a74c87dc2ef0add10e7aeb83f986f013 to your computer and use it in GitHub Desktop.
socket.io / engine.io load balancing with stickyness using nginx hash
// Same random hash will be used during the whole session
var hash = Math.random().toString(36).slice(2, 10)
// Get absolute uri
var uri = document.createElement('a')
uri.href = '/engine.io/?hash=' + hash
var socket = eio(uri.href)
http {
upstream eio {
hash $arg_hash consistent;
server localhost:3001;
server localhost:3002;
}
server {
listen 3000;
location /engine.io {
proxy_pass http://eio;
proxy_http_version 1.1;
proxy_next_upstream error timeout invalid_header;
proxy_connect_timeout 2;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment