You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+222-4Lines changed: 222 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,7 @@ Contributors:
30
30
August 21, 2025 STATUS compared to [http-proxy](https://www.npmjs.com/package/http-proxy) and [httpxy](https://www.npmjs.com/package/httpxy):
31
31
32
32
- Library entirely rewritten in Typescript in a modern style, with many typings added internally and strict mode enabled.
33
+
-**HTTP/2 Support**: Full HTTP/2 support via [undici](https://github.com/nodejs/undici) with callback-based request/response lifecycle hooks.
33
34
- All dependent packages updated to latest versions, addressing all security vulnerabilities according to `pnpm audit`.
34
35
- Code rewritten to not use deprecated/insecure API's, e.g., using `URL` instead of `parse`.
35
36
- Fixed socket leaks in the Websocket proxy code, going beyond [http-proxy-node16](https://www.npmjs.com/package/http-proxy-node16) to also instrument and logging socket counts. Also fixed an issue with uncatchable errors when using websockets.
@@ -89,7 +90,9 @@ This is the original user's guide, but with various updates.
89
90
-[Setup a stand-alone proxy server with latency](#setup-a-stand-alone-proxy-server-with-latency)
90
91
-[Using HTTPS](#using-https)
91
92
-[Proxying WebSockets](#proxying-websockets)
93
+
-[HTTP/2 Support with Undici](#http2-support-with-undici)
// The headers are modified via the onBeforeRequest callback
285
+
proxy.web(req, res);
286
+
});
287
+
288
+
console.log("listening on port 5050");
289
+
server.listen(5050);
290
+
```
291
+
251
292
**[Back to top](#table-of-contents)**
252
293
253
294
#### Modify a response from a proxied server
@@ -398,6 +439,103 @@ proxyServer.listen(8015);
398
439
399
440
**[Back to top](#table-of-contents)**
400
441
442
+
#### HTTP/2 Support with Undici
443
+
444
+
http-proxy-3 supports HTTP/2 through [undici](https://github.com/nodejs/undici), a modern HTTP client. When undici is enabled, the proxy can communicate with HTTP/2 servers and provides enhanced performance and features.
// Note: response.body is a stream, not the actual body content
505
+
}
506
+
}
507
+
});
508
+
```
509
+
510
+
##### HTTP/2 with HTTPS Proxy
511
+
512
+
```js
513
+
import { readFileSync } from"node:fs";
514
+
515
+
constproxy=createProxyServer({
516
+
target:"https://http2-target.example.com",
517
+
ssl: {
518
+
key:readFileSync("server-key.pem"),
519
+
cert:readFileSync("server-cert.pem")
520
+
},
521
+
undici: {
522
+
agentOptions: {
523
+
allowH2:true,
524
+
connect: { rejectUnauthorized:false }
525
+
}
526
+
},
527
+
secure:false// Skip SSL verification for self-signed certs
528
+
}).listen(8443);
529
+
```
530
+
531
+
**Important Notes:**
532
+
- When `undici` option is provided, the proxy uses undici's HTTP client instead of Node.js native `http`/`https` modules
533
+
- undici automatically handles HTTP/2 negotiation when `allowH2: true` is set
534
+
- The `onBeforeRequest` and `onAfterResponse` callbacks are only available in the undici code path
535
+
- Traditional `proxyReq` and `proxyRes` events are not emitted in the undici path - use the callbacks instead
536
+
537
+
**[Back to top](#table-of-contents)**
538
+
401
539
### Options
402
540
403
541
`httpProxy.createProxyServer` supports the following options:
@@ -491,6 +629,14 @@ proxyServer.listen(8015);
491
629
};
492
630
```
493
631
632
+
- **ca**: Optionally override the trusted CA certificates. This is passed to https.request.
633
+
634
+
- **undici**: Enable undici for HTTP/2 support. Set to `true` for defaults, or provide custom configuration:
635
+
- `agentOptions`: Configuration for undici Agent (see [undici Agent.Options](https://github.com/nodejs/undici/blob/main/docs/api/Agent.md))
636
+
- `requestOptions`: Configuration for undici requests (see [undici Dispatcher.RequestOptions](https://github.com/nodejs/undici/blob/main/docs/api/Dispatcher.md#dispatcherrequestoptions))
637
+
- `onBeforeRequest`: Async callback called before making the undici request
638
+
- `onAfterResponse`: Async callback called after receiving the undici response
639
+
494
640
**NOTE:**
495
641
`options.ws` and `options.ssl` are optional.
496
642
`options.target` and `options.forward` cannot both be missing
@@ -502,6 +648,51 @@ If you are using the `proxyServer.listen` method, the following options are also
502
648
503
649
**[Back to top](#table-of-contents)**
504
650
651
+
### Configuration Compatibility
652
+
653
+
The following table shows which configuration options are compatible with different code paths:
- `error`: The error event is emitted if the request to the target fail. **We do not do any error handling of messages passed between client and proxy, and messages passed between proxy and target, so it is recommended that you listen on errors and handle them.**
@@ -512,11 +703,13 @@ If you are using the `proxyServer.listen` method, the following options are also
512
703
- `close`: This event is emitted once the proxy websocket was closed.
513
704
- (DEPRECATED) `proxySocket`: Deprecated in favor of `open`.
514
705
706
+
**Note**: When using the undici code path (HTTP/2), the `proxyReq` and `proxyRes` events are **not** emitted. Instead, use the `onBeforeRequest` and `onAfterResponse` callback functions in the `undici` configuration.
0 commit comments