2013-05-29 63 views
1

我使用nginx 1.5作为nodejs express应用前的代理,主要是为静态内容提供服务。我已成功将http版本升级到1.1以启用websocket传输。使用Nginx作为nodej忽略websockets的反向代理

我现在想通过添加反向代理缓存来提高性能。我有这个地方,但现在websockets似乎被缓存?有没有简单的方法来配置nginx缓存一切,除了websockets,特别是使用socket.io,包括长轮询后备等。

我的配置看起来有点像这样;

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

回答

3

我不太明白你是什么意思:

但现在的WebSockets似乎被缓存?

但也许这将帮助:

proxy_cache_bypass $http_upgrade; 

参考:http://nginx.org/r/proxy_cache_bypass

+0

感谢您的帮助和道歉的新手问题。我对nginx和websockets很陌生。 proxy_cache_bypass解决方案似乎是我正在寻找的,但是,我仍然有websockets的问题。 当使用socket.io启动握手时,我现在得到以下错误; node:../deps/uv/src/unix/stream.c:726:uv__write:Assertion'stream-> write_queue_size == 0'失败。 –

+0

我在想,如果它可能是我使用的nodejs 0.10.8的问题? https://github.com/joyent/node/issues/5573 基本上,一切都没有proxy_cache设置,并引发与他们的错误? –

+0

对不起,我没有经历过nodejs。我不知道这个错误是什么意思。 – VBart