【问题标题】:ActionCable not working with nginx on cloud serverActionCable 无法在云服务器上使用 nginx
【发布时间】:2018-02-28 09:29:57
【问题描述】:

我正在使用 rails 5 作为 actioncable。我已经在我的本地机器上(在开发环境中)开发和测试了带有 actioncable 的应用程序。但是当我尝试将代码部署到云服务器(即 digitalocean)时。 Actioncable 突然停止工作。我正在使用 Puma 和 nginx。我尝试在开发模式下启动服务器,但它仍然无法正常工作。

我的文件如下 发展.rb ' Rails.application.configure 做 # 此处指定的设置将优先于 config/application.rb 中的设置。

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

# Do not eager load code on boot.
config.eager_load = false

config.action_cable.disable_request_forgery_protection = true

# Show full error reports.
config.consider_all_requests_local = true

config.action_mailer.default_url_options = { host: 'localhost:3000' }

# Rails.application.configure do 
#   config.action_cable.url = "ws://localhost:3000/cable"
# end 

config.action_cable.allowed_request_origins = ['webserver','webserver']
config.web_socket_server_url = "wss://webserver/cable" 


# Rails.application.configure do 
#   config.action_cable.url = "wss://webserver/cable"
# end 

# config.action_cable.allowed_request_origins = ['http://webserver']

# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true

config.cache_store = :memory_store
config.public_file_server.headers = {
  'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = false

config.cache_store = :null_store
end

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log

# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true

# Suppress logger output for asset requests.
config.assets.quiet = true
config.active_job.queue_adapter = :resque

# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end

nginx 配置文件

    upstream app_server {
            server 127.0.0.1:3000;
    }

    server {
        listen   80;
        root /home/spot_me/public;
        server_name 165.227.186.52;
        index index.htm index.html;

        location / {
                try_files $uri/index.html $uri.html $uri @app;
        }

        location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
                        try_files $uri @app;
                }

         location @app {
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_redirect off;
                proxy_pass http://app_server;
        }

         location /cable {
            proxy_pass http://app_server/;
            proxy_http_version 1.1;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Proto http;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_set_header X-Real-IP $remote_addr;
            proxy_read_timeout 86400;
            proxy_redirect off;
                }

    }

config/puma.rb

     Puma can serve each request in a thread from an internal thread pool.
    # The `threads` method setting takes two numbers a minimum and maximum.
    # Any libraries that use thread pools should be configured to match
    # the maximum value specified for Puma. Default is set to 5 threads for minimum
    # and maximum, this matches the default thread size of Active Record.
    #
    threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
    threads threads_count, threads_count

    # Specifies the `port` that Puma will listen on to receive requests, default is 3000.
    #
    port        ENV.fetch("PORT") { 3000 }

    # Specifies the `environment` that Puma will run in.
    #
    environment ENV.fetch("RAILS_ENV") { "development" }

    # Specifies the number of `workers` to boot in clustered mode.
    # Workers are forked webserver processes. If using threads and workers together
    # the concurrency of the application would be max `threads` * `workers`.
    # Workers do not work on JRuby or Windows (both of which do not support
    # processes).
    #
    # workers ENV.fetch("WEB_CONCURRENCY") { 2 }

    # Use the `preload_app!` method when specifying a `workers` number.
    # This directive tells Puma to first boot the application and load code
    # before forking the application. This takes advantage of Copy On Write
    # process behavior so workers use less memory. If you use this option
    # you need to make sure to reconnect any threads in the `on_worker_boot`
    # block.
    #
    # preload_app!

    # The code in the `on_worker_boot` will be called if you are using
    # clustered mode by specifying a number of `workers`. After each worker
    # process is booted this block will be run, if you are using `preload_app!`
    # option you will want to use this block to reconnect to any threads
    # or connections that may have been created at application boot, Ruby
    # cannot share connections between processes.
    #
    # on_worker_boot do
    #   ActiveRecord::Base.establish_connection if defined?(ActiveRecord)
    # end

    # Allow puma to be restarted by `rails restart` command.
    plugin :tmp_restart

【问题讨论】:

    标签: nginx ruby-on-rails-5 digital-ocean puma actioncable


    【解决方案1】:

    Nginx 可以与乘客服务器顺利运行。您可以使用乘客应用程序服务器来运行您的 rails 应用程序。在您的Gemfile 中添加gem 'passenger' 并运行bundle install。 要启动应用程序,请使用以下命令: RAILS_ENV=production passenger start -p 3000

    您还可以检查应用程序中的以下文件/步骤:

    1. 确保您的Gemfile 中有gem 'redis', '~> 3.0'
    2. config/cable.yml文件应该被添加,它应该有以下内容:

      default: &default
      adapter: redis
        url: redis://localhost:6379/1
      
      development:
        <<: *default
      
      test:
        adapter: async
      
      production:
        adapter: redis
        url: redis://localhost:6379/1
      
    3. mount ActionCable.server =&gt; '/cable'在路由文件顶部添加路由。

    4. application.html.erb中添加&lt;%= action_cable_meta_tag %&gt;

    5. application.js中添加以下行:

      +//= require cable
      +//= require_tree ./channels
      
    6. 确保在启动 rails 应用程序之前启动 redis 服务器。 redis-server

    【讨论】:

    • 其实我用的是android作为前端而不是网页。现在的问题是 channels/connection.rb 的连接方法 的连接方法被调用,但我无法调用任何 channel 对象 的订阅。提前致谢。 @vijaymmali
    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 2022-01-25
    • 2018-01-06
    • 2011-06-06
    相关资源
    最近更新 更多