mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-23 17:16:40 +01:00
Working on a fix for proxy related to setting Host header (cf. #874)
Also see https://forum.caddyserver.com/t/caddy-0-9-beta-version-available-updated-beta-2/146/29?u=matt which has another account of strange proxy behavior; I think this resolves that.
This commit is contained in:
parent
80dd95a495
commit
a1bc94e409
1 changed files with 15 additions and 19 deletions
|
@ -101,16 +101,26 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
rr.Replacer.Set("upstream", host.Name)
|
||||
}
|
||||
|
||||
// for now, assume the backend's hostname is just a hostname; we'll
|
||||
// handle extra information like scheme later
|
||||
outreq.Host = host.Name
|
||||
proxy := host.ReverseProxy
|
||||
|
||||
// a backend's name may contain more than just the host,
|
||||
// so we parse it as a URL to try to isolate the host.
|
||||
if nameURL, err := url.Parse(host.Name); err == nil {
|
||||
outreq.Host = nameURL.Host
|
||||
if proxy == nil {
|
||||
proxy = NewSingleHostReverseProxy(nameURL, host.WithoutPathPrefix)
|
||||
}
|
||||
} else {
|
||||
outreq.Host = host.Name
|
||||
}
|
||||
if proxy == nil {
|
||||
return http.StatusInternalServerError, errors.New("proxy for host '" + host.Name + "' is nil")
|
||||
}
|
||||
|
||||
// set headers for request going upstream
|
||||
if host.UpstreamHeaders != nil {
|
||||
if replacer == nil {
|
||||
rHost := r.Host
|
||||
replacer = httpserver.NewReplacer(r, nil, "")
|
||||
outreq.Host = rHost
|
||||
}
|
||||
if v, ok := host.UpstreamHeaders["Host"]; ok {
|
||||
outreq.Host = replacer.Replace(v[len(v)-1])
|
||||
|
@ -127,25 +137,11 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|||
var downHeaderUpdateFn respUpdateFn
|
||||
if host.DownstreamHeaders != nil {
|
||||
if replacer == nil {
|
||||
rHost := r.Host
|
||||
replacer = httpserver.NewReplacer(r, nil, "")
|
||||
outreq.Host = rHost
|
||||
}
|
||||
downHeaderUpdateFn = createRespHeaderUpdateFn(host.DownstreamHeaders, replacer)
|
||||
}
|
||||
|
||||
// a backend's name may contain more than just the host,
|
||||
// so we parse it as a URL so we can isolate the host.
|
||||
proxy := host.ReverseProxy
|
||||
if nameURL, err := url.Parse(outreq.Host); err == nil {
|
||||
outreq.Host = nameURL.Host
|
||||
if proxy == nil {
|
||||
proxy = NewSingleHostReverseProxy(nameURL, host.WithoutPathPrefix)
|
||||
}
|
||||
} else if proxy == nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
// tell the proxy to serve the request
|
||||
atomic.AddInt64(&host.Conns, 1)
|
||||
backendErr := proxy.ServeHTTP(w, outreq, downHeaderUpdateFn)
|
||||
|
|
Loading…
Reference in a new issue