繁体   English   中英

Websocket连接失败,nginx和faye

[英]Websocket connection failed with nginx and faye

我正在尝试将聊天引入我的Rails应用程序。 为此,我使用了gem private_pub,它在开发模式下运行良好。

在生产中,我使用的是Apache + Passenger,但是无法使用它配置Faye,因此我将Apache更改为Nginx。 我的主要应用仍在Apache服务器上,该演示在Nginx上使用端口8080(仅用于测试)。

我可以通过输入http://chat.mysite.com:8080/faye.js来连接到faye.js,但是来自应用程序的连接会引发错误(浏览器控制台)。

WebSocket与“ ws:// localhost:9292 / faye”的连接失败:连接建立错误: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 site.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在设计上与ActionCable非常相似。 在您走太远之前,您可能需要阅读我的博客文章“ ActionAcable-优缺点 ”,因为它解决了诸如private_pub类的系统很好时(如果不是)的合适用例。

如果您当然已经意识到缺点,那么祝您好运!

我试图按照@niceman的说明配置我的private_pub.yml。 现在一切正常。

production:
  server: "http://my-ip:8080/faye"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM