Skip to content

Commit

Permalink
chore: refactore reversproxy healthcheck redirect variable name and d…
Browse files Browse the repository at this point in the history
…escription of the same
  • Loading branch information
aliasgar55 committed May 6, 2024
1 parent d15bfc9 commit df8547c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
9 changes: 8 additions & 1 deletion 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 @@ -454,7 +455,13 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
if d.NextArg() {
return d.ArgErr()
}
h.HealthChecks.Active.HealthFollowRedirects = true
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() {
Expand Down
14 changes: 6 additions & 8 deletions modules/caddyhttp/reverseproxy/healthchecks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package reverseproxy

import (
"context"
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -83,8 +82,8 @@ 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"`
// 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 @@ -158,12 +157,11 @@ func (a *ActiveHealthChecks) Provision(ctx caddy.Context, h *Handler) error {
Timeout: timeout,
Transport: h.Transport,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
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
if !a.FollowRedirects {
return fmt.Errorf(
"active health check encountered a redirect; enable 'health_follow_redirects' if redirects are intentional")
}
return nil
},
}

Expand Down

0 comments on commit df8547c

Please sign in to comment.