Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverseproxy: Stop following redirects in health checks by default, add health_follow_redirects #6302

Merged
merged 6 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions modules/caddyhttp/reverseproxy/caddyfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
// health_timeout <duration>
// health_status <status>
// health_body <regexp>
// health_follow_redirects
// health_headers {
// <field> [<values...>]
// }
Expand Down Expand Up @@ -450,6 +451,18 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
}
h.HealthChecks.Active.ExpectBody = d.Val()

case "health_follow_redirects":
aliasgar55 marked this conversation as resolved.
Show resolved Hide resolved
if d.NextArg() {
return d.ArgErr()
}
if h.HealthChecks == nil {
h.HealthChecks = new(HealthChecks)
}
if h.HealthChecks.Active == nil {
h.HealthChecks.Active = new(ActiveHealthChecks)
}
h.HealthChecks.Active.FollowRedirects = true

case "health_passes":
if !d.NextArg() {
return d.ArgErr()
Expand Down
11 changes: 10 additions & 1 deletion modules/caddyhttp/reverseproxy/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@
// HTTP headers to set on health check requests.
Headers http.Header `json:"headers,omitempty"`

// Whether to follow HTTP redirects in response to active health checks (default off).
FollowRedirects bool `json:"follow_redirects,omitempty"`

// How frequently to perform active health checks (default 30s).
Interval caddy.Duration `json:"interval,omitempty"`

Expand Down Expand Up @@ -153,6 +156,12 @@
a.httpClient = &http.Client{
Timeout: timeout,
Transport: h.Transport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if !a.FollowRedirects {
return http.ErrUseLastResponse

Check failure on line 161 in modules/caddyhttp/reverseproxy/healthchecks.go

View workflow job for this annotation

GitHub Actions / lint (mac)

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/caddyserver/caddy/v2/cmd) -s prefix(github.com/caddyserver/caddy) --custom-order (gci)
aliasgar55 marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
},
}

for _, upstream := range h.Upstreams {
Expand Down Expand Up @@ -453,7 +462,7 @@
markUnhealthy()
return nil
}
} else if resp.StatusCode < 200 || resp.StatusCode >= 400 {
} else if resp.StatusCode < 200 || resp.StatusCode >= 300 {
h.HealthChecks.Active.logger.Info("status code out of tolerances",
zap.Int("status_code", resp.StatusCode),
zap.String("host", hostAddr),
Expand Down
Loading