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

时间:2017-10-31 22:18:41

标签: 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

这是我的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

这是我的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;
}

我的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'

如何解决此问题并使ActionCable适用于生产?

0 个答案:

没有答案