diff --git a/plugins/proxy-backend/src/service/router.ts b/plugins/proxy-backend/src/service/router.ts index 1798c56cc6..c4da4151bb 100644 --- a/plugins/proxy-backend/src/service/router.ts +++ b/plugins/proxy-backend/src/service/router.ts @@ -102,6 +102,11 @@ export function buildMiddleware( // Use the custom middleware filter to do two things: // 1. Remove any headers not in the allow list to stop them being forwarded // 2. Only permit the allowed HTTP methods if configured + // + // We are filtering the proxy request headers here rather than in + // `onProxyReq` becuase when global-agent is enabled then `onProxyReq` + // fires _after_ the agent has already sent the headers to the proxy + // target, causing a ERR_HTTP_HEADERS_SENT crash const filter = (_pathname: string, req: http.IncomingMessage): boolean => { const headerNames = Object.keys(req.headers); headerNames.forEach(h => {