我正在试验 ActionCable(主要是复制 DHH example)并尝试让它在具有瘦(端口 8443)和 nginx 的 Ubuntu 服务器上运行。它在本地一切正常,但是,当我尝试在实时服务器上代理它时,我收到此错误响应:失败:WebSocket 握手期间出错:意外响应代码:301
。
这是我的 nginx 配置的相关部分:
server {
listen 80;
server_name _not_;
root /home/ubuntu/www/;
}
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream websocket {
server 127.0.0.1:8443;
}
server {
listen 80;
...
location /websocket/ {
proxy_pass http://127.0.0.1:8443;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
}
...
}
我有点不适应这里的 nginx -- 我错过了什么或错了什么?
最佳答案
一个月后我回过头来发现问题不在 nginx 配置上,而在 thin 上。我做了三件事:
(1) 配置精简使用the Faye adapter :
# config.ru
require 'faye'
Faye::WebSocket.load_adapter('thin')
require ::File.expand_path('../config/environment', __FILE__)
use Faye::RackAdapter, mount: '/faye', timeout: 25
run Rails.application
(2) 切换到在 routes.rb
中安装 ActionCable,而不是尝试运行它 as a standalone .
#routes.rb
MyAwesomeApp::Application.routes.draw do
...
match "/websocket", to: ActionCable.server, via: [:get, :post]
end
(3) 返回到我的正常 nginx 配置,上游的 websockets 很薄(就像网络服务器一样:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close; }
upstream thin {
server 127.0.0.1:3000;
}
server {
...
location /websocket {
proxy_pass http://thin;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade; }
...
}
所以,我向 nginx 道歉,我为你开脱了——看来问题主要与瘦有关。
编辑:我添加了我在路由中安装后返回的旧 nginx 配置。同样值得注意的是,对于那些使用 SSL 的人来说,config.force_ssl
将破坏安全的 wss
websocket。相反,您应该在 Controller 级别执行 force_ssl
,as recommended here ,并配置 nginx 以将任何 HTTP 路由重写为 HTTPS。
关于ruby-on-rails - 配置 nginx 以代理 thin 和 Rails ActionCable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32322652/