8

您好,我正在尝试使用 ror 5.0.0 beta(带有 puma)在生产模式下进行简单的聊天(在 localhost 中没有问题)。

这是我的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

生产: url: redis://localhost:6379/1

开发: url: redis://localhost:6379/2

测试: url: redis://localhost:6379/3

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

这是我收到的错误:

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

有小费吗?:) 谢谢

4

3 回答 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 选项,它就会起作用。-)

感谢大家的帮助

于 2016-02-08T12:34:26.883 回答
1

您的 websocket URI is /cable/and not /cable,因此后者会遇到问题location /。尝试:

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

另外,不确定您是否需要break;在那里。}我认为这两个块之间的缺失location只是问题中的一个错字。

EDIT1:添加rewrite以恢复正确的上游映射。

EDIT2:替代解决方案是明确重写/cable/cable/

location = /cable { rewrite ^ /cable/ last; }
location /cable/ { 
    proxy_pass http://websocket/;
    ...
}
于 2016-01-27T16:07:54.247 回答
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"
于 2016-01-29T23:58:29.787 回答