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: Keep path to unix socket as dial address (#4232)
elnoro committed
Jul 7, 2021 at 08:43 UTC
d882211080cbb97f5e1cfdd87be6874eb1a9430a
2 files changed
+56
-1
caddytest/integration/reverseproxy_test.go
+54
@@ -436,3 +436,57 @@ func TestReverseProxyHealthCheckUnixSocket(t *testing.T) {
436
437
tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!")
438
}
439
+
440
+func TestReverseProxyHealthCheckUnixSocketWithoutPort(t *testing.T) {
441
+ if runtime.GOOS == "windows" {
442
+ t.SkipNow()
443
+ }
444
+ tester := caddytest.NewTester(t)
445
+ f, err := ioutil.TempFile("", "*.sock")
446
+ if err != nil {
447
+ t.Errorf("failed to create TempFile: %s", err)
448
+ return
449
+ }
450
+ // a hack to get a file name within a valid path to use as socket
451
+ socketName := f.Name()
452
+ os.Remove(f.Name())
453
+
454
+ server := http.Server{
455
+ Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
456
+ if strings.HasPrefix(req.URL.Path, "/health") {
457
+ w.Write([]byte("ok"))
458
+ return
459
+ }
460
+ w.Write([]byte("Hello, World!"))
461
+ }),
462
+ }
463
+
464
+ unixListener, err := net.Listen("unix", socketName)
465
+ if err != nil {
466
+ t.Errorf("failed to listen on the socket: %s", err)
467
+ return
468
+ }
469
+ go server.Serve(unixListener)
470
+ t.Cleanup(func() {
471
+ server.Close()
472
+ })
473
+ runtime.Gosched() // Allow other goroutines to run
474
+
475
+ tester.InitServer(fmt.Sprintf(`
476
+ {
477
+ http_port 9080
478
+ https_port 9443
479
+ }
480
+ http://localhost:9080 {
481
+ reverse_proxy {
482
+ to unix/%s
483
+
484
+ health_path /health
485
+ health_interval 2s
486
+ health_timeout 5s
487
+ }
488
+ }
489
+ `, socketName), "caddyfile")
490
+
491
+ tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!")
492
+}
modules/caddyhttp/reverseproxy/healthchecks.go
+2
-1
@@ -189,13 +189,14 @@ func (h *Handler) doActiveHealthCheckForAllHosts() {
189
return
190
}
191
hostAddr := addr.JoinHostPort(0)
192
+ dialAddr := hostAddr
193
if addr.IsUnixNetwork() {
194
// this will be used as the Host portion of a http.Request URL, and
195
// paths to socket files would produce an error when creating URL,
196
// so use a fake Host value instead; unix sockets are usually local
197
hostAddr = "localhost"
198
}
198
- err = h.doActiveHealthCheck(DialInfo{Network: addr.Network, Address: hostAddr}, hostAddr, upstream.Host)
199
+ err = h.doActiveHealthCheck(DialInfo{Network: addr.Network, Address: dialAddr}, hostAddr, upstream.Host)
200
if err != nil {
201
h.HealthChecks.Active.logger.Error("active health check failed",
202
zap.String("address", hostAddr),