【发布时间】:2016-10-17 17:49:30
【问题描述】:
我正在尝试在我的 Rails 应用中引入聊天功能。为此,我使用了 gem private_pub,它在开发模式下完美运行。
在生产中我使用的是 Apache + Passenger,但我无法使用它配置 Faye,所以我将 Apache 更改为 Nginx。我的主要应用程序仍在 Apache 服务器上,这个演示在 Nginx 上使用端口 8080(仅用于测试)。
我可以通过输入 http://chat.mysite.com:8080/faye.js 连接到 faye.js,但是来自应用的连接会引发错误(浏览器控制台)。
到 'ws://localhost:9292/faye' 的 WebSocket 连接失败:连接建立错误:net::ERR_CONNECTION_REFUSED
在此错误之后,每 5 秒出现另一个错误。
faye.js:2 GET http://localhost:9292/faye?message=%5B%7B%22channel%22%3A%22%2Fmeta%2Fhands…22%2C%22callback-polling%22%5D%2C%22id%22%3A%221%22%7D%5D&jsonp=jsonp2 net ::ERR_CONNECTION_REFUSED
我的 private_pub.yml
production:
server: "http://localhost:9292/faye"
secret_token: "mysecret"
signature_expiration: 3600 # one hour
我的 private_pub.ru
require "bundler/setup"
require "yaml"
require "faye"
require "private_pub"
Faye::WebSocket.load_adapter('thin')
PrivatePub.load_config(File.expand_path("../config/private_pub.yml", __FILE__), ENV["RAILS_ENV"] || "development")
run PrivatePub.faye_app
我的 nginx 站点.conf
server {
listen 8080;
server_name www.chat.mysite.com;
passenger_enabled on;
passenger_app_env production;
root /var/www/mysite/public;
location ^~ /faye {
proxy_pass http://127.0.0.1:9292;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_buffering off;
proxy_redirect off;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
#proxy_set_header X-Forwarded-Proto https;
proxy_cache_bypass $http_pragma $http_authorization;
proxy_no_cache $http_pragma $http_authorization;
break;
}
}
如果我将 private_pub.yml 更改为 http://localhost:9292/faye/faye,我会看到类似“无法加载资源 /faye/faye.js”的错误。
我应该如何更改我的 Nginx conf 或 app yml 来解决 websocket 错误?
【问题讨论】:
-
在 private_pub.yml 中尝试将服务器更改为“localhost:8080/faye”
-
@niceman 收到错误 application-63199a9….js:14 GET localhost:8080/faye.js net::ERR_CONNECTION_REFUSED
-
hmmm 也许你应该在 private_pub.yml 中放置服务器名称而不是 localhost,我认为放置 localhost 会使客户端调用自己而不是你的服务器
标签: ruby-on-rails nginx websocket faye private-pub