← All notes · 2026-08-19 · 4 min read

Always Configure All Four Timeouts on net/http.Server

Notes on ReadHeaderTimeout, ReadTimeout, WriteTimeout, and IdleTimeout behind a reverse proxy.

gohttp

Why http.ListenAndServe is unsafe by default

The package-level http.ListenAndServe helper creates an http.Server with zero timeouts, leaving slowloris or stalled upstream clients free to hold file descriptors indefinitely.

Even when nginx terminates TLS in front of Go, explicit server timeouts catch misbehaving internal callers and keep goroutine counts flat.

srv := &http.Server{
    Addr:              addr,
    Handler:           mux,
    ReadHeaderTimeout: 5 * time.Second,
    ReadTimeout:       10 * time.Second,
    WriteTimeout:      10 * time.Second,
    IdleTimeout:       60 * time.Second,
}