I'm using nginx 1.5 as a proxy in front of a nodejs express app, mostly to serve the static content. I've successfully upgraded the http version to 1.1 to enable websocket transport.
I'd now like to increase performance by adding reverse proxy caching. I have this in place, but now websockets appear to be cached? Is there an easy way to configure nginx to cache everything apart from websockets, specifically with the use of socket.io, including long-polling fallbacks etc..?
My config looks a little bit like this;
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
# express/socket.io app
upstream app {
server 127.0.0.1:3000;
}
# the nginx server instance
server {
listen 0.0.0.0:80;
server_name server-name.com;
location / {
#access_log off;
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;
proxy_pass http://app/;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_cache STATIC;
proxy_cache_valid 200 1d;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|flv|swf|html|htm|mp3|webm|ogv|ogg|mp3|m4a)$ {
root /var/www/server-name.com/public;
}
}
I don't quite understand what do you mean by:
but now websockets appear to be cached?
but probably this will help:
proxy_cache_bypass $http_upgrade;
Reference: http://nginx.org/r/proxy_cache_bypass