Repository: caddyserver/caddy (language: go)

Issue:
reverse proxy: when timeout occurs Caddy responses as 502 Bad Gateway
When I use timeout limit in my Caddyfile and upstream server is slow, Caddy responses in such way:

`HTTP 502: net/http: timeout awaiting response headers`
`HTTP 502: dial tcp <some IP address>:443: i/o timeout`

I think, it should be 504 Gateway Timeout. Because HTTP code 504 means, that I should increase timeout limit. 


Discussion / hints:

Fix patch:
diff --git a/modules/caddyhttp/reverseproxy/reverseproxy.go b/modules/caddyhttp/reverseproxy/reverseproxy.go
index 07eb99ce0b2..1068c23da6c 100644
--- a/modules/caddyhttp/reverseproxy/reverseproxy.go
+++ b/modules/caddyhttp/reverseproxy/reverseproxy.go
@@ -524,7 +524,7 @@ func (h *Handler) proxyLoopIteration(r *http.Request, origReq *http.Request, w h
 
 	// proxy the request to that upstream
 	proxyErr = h.reverseProxy(w, r, origReq, repl, dialInfo, next)
-	if proxyErr == nil || proxyErr == context.Canceled {
+	if proxyErr == nil || errors.Is(proxyErr, context.Canceled) {
 		// context.Canceled happens when the downstream client
 		// cancels the request, which is not our failure
 		return true, nil
@@ -1180,6 +1180,11 @@ func statusError(err error) error {
 	// errors proxying usually mean there is a problem with the upstream(s)
 	statusCode := http.StatusBadGateway
 
+	// timeout errors have a standard status code (see issue #4823)
+	if err, ok := err.(net.Error); ok && err.Timeout() {
+		statusCode = http.StatusGatewayTimeout
+	}
+
 	// if the client canceled the request (usually this means they closed
 	// the connection, so they won't see any response), we can report it
 	// as a client error (4xx) and not a server error (5xx); unfortunately


Test patch: