经验不足,遇到悲剧,我们测试环境下是走的http
,所以我写的websocket
没用加ssl
,调试着没问题,但是上线才发现https
下不支持ws
,必须都换成wss
。
Mixed Content: The page at 'https://xxx' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://xxx/websocket/?rid=18'. This request has been blocked; this endpoint must be available over WSS.
两个思路:
一是让https
请求中允许使用ws
https://developer.mozilla.org/zh-CN/docs/Web/Security/CSP/CSP_policy_directives
我尝试在这个页面里添加Content-Security-Policy
头信息
header('Content-Security-Policy: connect-src *;');
测试发现还是不行。
顺便记录下安卓里webview里https
不显示http
链接的图片也是同样的原理。
原生可以设置
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
页面可以设置
header('Content-Security-Policy: img-src *;');
二是配置成wss
我想在https web
服务器上对websocket
后端服务器上做代理
location ^~ /websocket-request/ { proxy_pass http://xxx.55.75.xx:8080/; proxy_redirect off; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; }
但是一直都是返回504,我想难道是因为这是后端服务器,前面还有nginx
做负载均衡,所以这里指定了Upgrade
,而前端nginx
没配置该头信息,所以消息无法回传,握手无法完成?
抱着这个疑问,我同事在测试服务器的nginx
上添加了同样的配置,能够生效。
难道真的是应该在配置负载均衡的地方代理websocket
才行?