Skip to content

Instantly share code, notes, and snippets.

@atma
Created August 18, 2013 16:45
Show Gist options
  • Save atma/6262642 to your computer and use it in GitHub Desktop.
Save atma/6262642 to your computer and use it in GitHub Desktop.
Nginx + nodejs + socket.io websockets
# Add to nginx.conf http section
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream some_upsteram_com {
server 127.0.0.1:3000;
keepalive 15;
}
server {
listen 80;
root /home/user/www/public;
index index.html index.htm;
access_log /home/user/logs/site-access.log;
error_log /home/user/logs/site-error.log;
server_name site.com www.site.com;
large_client_header_buffers 8 32k;
location / {
try_files $uri @nodejs;
}
# Important! Serve client socket.io file as normal static file, e.g. /js/libs/socket.io/socket.io.min.js
location /socket.io/ {
proxy_pass http://some_upsteram_com;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
location @nodejs {
proxy_pass http://some_upsteram_com;
}
location ~ /\. {
deny all;
}
}
@WangJi
Copy link

WangJi commented Feb 22, 2016

what about multiple node.js APP that use socket.io?

@colorfulberry
Copy link

I got an error
"nginx: [emerg] unknown "connection_upgrade" variable" which I fixed by setting it to be 'proxy_set_header Connection "upgrade";'
I fixed it by follow
change
proxy_set_header Connection $connection_upgrade;
to
'proxy_set_header Connection "upgrade";'

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