From 86ef3a796c2fc35e7aa06b4fd50a77a44de46356 Mon Sep 17 00:00:00 2001 From: aliasgar Date: Mon, 6 May 2024 10:49:22 +0530 Subject: [PATCH] added health_follow_redirect in active health checks --- cmd/caddy/Caddfile | 10 ---------- modules/caddyhttp/reverseproxy/caddyfile.go | 7 +++++++ modules/caddyhttp/reverseproxy/healthchecks.go | 10 +++++++++- 3 files changed, 16 insertions(+), 11 deletions(-) delete mode 100644 cmd/caddy/Caddfile diff --git a/cmd/caddy/Caddfile b/cmd/caddy/Caddfile deleted file mode 100644 index 8ed672120a9e..000000000000 --- a/cmd/caddy/Caddfile +++ /dev/null @@ -1,10 +0,0 @@ -{ - debug -} - -localhost:3000 { - respond "hello world" - reverse_proxy localhost:8003 { - health_uri http://localhost:8003/health - } -} diff --git a/modules/caddyhttp/reverseproxy/caddyfile.go b/modules/caddyhttp/reverseproxy/caddyfile.go index 3bc4de017b3d..84829bdd2eb3 100644 --- a/modules/caddyhttp/reverseproxy/caddyfile.go +++ b/modules/caddyhttp/reverseproxy/caddyfile.go @@ -450,6 +450,12 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { } h.HealthChecks.Active.ExpectBody = d.Val() + case "health_follow_redirects": + if d.NextArg() { + return d.ArgErr() + } + h.HealthChecks.Active.HealthFollowRedirects = true + case "health_passes": if !d.NextArg() { return d.ArgErr() @@ -644,6 +650,7 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error { h.TrustedProxies = append(h.TrustedProxies, d.Val()) } + case "header_up": var err error diff --git a/modules/caddyhttp/reverseproxy/healthchecks.go b/modules/caddyhttp/reverseproxy/healthchecks.go index 651ebfa71b48..a306a79c4d03 100644 --- a/modules/caddyhttp/reverseproxy/healthchecks.go +++ b/modules/caddyhttp/reverseproxy/healthchecks.go @@ -83,6 +83,9 @@ type ActiveHealthChecks struct { // HTTP headers to set on health check requests. Headers http.Header `json:"headers,omitempty"` + // boolean to follow redirects in health checks disabled by default + HealthFollowRedirects bool `json:"health_follow_redirects,omitempty"` + // How frequently to perform active health checks (default 30s). Interval caddy.Duration `json:"interval,omitempty"` @@ -155,7 +158,12 @@ func (a *ActiveHealthChecks) Provision(ctx caddy.Context, h *Handler) error { Timeout: timeout, Transport: h.Transport, CheckRedirect: func(req *http.Request, via []*http.Request) error { - return errors.New("Cannot redirect") + if !a.HealthFollowRedirects { + return errors.New( + "Redirects are disabled in health check, set health_follow_redirects flag in config to avoid this error") + } else { + return nil + } }, }