主应用程序的Websockets(nginx + passenger + faye)

时间:2014-12-08 01:41:28

标签: ruby-on-rails nginx websocket passenger faye

我试图在我的rails应用程序上设置websockets。我的应用程序适用于使用SocketRocker库的iOS客户端。

作为websockets后端,我使用faye-rails gem。 它作为机架中间件集成到rails应用程序

config.middleware.delete Rack::Lock
config.middleware.use FayeRails::Middleware, mount: '/ws', server: 'passenger', engine: {type: Faye::Redis, uri: redis_uri}, :timeout => 25 do
  map default: :block
end

在我使用Nginx将其上传到生产服务器之前,它一直很完美。我已经尝试了很多解决方案来将websocket请求传递给后端,但没有运气。最重要的是有两个服务器正在运行,但我只有一个。我的想法是我只需要将/ faye端点的请求代理到/ ws(更新标头)。什么是正确的proxy_pass参数应该在我的情况下?

    location /faye {
    proxy_pass http://$server_name/ws;
    proxy_buffering off;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    }

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,经过一段时间的努力,我终于可以让它发挥作用了。

我使用 nginx 1.8 服务器一起使用gem' faye-rails '我的挂载点是 / faye

我的nginx配置看起来像这样:

upstream thin_server {
    server 127.0.0.1:3000;
}

map $http_upgrade $connection_upgrade {
        default upgrade;
        ''      close;
}

server {
    ...   
    proxy_redirect off; 
    proxy_cache off;     

    location = /faye {
        proxy_pass http://thin_server;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        chunked_transfer_encoding off;
        proxy_buffering off;
        proxy_cache off;
    }

    location / {
        try_files $uri/index.html $uri.html $uri @proxy;
    }

    location @proxy {
        proxy_pass http://thin_server;
        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;                              
    }                                                            
    ...
}                                                  

让我工作的最后一个转折点是当我设置" location = / faye"。在我尝试之前" location / faye"和"位置〜/ faye"它失败了。 它看起来像是等号" ="防止nginx与其他位置设置混合。

相关问题