动作电缆不适用于生产手抖动问题
Action cable not working on production hand shake problem
Action cable 正在本地进行开发和生产模式,但尚未进行生产。我正在使用 docker 和美洲狮。我的 puma.rb
有以下代码
#!/usr/bin/env puma
environment ENV.fetch("RAILS_ENV") { "development" }
workers 2
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count / 2, threads_count
app_dir = File.expand_path("../..", __FILE__)
daemonize false
preload_app!
bind 'tcp://0.0.0.0:3000'
bind "unix://#{app_dir}/tmp/sockets/puma.sock"
# plugin :tmp_restart
rackup DefaultRackup
tag 'project_puma'
before_fork do
require 'puma_worker_killer'
interval = (ENV.fetch("PUMA_WORKER_KILLER_INTERVAL") { 180 }).to_i
PumaWorkerKiller.enable_rolling_restart(60 * interval)
end
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
on_restart do
ActiveRecord::Base.connection_pool.disconnect!
end
cable.yml
development:
adapter: redis
url: redis://localhost:6379/1
test:
adapter: async
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://redis:6379/1" } %>
channel_prefix: project_admin_production
在production.rb
我有
config.action_cable.url = "wss://mysite.com/cable"
在浏览器控制台中,我得到 Firefox can’t establish a connection to the server at ws://mysite.com/cable
。在日志中我可以看到错误 Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
,
我正在使用 rails 5.1.6
和 puma 3.4
。我需要做什么来解决这个问题。
我通过在 nginx 配置中添加以下内容解决了这个问题
location /cable {
proxy_pass http://puma;
proxy_http_version 1.1;
proxy_set_header Upgrade websocket;
proxy_set_header Connection Upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
break;
}
Action cable 正在本地进行开发和生产模式,但尚未进行生产。我正在使用 docker 和美洲狮。我的 puma.rb
有以下代码
#!/usr/bin/env puma
environment ENV.fetch("RAILS_ENV") { "development" }
workers 2
threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i
threads threads_count / 2, threads_count
app_dir = File.expand_path("../..", __FILE__)
daemonize false
preload_app!
bind 'tcp://0.0.0.0:3000'
bind "unix://#{app_dir}/tmp/sockets/puma.sock"
# plugin :tmp_restart
rackup DefaultRackup
tag 'project_puma'
before_fork do
require 'puma_worker_killer'
interval = (ENV.fetch("PUMA_WORKER_KILLER_INTERVAL") { 180 }).to_i
PumaWorkerKiller.enable_rolling_restart(60 * interval)
end
on_worker_boot do
ActiveSupport.on_load(:active_record) do
ActiveRecord::Base.establish_connection
end
end
on_restart do
ActiveRecord::Base.connection_pool.disconnect!
end
cable.yml
development:
adapter: redis
url: redis://localhost:6379/1
test:
adapter: async
production:
adapter: redis
url: <%= ENV.fetch("REDIS_URL") { "redis://redis:6379/1" } %>
channel_prefix: project_admin_production
在production.rb
我有
config.action_cable.url = "wss://mysite.com/cable"
在浏览器控制台中,我得到 Firefox can’t establish a connection to the server at ws://mysite.com/cable
。在日志中我可以看到错误 Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
,
我正在使用 rails 5.1.6
和 puma 3.4
。我需要做什么来解决这个问题。
我通过在 nginx 配置中添加以下内容解决了这个问题
location /cable {
proxy_pass http://puma;
proxy_http_version 1.1;
proxy_set_header Upgrade websocket;
proxy_set_header Connection Upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
break;
}