Skip to content

Commit b2b29dc

Browse files
aliasgar55aliasgar
andauthored
reverseproxy: Implement health_follow_redirects (#6302)
* added health_follow_redirect in active health checks * chore: code format * chore: refactore reversproxy healthcheck redirect variable name and description of the same * chore: formatting * changed reverse proxy health check status code range to be between 200-299 * chore: formatting --------- Co-authored-by: aliasgar <[email protected]>
1 parent c97292b commit b2b29dc

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

modules/caddyhttp/reverseproxy/caddyfile.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
7575
// health_timeout <duration>
7676
// health_status <status>
7777
// health_body <regexp>
78+
// health_follow_redirects
7879
// health_headers {
7980
// <field> [<values...>]
8081
// }
@@ -450,6 +451,18 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
450451
}
451452
h.HealthChecks.Active.ExpectBody = d.Val()
452453

454+
case "health_follow_redirects":
455+
if d.NextArg() {
456+
return d.ArgErr()
457+
}
458+
if h.HealthChecks == nil {
459+
h.HealthChecks = new(HealthChecks)
460+
}
461+
if h.HealthChecks.Active == nil {
462+
h.HealthChecks.Active = new(ActiveHealthChecks)
463+
}
464+
h.HealthChecks.Active.FollowRedirects = true
465+
453466
case "health_passes":
454467
if !d.NextArg() {
455468
return d.ArgErr()

modules/caddyhttp/reverseproxy/healthchecks.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ type ActiveHealthChecks struct {
8282
// HTTP headers to set on health check requests.
8383
Headers http.Header `json:"headers,omitempty"`
8484

85+
// Whether to follow HTTP redirects in response to active health checks (default off).
86+
FollowRedirects bool `json:"follow_redirects,omitempty"`
87+
8588
// How frequently to perform active health checks (default 30s).
8689
Interval caddy.Duration `json:"interval,omitempty"`
8790

@@ -153,6 +156,12 @@ func (a *ActiveHealthChecks) Provision(ctx caddy.Context, h *Handler) error {
153156
a.httpClient = &http.Client{
154157
Timeout: timeout,
155158
Transport: h.Transport,
159+
CheckRedirect: func(req *http.Request, via []*http.Request) error {
160+
if !a.FollowRedirects {
161+
return http.ErrUseLastResponse
162+
}
163+
return nil
164+
},
156165
}
157166

158167
for _, upstream := range h.Upstreams {
@@ -453,7 +462,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
453462
markUnhealthy()
454463
return nil
455464
}
456-
} else if resp.StatusCode < 200 || resp.StatusCode >= 400 {
465+
} else if resp.StatusCode < 200 || resp.StatusCode >= 300 {
457466
h.HealthChecks.Active.logger.Info("status code out of tolerances",
458467
zap.Int("status_code", resp.StatusCode),
459468
zap.String("host", hostAddr),

0 commit comments

Comments
 (0)