再会!我想用 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);
});
});
我能做些什么?在开发环境中一切正常,但在生产中只有错误。