@marcel91200 / caddy Mirror · read-only / commits / 3c9256a1
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

reverseproxy: Caddyfile health check headers, host header support (#3948)

* reverse_proxy: 1.health check headers can be set through Caddyfile using health_headers directive; 2.health check header host can be set properly * reverse_proxy: replace example with syntax definition inline health_headers directive parse function * bugfix: change caddyfile_adapt testcase file from space to tab * reverseproxy: modify health_header value document as optional and add more test cases

yaxin committed Jan 5, 2021 at 02:26 UTC 3c9256a1bea79a542500c8d07a9fd06fa806a79e
3 files changed +86 -1
caddytest/integration/caddyfile_adapt/reverse_proxy_health_headers.txt new
+57
@@ -0,0 +1,57 @@
1 +:8884
2 +
3 +reverse_proxy 127.0.0.1:65535 {
4 + health_headers {
5 + Host example.com
6 + X-Header-Key 95ca39e3cbe7
7 + X-Header-Keys VbG4NZwWnipo 335Q9/MhqcNU3s2TO
8 + X-Empty-Value
9 + }
10 +}
11 +----------
12 +{
13 + "apps": {
14 + "http": {
15 + "servers": {
16 + "srv0": {
17 + "listen": [
18 + ":8884"
19 + ],
20 + "routes": [
21 + {
22 + "handle": [
23 + {
24 + "handler": "reverse_proxy",
25 + "health_checks": {
26 + "active": {
27 + "headers": {
28 + "Host": [
29 + "example.com"
30 + ],
31 + "X-Empty-Value": [
32 + ""
33 + ],
34 + "X-Header-Key": [
35 + "95ca39e3cbe7"
36 + ],
37 + "X-Header-Keys": [
38 + "VbG4NZwWnipo",
39 + "335Q9/MhqcNU3s2TO"
40 + ]
41 + }
42 + }
43 + },
44 + "upstreams": [
45 + {
46 + "dial": "127.0.0.1:65535"
47 + }
48 + ]
49 + }
50 + ]
51 + }
52 + ]
53 + }
54 + }
55 + }
56 + }
57 +}
modules/caddyhttp/reverseproxy/caddyfile.go
+23
@@ -63,6 +63,9 @@ func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error)
63 // health_timeout <duration>
64 // health_status <status>
65 // health_body <regexp>
66 +// health_headers {
67 +// <field> [<values...>]
68 +// }
69 //
70 // # passive health checking
71 // max_fails <num>
@@ -313,6 +316,26 @@ func (h *Handler) UnmarshalCaddyfile(d *caddyfile.Dispenser) error {
316 }
317 h.HealthChecks.Active.Port = portNum
318
319 + case "health_headers":
320 + healthHeaders := make(http.Header)
321 + for d.Next() {
322 + for d.NextBlock(0) {
323 + key := d.Val()
324 + values := d.RemainingArgs()
325 + if len(values) == 0 {
326 + values = append(values, "")
327 + }
328 + healthHeaders[key] = values
329 + }
330 + }
331 + if h.HealthChecks == nil {
332 + h.HealthChecks = new(HealthChecks)
333 + }
334 + if h.HealthChecks.Active == nil {
335 + h.HealthChecks.Active = new(ActiveHealthChecks)
336 + }
337 + h.HealthChecks.Active.Headers = healthHeaders
338 +
339 case "health_interval":
340 if !d.NextArg() {
341 return d.ArgErr()
modules/caddyhttp/reverseproxy/healthchecks.go
+6 -1
@@ -26,6 +26,7 @@ import (
26 "regexp"
27 "runtime/debug"
28 "strconv"
29 + "strings"
30 "time"
31
32 "github.com/caddyserver/caddy/v2"
@@ -241,7 +242,11 @@ func (h *Handler) doActiveHealthCheck(dialInfo DialInfo, hostAddr string, host H
242 return fmt.Errorf("making request: %v", err)
243 }
244 for key, hdrs := range h.HealthChecks.Active.Headers {
244 - req.Header[key] = hdrs
245 + if strings.ToLower(key) == "host" {
246 + req.Host = h.HealthChecks.Active.Headers.Get(key)
247 + } else {
248 + req.Header[key] = hdrs
249 + }
250 }
251
252 // do the request, being careful to tame the response body