RoR 5.0.0 ActionCable wss WebSocket 握手:意外响应代码:301

问题描述 投票:0回答:3

您好,我正在尝试使用 ror 5.0.0 beta(使用 puma)提供简单的聊天服务 在生产模式下工作(在本地主机中没有问题)。

这是我的Nginx配置:

upstream websocket {
    server 127.0.0.1:28080;
}


server {

    listen 443;
    server_name mydomain;
    ssl_certificate ***/server.crt;
    ssl_certificate_key ***/server.key;
    ssl on;
    ssl_session_cache builtin:1000 shared:SSL:10m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 
HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;
    access_log /var/log/nginx/jenkins.access.log;

    location / {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_pass http://localhost:3000;
      proxy_read_timeout 90;

      proxy_redirect http://localhost:3000 https://mydomain;


    location /cable/{
        proxy_pass         http://websocket/;
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection "upgrade";
        proxy_set_header   Host $http_host;
        break;
    }

   }

这是config/redis/cable.yml

生产: 网址:redis://localhost:6379/1

发展: 网址:redis://localhost:6379/2

测试: 网址:redis://localhost:6379/3

config /环境/生产.rb

  # Action Cable endpoint configuration
  config.action_cable.url = 'wss://mydomain/cable'
  # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]

  # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
  config.force_ssl = false

这是我收到的错误:

application-[...].js:27 WebSocket 连接到“wss://mydomain/cable”失败:WebSocket 握手期间出错:意外的响应代码:301

有什么建议吗? :) 谢谢

nginx websocket ruby-on-rails-5 puma actioncable
3个回答
3
投票

解决了添加phusion乘客。

nginx 配置现在是:

server{
listen 80;
passenger_enabled on;
passenger_app_env production;
passenger_ruby /../ruby-2.3.0/ruby;
root /path to application/public;
client_max_body_size 4G;
keepalive_timeout 10;
[...]

   location /cable{
        passenger_app_group_name websocket;
        passenger_force_max_concurrent_requests_per_process 0;
   } 
}

您必须删除默认文件夹 config/redis/cable.yml 并将该文件仅移至 /config。

对于 SSL,只需启用默认 ssl 选项即可工作。-)

谢谢大家的帮助


1
投票

您的 websocket URI 是

/cable/
而不是
/cable
,因此后者将命中
location /
块。尝试:

location /cable { 
    rewrite ^/cable$ / break;
    rewrite ^/cable(.*)$ $1 break;
    proxy_pass         http://websocket;
    ...
}

另外,不确定你是否需要

break;
在那里。我认为两个
}
块之间缺少的
location
只是问题中的拼写错误。

编辑1:添加

rewrite
以恢复正确的上游映射。

EDIT2:替代解决方案是将

/cable
显式重写为
/cable/
,如下所示:

location = /cable { rewrite ^ /cable/ last; }
location /cable/ { 
    proxy_pass http://websocket/;
    ...
}

0
投票

我昨天花了近 5 个小时试图解决这个特殊问题。我最终为 Websocket 连接使用了一个名为

ws.example.com
的单独域,因为其他所有内容都导致了
301 redirect

这是我的

nginx.conf
文件。我已经删除了 SSL 部分,但您可以插入自己的部分。请注意,您需要 nginx 1.4+,因为此版本之前的所有内容都不支持 websocket 代理。

upstream socket {
  server unix:/mysocket fail_timeout=0;
}

upstream websocket {
    server 127.0.0.1:28080;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

server {
  listen       443 ssl;
  server_name  ws.example.com;
  access_log off;

  # SSL configs here

  location / {
      proxy_pass http://websocket/;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection $connection_upgrade;
  }
}

server {
  listen       443 ssl;
  server_name  example.com;

  # SSL configs here

  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://socket;
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

我在某处读到

allowed_request_origins
没有按预期工作,所以我采取了安全的方式(直到错误修复)并打开了完全使用
ActionCable.server.config.disable_request_forgery_protection = true
的检查器。

这是我的

cable.ru
用于启动操作电缆的文件。

require ::File.expand_path('../../config/environment', __FILE__)
Rails.application.eager_load!

require 'action_cable/process/logging'

Rails.logger.level = 0
ActionCable.server.config.disable_request_forgery_protection = true

run ActionCable.server

我也在使用 Github 上的最新 Rails 版本。

gem "rails", github: "rails/rails"
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.