Search code examples
ruby-on-railsnginxwebsocketfayeprivate-pub

Websocket connection failed with nginx and faye


I'm trying to introduce chatting to my Rails app. For this purpose I used gem private_pub and it works perfectly in development mode.

In production I was using Apache + Passenger, but I couldn't configure Faye with it, so I changed Apache to Nginx. My main app is still on Apache server, and this demo on Nginx with port 8080 (just for test).

I'm able to connect to faye.js by entering http://chat.mysite.com:8080/faye.js, but the connection from app throws an error (browser console).

WebSocket connection to 'ws://localhost:9292/faye' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

After this error, another error appears every 5 seconds.

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

My private_pub.yml

production:
  server: "http://localhost:9292/faye"
  secret_token: "mysecret"
  signature_expiration: 3600 # one hour

My 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

My 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;
    }  
 }

If I change private_pub.yml to http://localhost:9292/faye/faye, I saw error like "can't load resource /faye/faye.js".

How should I change my Nginx conf or app yml to resolve websocket error?


Solution

  • I tried to configure my private_pub.yml as @niceman said. Now all is working good.

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