我一直在阅读here有关通过cloudfront发行版连接websocket服务与客户端的文档,但我无法理解websocket协议的升级将发生在哪里。升级是否需要由服务器处理,还是由 Cloudfront 发行版处理将协议更改为 wss 并通过连接进行响应?
我的问题的答案是对 Cloudfront Distribution 进行简单的更改。需要添加这些标头,以便我的服务器(使用express/express-ws)能够接收带有正确标头的 http 请求来验证会话并返回升级后的内容客户端的协议响应。 (这是开箱即用的express-ws和rxjs)。如果需要,我可以发布一个编码示例。
我解决了这个问题:
AllViewer
:Cloudfront > Distributions > YourDistributionId > Behaviours > Cache key and origin requests > Origin request policy = AllViewer
(请注意,此分配将所有流量转发到
/yourpath
)
Update
和 Connection
:location /yourpath {
proxy_pass http://localhost:3030/; # the websocket server, in my case node
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}