我在使用这些日志消息连接到非开发环境中的websocket时遇到问题
Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: close, HTTP_UPGRADE: )
Finished "/cable/"[non-WebSocket] for 127.0.0.1 at 2016-07-06 09:44:29 +1000
我稍微调试了一下,发现浏览器发送的请求/ javascript与unicorn收到的请求(与nginx一起运行)不完全相同。
浏览器的请求标头是
GET ws://cc-uat.com.au/cable HTTP/1.1
Host: cc-uat.com.au
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
Origin: http://cc-uat.com.au
Sec-WebSocket-Version: 13
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
Cookie: <Lot of cookies>
Sec-WebSocket-Key: QGdJkYIA2u7vtmMVXfHKtQ==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Protocol: actioncable-v1-json, actioncable-unsupported
此处的连接是&#39;升级&#39;但是websocket请求有关闭&#39;(可能是nginx弄乱了吗?)
websocket驱动程序中的这段代码失败了
def self.websocket?(env)
connection = env['HTTP_CONNECTION'] || ''
upgrade = env['HTTP_UPGRADE'] || ''
env['REQUEST_METHOD'] == 'GET' and
connection.downcase.split(/ *, */).include?('upgrade') and
upgrade.downcase == 'websocket'
end
更新
这是我的nginx配置
upstream app {
server unix:/home/osboxes/sites/actioncable-examples/shared/sockets/unicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name localhost;
root /home/osboxes/sites/actioncable-example/public;
try_files $uri/index.html $uri @app;
location @app {
proxy_pass http://app;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
我已在/ cable
上安装了actioncable服务器mount ActionCable.server => "/cable"
通过nginx更改,我能够成功进行握手,但服务器无法发送心跳并且连接不断下降。
Started GET "/cable" for 127.0.0.1 at 2016-07-07 05:48:06 +0100
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2016-07-07 05:48:06 +0100
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: upgrade, HTTP_UPGRADE: websocket)
答案 0 :(得分:2)
Have you set the config.action_cable.allowed_request_origins
in production.rb to allow connections from your production domain?
In my nginx.conf there is also
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
I am not entirely sure if they are really required, but it works for me.
答案 1 :(得分:0)
Rails 5动作电缆CORS:
在action_cable.rb
中创建一个ruby文件,即my_rails_project/config/initializers
,并添加以下代码。
if Rails.env.development?
Rails.application.config.action_cable.allowed_request_origins = ['http://localhost:3001', 'http://127.0.0.1:3001']
end
你已经完成了。
答案 2 :(得分:0)
由于above answer,我遇到了类似的错误。问题是我将后端URL地址保留在config.action_cable.allowed_request_origins
中,因为将其更改为前端URL问题已解决。更清楚地说,我在完全不同的域中具有font-end,backend