Nginx RoR 5.0.0 ActionCable wss WebSocket握手:意外响应代码:301
你好,我正在尝试使用ror 5.0.0测试版(与puma一起)提供一个简单的聊天服务 在生产模式下工作(在localhost中没有问题) 这是我的Nginx配置:Nginx RoR 5.0.0 ActionCable wss WebSocket握手:意外响应代码:301,nginx,wss,puma,ruby-on-rails-5,actioncable,Nginx,Wss,Puma,Ruby On Rails 5,Actioncable,你好,我正在尝试使用ror 5.0.0测试版(与puma一起)提供一个简单的聊天服务 在生产模式下工作(在localhost中没有问题) 这是我的Nginx配置: upstream websocket { server 127.0.0.1:28080; } server { listen 443; server_name mydomain; ssl_certificate ***/server.crt; ssl_certificate_key ***
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/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
有什么建议吗?:)谢谢您的websocket URI是
/cable/
而不是/cable
,因此后者将命中位置/
块。尝试:
location /cable {
rewrite ^/cable$ / break;
rewrite ^/cable(.*)$ $1 break;
proxy_pass http://websocket;
...
}
此外,不确定您是否需要中断代码>在那里。我认为两个位置
块之间缺少的}
只是问题中的一个输入错误
EDIT1:添加了重写
,以恢复正确的上游映射
EDIT2:另一种解决方案是将/cable
显式重写为/cable/
,如下所示:
location = /cable { rewrite ^ /cable/ last; }
location /cable/ {
proxy_pass http://websocket/;
...
}
我昨天花了将近5个小时试图解决这个问题。最后,我为websocket连接使用了一个名为ws.example.com
的单独域,因为其他所有内容都导致了301重定向
这是我的nginx.conf
文件。我已经删除了SSL部分,但您可以插入自己的。请注意,您需要nginx1.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;
}
我在某个地方读到允许的\u请求\u来源
没有按预期工作,因此我采取了安全的方法(直到错误被修复),并完全使用ActionCable.server.config.disable\u请求\u伪造\u保护=true
这是我的cable.ru
文件,用于启动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
我还使用了Github的最新rails版本
gem "rails", github: "rails/rails"
我解决了添加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选项,它就可以工作。-)
感谢大家的帮助将位置/cable/{…}更改为/cable return me“WebSocket连接到”wss://mydomain/cable'失败:WebSocket握手期间出错:意外响应代码:502“网关错误-确保服务在端口28080上运行防火墙正在阻止该端口,现在我已将redis切换到4443和“上游websocket”{127.0.0.1:4443}->WebSocket握手时出错:意外响应代码:404至少我的错误代码正在更改xdNot Found-什么是上游URI,您可能已经破坏了从/cable/xxx
到/xxx上游的映射,因为它需要尾部/。我将编辑我的答案。这些配置是否完全替换了问题?还是补充?您是否在production.rb中“启用默认ssl选项”,即config.force\u ssl=true
?