新手问题 [已解决] 最近在用'faye-rails'做即时聊天,本地跑的没有问题,但是通过 nginx 部署到服务器上就不能用  

tianlitao · 2015年01月09日 · 最后由 rainxie 回复于 2017年09月25日 · 5580 次阅读

根据 faye-rails 这个"demo"做的,nginx 是需要做如何配置呢

nginx 是什么版本?应该是配置 websocket

#1 楼 @jimrokliu nginx version: nginx/1.6.0 websocket 该如何配置,有没有推荐的网址,谢谢

试着配置 nginx,会报错 WebSocket connection to 'ws://192.168.1.107/faye' failed: Error during WebSocket handshake: Unexpected response code: 301

location /faye/ {
     proxy_pass http://192.168.1.107;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection $connection_upgrade;
 }

我们的配置和你这个基本一样,我这里没问题。

location ~ ^/comet {
   proxy_pass http://faye;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "upgrade";
 }

在 appllication 下配置为

config.middleware.use FayeRails::Middleware, mount: '/faye', :timeout => 25,server:'passenger', engine: {type: Faye::Redis, host: 'localhost'} do
  map '/chat' => RealtimeChatController
  map default: :block
end

会 error.log 报这个错

cannot load such file -- passenger (LoadError)
  /var/lib/gems/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
  /var/lib/gems/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
  /var/lib/gems/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
  /var/lib/gems/1.9.1/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
  /var/lib/gems/1.9.1/gems/faye-websocket-0.9.2/lib/faye/websocket.rb:42:in `load_adapter'
  /var/lib/gems/1.9.1/gems/faye-rails-2.0.0/lib/faye-rails/middleware.rb:44:in `initialize'
  /var/lib/gems/1.9.1/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:43:in `new'
  /var/lib/gems/1.9.1/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:43:in `build'
  /var/lib/gems/1.9.1/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:118:in `block in build'
  /var/lib/gems/1.9.1/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:118:in `each'
  /var/lib/gems/1.9.1/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:118:in `inject'
  /var/lib/gems/1.9.1/gems/actionpack-4.1.4/lib/action_dispatch/middleware/stack.rb:118:in `build'
  /var/lib/gems/1.9.1/gems/railties-4.1.4/lib/rails/engine.rb:498:in `app'
  /var/lib/gems/1.9.1/gems/railties-4.1.4/lib/rails/application/finisher.rb:36:in `block in <module:Finisher>'
  /var/lib/gems/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `instance_exec'
  /var/lib/gems/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:30:in `run'
  /var/lib/gems/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:55:in `block in run_initializers'
  /usr/lib/ruby/1.9.1/tsort.rb:150:in `block in tsort_each'
  /usr/lib/ruby/1.9.1/tsort.rb:183:in `block (2 levels) in each_strongly_connected_component'
  /usr/lib/ruby/1.9.1/tsort.rb:219:in `each_strongly_connected_component_from'
  /usr/lib/ruby/1.9.1/tsort.rb:182:in `block in each_strongly_connected_component'
  /usr/lib/ruby/1.9.1/tsort.rb:180:in `each'
  /usr/lib/ruby/1.9.1/tsort.rb:180:in `each_strongly_connected_component'
  /usr/lib/ruby/1.9.1/tsort.rb:148:in `tsort_each'
  /var/lib/gems/1.9.1/gems/railties-4.1.4/lib/rails/initializable.rb:54:in `run_initializers'
  /var/lib/gems/1.9.1/gems/railties-4.1.4/lib/rails/application.rb:300:in `initialize!'
  /home/tlt/work/faye-rails-demo-master/config/environment.rb:5:in `<top (required)>'
  config.ru:3:in `require'
  config.ru:3:in `block in <main>'
  /var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
  /var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
  config.ru:1:in `new'
  config.ru:1:in `<main>'
  /var/lib/gems/1.9.1/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:112:in `eval'
  /var/lib/gems/1.9.1/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:112:in `preload_app'
  /var/lib/gems/1.9.1/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:158:in `<module:App>'
  /var/lib/gems/1.9.1/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:29:in `<module:PhusionPassenger>'
  /var/lib/gems/1.9.1/gems/passenger-4.0.57/helper-scripts/rack-preloader.rb:28:in `<main>'

有人碰到过这种问题么,求助啊

解决了 https://github.com/jamesotron/faye-rails/issues/58

回来结贴,修改 gem faye-rails lib/faye-rails/middleware.rb

       options = DEFAULTS.merge(options)
-      Faye::WebSocket.load_adapter(options.delete(:server))
+      server = options.delete(:server)
+      Faye::WebSocket.load_adapter(server) if server && server != 'passenger'

       @adapter = FayeRails::RackAdapter.new(@app, options)
       @adapter.instance_eval(&block) if block.respond_to? :call
jimrokliu 回复
location ~ ^/comet {

我犯低级错误了,路径竟然没写对,谢谢!

需要 登录 后方可回复, 如果你还没有账号请 注册新账号