Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 13:15 UTC
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 <joancena1268@mail.com>
Ali Asgar committed
May 7, 2024 at 20:10 UTC
b2b29dcd4956c9fa722daf6d31fc22aea74ed4fb
2 files changed
+23
-1
modules/caddyhttp/reverseproxy/caddyfile.go
+13
@@ -75,6 +75,7 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
75
// health_timeout <duration>
76
// health_status <status>
77
// health_body <regexp>
78
+// health_follow_redirects
79
// health_headers {
80
// <field> [<values...>]
81
// }
@@ -450,6 +451,18 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
451
}
452
h.HealthChecks.Active.ExpectBody = d.Val()
453
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
+
466
case "health_passes":
467
if !d.NextArg() {
468
return d.ArgErr()
modules/caddyhttp/reverseproxy/healthchecks.go
+10
-1
@@ -82,6 +82,9 @@ type ActiveHealthChecks struct {
82
// HTTP headers to set on health check requests.
83
Headers http.Header `json:"headers,omitempty"`
84
85
+ // Whether to follow HTTP redirects in response to active health checks (default off).
86
+ FollowRedirects bool `json:"follow_redirects,omitempty"`
87
+
88
// How frequently to perform active health checks (default 30s).
89
Interval caddy.Duration `json:"interval,omitempty"`
90
@@ -153,6 +156,12 @@ func (a *ActiveHealthChecks) Provision(ctx caddy.Context, h *Handler) error {
156
a.httpClient = &http.Client{
157
Timeout: timeout,
158
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
+ },
165
}
166
167
for _, upstream := range h.Upstreams {
@@ -453,7 +462,7 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, upstre
462
markUnhealthy()
463
return nil
464
}
456
- } else if resp.StatusCode < 200 || resp.StatusCode >= 400 {
465
+ } else if resp.StatusCode < 200 || resp.StatusCode >= 300 {
466
h.HealthChecks.Active.logger.Info("status code out of tolerances",
467
zap.Int("status_code", resp.StatusCode),
468
zap.String("host", hostAddr),