Puma和Nginx上的Rails ActionCable在通过capistrano部署后无法进行生产

Var*_*kul 9 ruby-on-rails nginx puma actioncable

我在Puma和Nginx上使用ActionCable通过Rails创建网站.

但是在部署到Production服务器之后,似乎ActionCable因为这个问题而无法正常工作.WebSocket连接到

WebSocket connection to 
'ws://sub.domain.com/cable' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
Run Code Online (Sandbox Code Playgroud)

这是我的 production.rb

config.action_cable.url = 'ws://sub.domain.com/cable'
config.web_socket_server_url = "ws://sub.domain.com/cable"
config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]
config.action_cable.disable_request_forgery_protection = true
Run Code Online (Sandbox Code Playgroud)

这是我的nginx.conf

upstream puma {
  server unix:///home/deploy/apps/app_name/shared/tmp/sockets/app_name-puma.sock;
}

server {
  listen 80 default_server deferred;
  # server_name example.com;

  root /home/deploy/apps/app_name/current/public;
  access_log /home/deploy/apps/app_name/current/log/nginx.access.log;
  error_log /home/deploy/apps/app_name/current/log/nginx.error.log info;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  location /cable {
    proxy_pass http://puma;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";

    # proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # proxy_set_header Host $http_host;
    # proxy_set_header X-Real-IP $remote_addr;
    # proxy_set_header X-Forwarded-Proto https;
    # proxy_redirect off;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 10M;
  keepalive_timeout 10;
}
Run Code Online (Sandbox Code Playgroud)

我的puma.rb

threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }
threads threads_count, threads_count
port        ENV.fetch("PORT") { 3000 }
workers ENV.fetch("WEB_CONCURRENCY") { 2 }
plugin :tmp_restart
environment ENV.fetch("RAILS_ENV") { "production" }
daemonize true
pidfile '/home/deploy/apps/app_name/shared/tmp/pids/puma.pid'
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题并使ActionCable能够在生产中使用?

sad*_*zzy 0

也许,你应该取消评论

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
Run Code Online (Sandbox Code Playgroud)

位置/电缆块中的标头,根据答案,通常由于它们而无法工作