ruby-on-rails - Actioncable Nginx 和 Puma WebSocket 握手 : Unexpected response

标签 ruby-on-rails ruby nginx puma actioncable

我正在尝试使用 Rails 5、Nginx 和 Puma 配置服务器。应用程序运行良好,但 Actioncable 给出

WebSocket connection to 'ws://server_name.com/cable' failed:
Error during WebSocket handshake: Unexpected response code: 200

下面是我的 nginx 设置,

upstream app {
  server unix:/tmp/app.sock fail_timeout=0;
}

server {
    listen       80;
    server_name  server_name.com;
    try_files $uri/index.html $uri @app;
    client_max_body_size 100M;
    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_pass http://app;
      client_max_body_size 10M;
    }

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

    location ~ ^/(assets|uploads)/ {
        root assets_path;
        gzip_static on;
        expires max;
        add_header Cache-Control public;
        add_header ETag "";
        break;
    }
    error_page   500 502 503 504  /500.html;
 }

在 production.rb 中的 Rails 中,我进行了如下设置。

config.action_cable.url = 'ws://server_name.com/cable'

任何帮助将不胜感激。

最佳答案

尝试添加:

config.action_cable.allowed_request_origins = ['*']
config.action_cable.disable_request_forgery_protection = true

到您的config/environments/Production.rb文件

它适用于我,你可以查看this link还。 还有这个question

关于ruby-on-rails - Actioncable Nginx 和 Puma WebSocket 握手 : Unexpected response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39292968/

相关文章:

ruby-on-rails - 测试 'save' 函数在传递无效数据时失败。 ( ruby Rspec)

ruby-on-rails - Rails 3 - 引擎和插件之间的区别

ruby-on-rails - 如何使用 Amazon SES 设置 Ruby on Rails Action 邮箱?

nginx - Docker + Nginx + ngx_http_image_filter_module

ruby-on-rails - rails 从 url 中删除 Controller 路径

php - 在 PHP 中对 API 的 cURL POST 请求不返回任何值

ruby-on-rails - 如何配置 Ruby on Rails 应用程序以使用佛历?

apache - 将 Laravel 项目从 Apache 迁移到 Nginx

php - Nginx 重写 CodeIgniter 应用程序主 CodeIgniter 应用程序子目录中的配置

ruby - 将 block 常量复制到模块中