RoR 5.0.0 ActionCable wss WebSocket handshake: Une

2020-03-15 04:05发布

Hello I'm trying to serve a simple chat using ror 5.0.0 beta (with puma) working on production mode (in localhost there are no problems).

This is my Nginx configuration:

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;
    }

   }

This is config/redis/cable.yml

production: url: redis://localhost:6379/1

development: url: redis://localhost:6379/2

test: url: redis://localhost:6379/3

and config/environments/production.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

And this is the error i'm receiving:

application-[...].js:27 WebSocket connection to 'wss://mydomain/cable' failed: Error during WebSocket handshake: Unexpected response code: 301

Any tips? :) Thanks

3条回答
戒情不戒烟
2楼-- · 2020-03-15 04:40

I spend almost 5 hours yesterday trying to solve this particular problem. I ended up using a separate domain for the websocket connection called ws.example.com as everything else resulted in a 301 redirect.

Here's is my nginx.conf file. I've removed the SSL parts, but you could just insert your own. Note that you need nginx 1.4+ as everything prior to this version doesn't support websocket proxying.

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;
}

I read somewhere that allowed_request_origins didn't work as expected so I went the safe way (until the bug is fixed) and turned the checker of completely using ActionCable.server.config.disable_request_forgery_protection = true.

Here's my cable.ru file for starting action cable.

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

I'm also using the latest rails version from Github.

gem "rails", github: "rails/rails"
查看更多
Deceive 欺骗
3楼-- · 2020-03-15 04:49

I solved adding phusion passenger.

nginx config is now :

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;
   } 
}

You have to remove default folder config/redis/cable.yml and move that file to /config only.

For SSL just enable default ssl options and it will works .-)

Thanks everyone for the help

查看更多
放荡不羁爱自由
4楼-- · 2020-03-15 04:53

Your websocket URI is /cable/ and not /cable, so the latter will hit the location / block. Try:

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

Also, not sure you need a break; in there. I presume the missing } between the two location blocks is just a typo in the question.

EDIT1: Added rewrite to restore correct upstream mapping.

EDIT2: Alternative solution is to explicitly rewrite /cable to /cable/ like this:

location = /cable { rewrite ^ /cable/ last; }
location /cable/ { 
    proxy_pass http://websocket/;
    ...
}
查看更多
登录 后发表回答