1

再会!我想用 Faye gem 运行 websocket 应用程序,但是出现以下问题:当我在生产服务器上部署我的应用程序时,Ngix 无法接收 faye.js 并且无法连接到 faye 服务器。在 Nginx error.log 中,我发现了下一个错误:

2014/12/24 15:44:39 [error] upstream prematurely closed connection while reading response header from upstream, client: 46.0.121.23, server: example.com, request: "GET /faye HTTP/1.1", upstream: "http://127.0.0.1:9292/faye", host: "example.com"

2014/12/24 15:44:39 [error] *1 connect() failed (111: Connection refused) while connecting to upstream, client: 46.0.121.23, server: example.com, request: "GET /faye HTTP/1.1", upstream: "http://127.0.0.1:9292/faye", host: "example.com"

我尝试如何在使用 dokku 部署的 rails 应用程序上启动 faye 服务器?NGINX + rails + dokku 上的 Error 502 Bad Gateway答案,但这对我没有帮助。

我的档案是

web: bundle exec rails s Puma -p 5000
faye: bundle exec rackup s Puma faye.ru

我的 faye.ru 是

require 'faye'
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)

class ServerAuth
  def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      if message['ext']['auth_token'] != FAYE_TOKEN
        message['error'] = 'Invalid authentication token.'
      end
    end
    callback.call(message)
  end
end
faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 0)
faye_server.add_extension(ServerAuth.new)
run faye_server

我的 nginx.conf 是:

upstream example.com { server 127.0.0.1:49169; }
server {
  listen      [::]:80;
  listen      80;
  server_name example.com;
  location    / {
    proxy_pass  http://example.com;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Forwarded-Port $server_port;
    proxy_set_header X-Request-Start $msec;
  }
  location /faye {
    proxy_redirect     off;
    proxy_set_header   Upgrade    $http_upgrade;
    proxy_set_header   Connection "upgrade";
    proxy_http_version 1.1;
    proxy_buffering    off;
    proxy_cache_bypass $http_pragma $http_authorization;
    proxy_no_cache     $http_pragma $http_authorization;
    proxy_pass http://localhost:9292;
  }
}

我的应用接下来获取 faye.js:

<%= javascript_include_tag "http://example.com/faye.js" %>

并连接到faye服务器

$(function() {
    var faye = new Faye.Client('http://example.com/faye');
    faye.subscribe('/comments/new', function (data) {
        eval(data);
    });
});

我能做些什么?在开发环境中一切正常,但在生产中只有错误。

4

1 回答 1

0

尝试从这个要点配置 https://gist.github.com/Bubelbub/0a942a0d51a3d329897d

在服务器部分:

large_client_header_buffers 8 32k;

在费伊位置:

 proxy_buffers 8 32k;
 proxy_buffer_size 64k;
于 2015-03-11T14:20:09.377 回答