网上有很多方案都不能解决,最后不断测试,找到一个可行的写法。
以例说明,用二级域名分流
#分流地址
tt.abc.com {
#这些是入站协议header里的特征(应该还有其他也可以用),用于判断是websocket,
@ws {
header Connection *Upgrade*
header Upgrade websocket
}
encode gzip
#证书要和分流地址匹配
tls ./abc.com.pem ./abc.com.key
reverse_proxy @ws localhost:61235 {
#转发后端http,取消tls验证
transport http {
tls_insecure_skip_verify
}
}
#不是websocket协议的,走这里
resverse_proxy localhost:61234
}
官方文档中,
By default, Caddy passes thru incoming headers—including Host—to the backend without modifications, with three exceptions:
It sets or augments the X-Forwarded-For header field.
It sets the X-Forwarded-Proto header field.
It sets the X-Forwarded-Host header field.
For these X-Forwarded-* headers, by default, the proxy will ignore their values from incoming requests, to prevent spoofing.
翻译意思是:
默认情况下,Caddy会将传入的头信息(包括Host)无修改地传递给后端,但有三个例外:
它设置或增强了X-Forwarded-For头字段。
它设置了X-Forwarded-Proto头字段。
它设置了X-Forwarded-Host头字段。
对于这些X-Forwarded-*头,代理默认会忽略来自传入请求的它们的值,以防止欺骗。