我想要通过 curl http://127.0.0.1:1234/https://www.baidu.com 来访问Caddy监听的端口时,Caddy可以自动解析并反代URL后面的目标网站 https://www.baidu.com
于是我写了一份配置初步实现了这个功能,可以正常解析出目标URL了并反代,但是在反代时遇到了点问题不及预期:它在反代去请求时使用的是http协议访问的http://www.baidu.com 而不是https协议(我抓包看到的),导致无法访问。
但是我通过respond {backend}看了backend变量的值的内容的确就是https协议https://www.baidu.com;哪位大佬帮忙看看是啥原因,和ChatGPT聊了半天它也不知道原因
:1234 {
log {
output stdout
format console
}
@parsePath path_regexp target ^/((http|https):/+([^/]+))(/.*)?$
handle @parsePath {
# 使用map指令动态设置变量
map {http.regexp.target.2} {url_scheme} {
default {http.regexp.target.2}
}
map {http.regexp.target.3} {url_host} {
default {http.regexp.target.3}
}
map {url_host} {backend} {
default {url_scheme}://{url_host}
}
rewrite {http.regexp.target.4}
# respond {backend}
reverse_proxy {backend} {
header_up Host {url_host}
header_up X-Forwarded-Host {url_host}
header_up X-Forwarded-Proto {url_scheme}
}
}
handle {
respond "URL格式不正确或不支持的请求" 401
}
}