无法在生产服务器上为 websocket-rails 启动独立服务器

Posted

技术标签:

【中文标题】无法在生产服务器上为 websocket-rails 启动独立服务器【英文标题】:Unable to start stand alone server for websocket-rails on production server 【发布时间】:2014-07-01 06:26:36 【问题描述】:

我正在尝试使用websocket-rails 在我的应用程序上实现类似聊天的系统。我的Gemfile 看起来像:

宝石文件

source 'https://rubygems.org'


# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.1'
# Use sqlite3 as the database for Active Record
gem 'mysql2'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'
# Use Uglifier as compressor for javascript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
gem 'therubyracer',  platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0'
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '~> 0.4.0',          group: :doc


# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring',        group: :development

group :development, :test do
  gem 'byebug'
  gem 'annotate'
  gem 'better_errors'
  gem 'bullet'
  gem 'railroady'
  gem 'quiet_assets'
  gem 'jazz_hands'
end

gem 'thin'


# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Use unicorn as the app server
# gem 'unicorn'
gem 'unicorn' , group: :production

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

  gem 'capistrano','~> 2.15.5' , group: :development
  gem 'rvm-capistrano'
  gem 'capistrano-ext'

# Use debugger
# gem 'debugger', group: [:development, :test]
gem 'lorempixum', '~> 0.0.3'


gem 'kaminari'

gem "opentok", "~> 2.2"
gem 'slim'

gem 'pg'
gem 'rails_12factor', group: :production
gem 'private_pub'

gem 'devise'
gem 'redis'
gem 'websocket-rails'

config/initializers/websocket_rails.rb

WebsocketRails.setup do |config|

  # Uncomment to override the default log level. The log level can be
  # any of the standard Logger log levels. By default it will mirror the
  # current Rails environment log level.
  # config.log_level = :debug

  # Uncomment to change the default log file path.
  # config.log_path = "#Rails.root/log/websocket_rails.log"

  # Set to true if you wish to log the internal websocket_rails events
  # such as the keepalive `websocket_rails.ping` event.
  # config.log_internal_events = false

  # Change to true to enable standalone server mode
  # Start the standalone server with rake websocket_rails:start_server
  # * Requires Redis
  config.standalone = true

  # Change to true to enable channel synchronization between
  # multiple server instances.
  # * Requires Redis.
  config.synchronize = false

  # Prevent Thin from daemonizing (default is true)
  # config.daemonize = false

  # Uncomment and edit to point to a different redis instance.
  # Will not be used unless standalone or synchronization mode
  # is enabled.
  # config.redis_options = :host => 'localhost', :port => '6379'

  # By default, all subscribers in to a channel will be removed
  # when that channel is made private. If you don't wish active
  # subscribers to be removed from a previously public channel
  # when making it private, set the following to true.
  # config.keep_subscribers_when_private = false

  # Set to true if you wish to broadcast channel subscriber_join and
  # subscriber_part events. All subscribers of a channel will be
  # notified when other clients join and part the channel. If you are
  # using the UserManager, the current_user object will be sent along
  # with the event.
  # config.broadcast_subscriber_events = true

  # Used as the key for the WebsocketRails.users Hash. This method
  # will be called on the `current_user` object in your controller
  # if one exists. If `current_user` does not exist or does not
  # respond to the identifier, the key will default to `connection.id`
  # config.user_identifier = :id

  # Uncomment and change this option to override the class associated
  # with your `current_user` object. This class will be used when
  # synchronization is enabled and you trigger events from background
  # jobs using the WebsocketRails.users UserManager.
  # config.user_class = User

  # Supporting HTTP streaming on Internet Explorer versions 8 & 9
  # requires CORS to be enabled for GET "/websocket" request.
  # List here the origin domains allowed to perform the request.
  # config.allowed_origins = ['http://localhost:3000']
  config.standalone_port = 3245

end

按照https://github.com/websocket-rails/websocket-rails/wiki/Standalone-Server- 所述启动服务器,在本地机器上一切正常

$ bundle exec rake websocket_rails:start_server
Websocket Rails Standalone Server listening on port 3245

$ ps -ef | grep 3245
sahil     8719  2290  4 11:50 ?        00:00:02 thin server (0.0.0.0:3245) [websocket_rails]                                                                                                                  
sahil     8771  3688  0 11:51 pts/1    00:00:00 grep 3245

但服务器上的返回结果相同

$ ps -ef | grep 3245
deploy    7249  1348  0 02:23 pts/0    00:00:00 grep --color=auto 3245

显然没有运行 websocket_rails 的瘦服务器。生产服务器上可能有什么问题?

【问题讨论】:

bundle exec rake websocket_rails:start_server RAILS_ENV=production working 嗨,sahil,我面临着类似的问题。我的服务器(包括瘦服务器、乘客服务器和 nginx)工作正常,但我无法代理传递 websocket 请求,因此它被转发到瘦服务器。这是我面临的问题的链接-> github.com/websocket-rails/websocket-rails/issues/… 你遇到这个问题了吗?你是怎么解决这个问题的。让我知道。谢谢 【参考方案1】:

您可能没有设置环境变量。为了解决类似的问题,在我们的生产服务器上,所有 Rails 应用程序都在一个名为“passenger”的用户下运行。该用户在其配置文件脚本读取中有一个环境变量定义:

export RAILS_ENV="production"

我们为 capistrano 用户做了同样的事情,因此资产预编译、服务器任务等都在正确的环境下运行。

【讨论】:

以上是关于无法在生产服务器上为 websocket-rails 启动独立服务器的主要内容,如果未能解决你的问题,请参考以下文章

websocket-rails 在生产中没有连接

websocket-rails,websocket握手错误

websocket-rails:服务器发出 http:// 协议;客户端期望 ws:// 协议

为啥 websocket-rails 需要作为独立服务器启动以支持基于非事件机的 Web 服务器?

在单个服务器上为多个 Laravel 应用程序使用 Redis 队列

用于 websocket-rails gem 的 Ruby websocket 客户端