{"record":{"id":"202f48bd7bda1573","repo":"caddyserver/caddy","slug":"preparing-request-for-upstream-round-trip-v","errorCode":null,"errorMessage":"preparing request for upstream round-trip: %v","messagePattern":"preparing request for upstream round-trip: (.+?)","errorType":"http","errorClass":null,"httpStatus":500,"severity":"error","filePath":"modules/caddyhttp/reverseproxy/reverseproxy.go","lineNumber":492,"sourceCode":"func (b *bodyNopCloserIfNotRead) Close() error {\n\t// don't close the body\n\tif b.read == 0 {\n\t\treturn nil\n\t}\n\t// close as usual, when -1, any read will return EOF as the original read will do\n\t// in other cases, the read will fail as body is closed because we do not want partial bodies to be sent to the upstream\n\t// users can buffer the entire request body to allow the request to be resent\n\treturn b.ReadCloser.Close()\n}\n\nfunc (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error {\n\trepl := r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer)\n\n\t// prepare the request for proxying; this is needed only once\n\tclonedReq, err := h.prepareRequest(r, repl)\n\tif err != nil {\n\t\treturn caddyhttp.Error(http.StatusInternalServerError,\n\t\t\tfmt.Errorf(\"preparing request for upstream round-trip: %v\", err))\n\t}\n\n\t// websocket over http2 or http3 if extended connect is enabled, assuming backend doesn't support this, the request will be modified to http1.1 upgrade\n\t// Both use the same upgrade mechanism: server advertizes extended connect support, and client sends the pseudo header :protocol in a CONNECT request\n\t// The quic-go http3 implementation also puts :protocol in r.Proto for CONNECT requests (quic-go/http3/headers.go@70-72,185,203)\n\t// TODO: once we can reliably detect backend support this, it can be removed for those backends\n\tif (r.ProtoMajor == 2 && r.Method == http.MethodConnect && r.Header.Get(\":protocol\") == \"websocket\") ||\n\t\t(r.ProtoMajor == 3 && r.Method == http.MethodConnect && r.Proto == \"websocket\") {\n\t\tclonedReq.Header.Del(\":protocol\")\n\t\t// keep the body for later use. http1.1 upgrade uses http.NoBody\n\t\tcaddyhttp.SetVar(clonedReq.Context(), \"extended_connect_websocket_body\", clonedReq.Body)\n\t\tclonedReq.Body = http.NoBody\n\t\tclonedReq.Method = http.MethodGet\n\t\tclonedReq.Header.Set(\"Upgrade\", \"websocket\")\n\t\tclonedReq.Header.Set(\"Connection\", \"Upgrade\")\n\t\tkey := make([]byte, 16)\n\t\t_, randErr := rand.Read(key)\n\t\tif randErr != nil {","sourceCodeStart":474,"sourceCodeEnd":510,"githubUrl":"https://github.com/caddyserver/caddy/blob/50e54ee279aa1e504fe218ca49ab6ae16c100410/modules/caddyhttp/reverseproxy/reverseproxy.go#L474-L510","documentation":"Error \"preparing request for upstream round-trip: %v\" thrown in caddyserver/caddy.","triggerScenarios":"Thrown at modules/caddyhttp/reverseproxy/reverseproxy.go:492 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check the reverse proxy rewrite and header configuration; the wrapped error explains why the request could not be prepared."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"50e54ee279aa1e504fe218ca49ab6ae16c100410","analyzedAt":"2026-08-15T09:20:21.641Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}