diff --git a/docs/02.Tutorials/2.6.Websocket.md b/docs/02.Tutorials/2.6.Websocket.md index 28ab9c7ceb..8622d1f73b 100644 --- a/docs/02.Tutorials/2.6.Websocket.md +++ b/docs/02.Tutorials/2.6.Websocket.md @@ -42,6 +42,9 @@ filters: pools: - servers: - url: ws://127.0.0.1:12345 + # keepHost: true, the `Host` will be the same as the original request + # If the backend is a load balancer, it would prove to be highly beneficial + keepHost: true - url: ws://127.0.0.1:9095 - url: ws://127.0.0.1:9096 - url: ws://127.0.0.1:9097 diff --git a/docs/07.Reference/7.02.Filters.md b/docs/07.Reference/7.02.Filters.md index 476db4246d..b8620cb552 100644 --- a/docs/07.Reference/7.02.Filters.md +++ b/docs/07.Reference/7.02.Filters.md @@ -359,6 +359,9 @@ name: proxy-example-1 pools: - servers: - url: ws://127.0.0.1:9095 + # keepHost: true, the `Host` will be the same as the original request + # If the backend is a load balancer, it would prove to be highly beneficial + keepHost: true - url: ws://127.0.0.1:9096 - url: ws://127.0.0.1:9097 loadBalance: @@ -2165,4 +2168,4 @@ template: | | Name | Type | Description | Required | |------|------|-------------|----------| | header | [httpheader.AdaptSpec](#httpheaderAdaptSpec) | Rules to revise request header | No | -| body | string | If provided the body of the original request is replaced by the value of this option. | No | \ No newline at end of file +| body | string | If provided the body of the original request is replaced by the value of this option. | No | diff --git a/pkg/filters/proxies/httpproxy/wspool.go b/pkg/filters/proxies/httpproxy/wspool.go index 24f02bd7df..2e65ee5113 100644 --- a/pkg/filters/proxies/httpproxy/wspool.go +++ b/pkg/filters/proxies/httpproxy/wspool.go @@ -123,6 +123,12 @@ func (sp *WebSocketServerPool) dialServer(svr *Server, req *httpprot.Request) (* CompressionMode: websocket.CompressionDisabled, } + // only set host when server address is not host name OR + // server is explicitly told to keep the host of the request. + if !svr.AddrIsHostName || svr.KeepHost { + opts.Host = req.Host() + } + opts.HTTPHeader.Del("Sec-WebSocket-Origin") opts.HTTPHeader.Del("Sec-WebSocket-Protocol") opts.HTTPHeader.Del("Sec-WebSocket-Accept")