ActionCable Rails 5 (Passenger) - 失败:WebSocket 握手期间出错:意外响应代码:404

ActionCable Rails 5 (Passenger) - failed: Error during WebSocket handshake: Unexpected response code: 404

我正在尝试将 ActionCable 和 Rails 5 部署到生产服务器 (DigitalOcean)。我已按照此处 Gorails 视频中提到的所有步骤进行操作:https://gorails.com/episodes/deploy-actioncable-and-rails-5

该应用程序是使用 Phusion Passenger + Nginx + Capistrano 部署的。

当我尝试在生产环境中检查我的网站时,我在浏览器的控制台上收到了这个错误:

WebSocket connection to 'ws://139.59.175.34/cable' failed: Error during WebSocket handshake: Unexpected response code: 404

这些是我的设置:

/etc/nginx/sites-enabled/default

server {
        listen 80;
        listen [::]:80 ipv6only=on;

        server_name my_server_domain;
        passenger_enabled on;
        rails_env    production;
        root         /home/deploy/my_app_domain/current/public;

        # ActionCabel config (disable this if u r not using it)
        location /cable {
           passenger_app_group_name actioncable_websocket;
           passenger_force_max_concurrent_requests_per_process 0;
        }

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

production.rb

config.action_cable.url = "/cable"
config.action_cable.allowed_request_origins = ["http://139.59.175.34"]

cable.js

(function() {
  this.App || (this.App = {});

  App.cable = ActionCable.createConsumer("/cable");

}).call(this);

我已尝试将 config/production.rb 上的 ActionCable 配置更改为:

  config.action_cable.url = [/ws:\/\/*/, /wss:\/\/*/]
  config.action_cable.allowed_request_origins = [/http:\/\/*/, /https:\/\/*/]

但还是没有运气。


我也查看了服务器上的 production.log,这是记录的错误:

WebSocket error occurred: One or more reserved bits are on: reserved1 = 1, reserved2 = 0, reserved3 = 0

更新:

以下是服务器上的防火墙设置:

您的配置看起来是正确的。

如果您有防火墙 运行,可能会发生此问题。

检查您的 iptables 配置 (linux) 是否有什么东西阻止了它(不过我没有特定的代码行可以提供给您)。

如果您在 Windows,请检查是否没有 "antivirus" 或 "firewall" 拦截/阻塞位。我听说 Node32 非常适合阻止 websockets。

更新:您是否也可以尝试使用 wss://139.59.175.34/cable 而不是 ws://139.59.175.34/cable 进行连接? (确保您的 NGinx 使用 SSL 代理)。 https://docs.nginx.com/nginx/admin-guide/security-controls/securing-http-traffic-upstream/

我终于找到了问题的答案。事实证明,我的生产服务器上没有安装 redis。所以我只需要安装redis,现在一切正常!

要检查你的服务器是否安装了redis,使用这个命令:

redis-cli

如果出现 redis 控制台,则一切就绪。如果出现error/warning,可以按照以下步骤安装和运行 redis:

  1. sudo apt install redis-server
  2. redis-server

希望对您有所帮助!