diff --git a/caddyconfig/httpcaddyfile/serveroptions.go b/caddyconfig/httpcaddyfile/serveroptions.go index 598d2153b..b5aa5b7ed 100644 --- a/caddyconfig/httpcaddyfile/serveroptions.go +++ b/caddyconfig/httpcaddyfile/serveroptions.go @@ -42,6 +42,7 @@ type serverOptions struct { ReadHeaderTimeout caddy.Duration WriteTimeout caddy.Duration IdleTimeout caddy.Duration + KeepAliveDisable bool KeepAliveInterval caddy.Duration KeepAliveIdle caddy.Duration KeepAliveCount int @@ -145,6 +146,13 @@ func unmarshalCaddyfileServerOptions(d *caddyfile.Dispenser) (any, error) { return nil, d.Errf("unrecognized timeouts option '%s'", d.Val()) } } + + case "keepalive_disable": + if d.NextArg() { + return nil, d.ArgErr() + } + serverOpts.KeepAliveDisable = true + case "keepalive_interval": if !d.NextArg() { return nil, d.ArgErr() @@ -331,6 +339,7 @@ func applyServerOptions( server.ReadHeaderTimeout = opts.ReadHeaderTimeout server.WriteTimeout = opts.WriteTimeout server.IdleTimeout = opts.IdleTimeout + server.KeepAliveDisable = opts.KeepAliveDisable server.KeepAliveInterval = opts.KeepAliveInterval server.KeepAliveIdle = opts.KeepAliveIdle server.KeepAliveCount = opts.KeepAliveCount diff --git a/modules/caddyhttp/app.go b/modules/caddyhttp/app.go index 502655cce..408dd0530 100644 --- a/modules/caddyhttp/app.go +++ b/modules/caddyhttp/app.go @@ -536,7 +536,7 @@ func (app *App) Start() error { // create the listener for this socket lnAny, err := listenAddr.Listen(app.ctx, portOffset, net.ListenConfig{ KeepAliveConfig: net.KeepAliveConfig{ - Enable: srv.KeepAliveInterval >= 0 || srv.KeepAliveIdle >= 0 || srv.KeepAliveCount >= 0, + Enable: !srv.KeepAliveDisable, Interval: time.Duration(srv.KeepAliveInterval), Idle: time.Duration(srv.KeepAliveIdle), Count: srv.KeepAliveCount, diff --git a/modules/caddyhttp/server.go b/modules/caddyhttp/server.go index 9cd2389d3..953712851 100644 --- a/modules/caddyhttp/server.go +++ b/modules/caddyhttp/server.go @@ -74,18 +74,29 @@ type Server struct { // 5m is applied to help avoid resource exhaustion. IdleTimeout caddy.Duration `json:"idle_timeout,omitempty"` + // KeepAliveDsiable disables TCP keepalive packets. + // If set, the other TCP KeepAlive settings have no effect. + // KeepAlive is enabled by default. + KeepAliveDisable bool `json:"keepalive_disable,omitempty"` + // KeepAliveInterval is the interval at which TCP keepalive packets // are sent to keep the connection alive at the TCP layer when no other - // data is being transmitted. The default is 15s. + // data is being transmitted. + // If zero, the default is 15s. + // If negative, underlying socket value is unchanged. KeepAliveInterval caddy.Duration `json:"keepalive_interval,omitempty"` // KeepAliveIdle is the time that the connection must be idle before // the first TCP keep-alive probe is sent when no other data is being - // transmitted. The default is 15s. + // transmitted. + // If zero, the default is 15s. + // If negative, underlying socket value is unchanged. KeepAliveIdle caddy.Duration `json:"keepalive_idle,omitempty"` // KeepAliveCount is the maximum number of TCP keep-alive probes that - // should be sent before dropping a connection. The default is 9. + // should be sent before dropping a connection. + // If zero, the default is 9. + // If negative, underlying socket value is unchanged. KeepAliveCount int `json:"keepalive_count,omitempty"` // MaxHeaderBytes is the maximum size to parse from a client's