diff --git a/caddyhttp/proxy/proxy.go b/caddyhttp/proxy/proxy.go index 5b386020e..aeff1b8f3 100644 --- a/caddyhttp/proxy/proxy.go +++ b/caddyhttp/proxy/proxy.go @@ -262,6 +262,10 @@ func (p Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { return http.StatusRequestEntityTooLarge, backendErr } + if backendErr == context.Canceled { + return CustomStatusContextCancelled, backendErr + } + // failover; remember this failure for some time if // request failure counting is enabled timeout := host.FailTimeout @@ -397,3 +401,5 @@ func mutateHeadersByRules(headers, rules http.Header, repl httpserver.Replacer) } } } + +const CustomStatusContextCancelled = 499 diff --git a/caddyhttp/proxy/proxy_test.go b/caddyhttp/proxy/proxy_test.go index caf2e6c99..1342b2bea 100644 --- a/caddyhttp/proxy/proxy_test.go +++ b/caddyhttp/proxy/proxy_test.go @@ -1369,7 +1369,7 @@ func TestCancelRequest(t *testing.T) { rec := httptest.NewRecorder() status, err := p.ServeHTTP(rec, req) - expectedStatus, expectErr := http.StatusBadGateway, context.Canceled + expectedStatus, expectErr := CustomStatusContextCancelled, context.Canceled if status != expectedStatus || err != expectErr { t.Errorf("expect proxy handle return status[%d] with error[%v], but got status[%d] with error[%v]", expectedStatus, expectErr, status, err)