使用Nginx、Puma和Redis部署Rails 5 Action Cable

12

我正在尝试使用Capistrano将一个启用了Action Cable的应用程序部署到VPS。我正在使用Puma、Nginx和Redis(用于Cable)。经过一些困难,我能够在本地开发环境中使其正常工作。我正在使用默认的in-process /cable URL。但是,在我尝试将其部署到VPS时,JS-log中一直出现这两个错误:

Establishing connection to host ws://{server-ip}/cable failed.
Connection to host ws://{server-ip}/cable was interrupted while loading the page.

在我的应用程序专用的nginx.error.log中,我得到了这些消息:

2016/03/10 16:40:34 [info] 14473#0: *22 client 90.27.197.34 closed keepalive connection

在 JS 提示中启用 ActionCable.startDebugging() 并没有显示任何有趣的内容。只是 ConnectionMonitor 无限期地尝试重新打开连接。我还在我的网络监视器中得到了大量的 301: Moved permanently -requests,请求 /cable。

我尝试过的事情:

  • Using the async adapter instead of Redis. (This is what is used in the developement env)
  • Adding something like this to my /etc/nginx/sites-enabled/{app-name}:

    location /cable/ {
      proxy_pass http://puma;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
    }
    
  • Setting Rails.application.config.action_cable.allowed_request_origins to the proper host (tried "http://{server-ip}" and "ws://{server-ip}")

  • Turning on Rails.application.config.action_cable.disable_request_forgery_protection

运气不好。是什么原因导致了这个问题?

$ rails -v
Rails 5.0.0.beta3

请告知我任何可能有用的额外细节。

1个回答

14

最后,我终于搞定了!我已经尝试了各种方法大约一个星期...

301重定向是由nginx试图将浏览器重定向到/cable/而不是/cable所致。这是因为我在location段中指定了/cable/而不是/cable!我从这个答案得到了灵感。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接