1

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;
    }
}

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help and apologies for the newbie questions. I'm very new to nginx and websockets. The proxy_cache_bypass solution appeared to be what I was looking for, however, I'm still having issues with websockets. When initiating a handshake with socket.io, I now get the following error; node: ../deps/uv/src/unix/stream.c:726: uv__write: Assertion `stream->write_queue_size == 0' failed.
I'm wondering if it might be an issues with nodejs 0.10.8 that I'm using? github.com/joyent/node/issues/5573 Essentially everything works without the proxy_cache settings and throws that error with them?
Sorry, I'm not experienced in nodejs. I have no idea what this error means.
No problem, thanks for you help. I'm pretty sure you've answered the original question correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.