diff --git a/doc/api/errors.md b/doc/api/errors.md index b06abbf8d96a2a..e109f58bb92b62 100644 --- a/doc/api/errors.md +++ b/doc/api/errors.md @@ -2489,6 +2489,18 @@ Accessing `Object.prototype.__proto__` has been forbidden using [`Object.setPrototypeOf`][] should be used to get and set the prototype of an object. + + +### `ERR_PROXY_INVALID_CONFIG` + +Failed to proxy a request because the proxy configuration is invalid. + + + +### `ERR_PROXY_TUNNEL` + +Failed to establish proxy tunnel when `NODE_USE_ENV_PROXY` is enabled. + ### `ERR_QUIC_APPLICATION_ERROR` diff --git a/doc/api/http.md b/doc/api/http.md index d104032e0f848c..81ebcd594ea8c8 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -116,6 +116,14 @@ http.get({ + +> Stability: 1.1 - Active development + +When Node.js creates the global agent, it checks the `NODE_USE_ENV_PROXY` +environment variable. If it is set to `1`, the global agent will be constructed +with `proxyEnv: process.env`, enabling proxy support based on the environment variables. + +Custom agents can also be created with proxy support by passing a +`proxyEnv` option when constructing the agent. The value can be `process.env` +if they just want to inherit the configuration from the environment variables, +or an object with specific setting overriding the environment. + +The following properties of the `proxyEnv` are checked to configure proxy +support. + +* `HTTP_PROXY` or `http_proxy`: Proxy server URL for HTTP requests. If both are set, + `http_proxy` takes precedence. +* `HTTPS_PROXY` or `https_proxy`: Proxy server URL for HTTPS requests. If both are set, + `https_proxy` takes precedence. +* `NO_PROXY` or `no_proxy`: Comma-separated list of hosts to bypass the proxy. If both are set, + `no_proxy` takes precedence. + +If the request is made to a Unix domain socket, the proxy settings will be ignored. + +### Proxy URL Format + +Proxy URLs can use either HTTP or HTTPS protocols: + +* HTTP proxy: `http://proxy.example.com:8080` +* HTTPS proxy: `https://proxy.example.com:8080` +* Proxy with authentication: `http://username:password@proxy.example.com:8080` + +### `NO_PROXY` Format + +The `NO_PROXY` environment variable supports several formats: + +* `*` - Bypass proxy for all hosts +* `example.com` - Exact host name match +* `.example.com` - Domain suffix match (matches `sub.example.com`) +* `*.example.com` - Wildcard domain match +* `192.168.1.100` - Exact IP address match +* `192.168.1.1-192.168.1.100` - IP address range +* `example.com:8080` - Hostname with specific port + +Multiple entries should be separated by commas. + +### Example + +Starting a Node.js process with proxy support enabled for all requests sent +through the default global agent: + +```console +NODE_USE_ENV_PROXY=1 HTTP_PROXY=http://proxy.example.com:8080 NO_PROXY=localhost,127.0.0.1 node client.js +``` + +To create a custom agent with built-in proxy support: + +```cjs +const http = require('node:http'); + +// Creating a custom agent with custom proxy support. +const agent = new http.Agent({ proxyEnv: { HTTP_PROXY: 'http://proxy.example.com:8080' } }); + +http.request({ + hostname: 'www.example.com', + port: 80, + path: '/', + agent, +}, (res) => { + // This request will be proxied through proxy.example.com:8080 using the HTTP protocol. + console.log(`STATUS: ${res.statusCode}`); +}); +``` + +Alternatively, the following also works: + +```cjs +const http = require('node:http'); +// Use lower-cased option name. +const agent1 = new http.Agent({ proxyEnv: { http_proxy: 'http://proxy.example.com:8080' } }); +// Use values inherited from the environment variables, if the process is started with +// HTTP_PROXY=http://proxy.example.com:8080 this will use the proxy server specified +// in process.env.HTTP_PROXY. +const agent2 = new http.Agent({ proxyEnv: process.env }); +``` + +[Built-in Proxy Support]: #built-in-proxy-support [RFC 8187]: https://www.rfc-editor.org/rfc/rfc8187.txt [`'ERR_HTTP_CONTENT_LENGTH_MISMATCH'`]: errors.md#err_http_content_length_mismatch [`'checkContinue'`]: #event-checkcontinue diff --git a/doc/api/https.md b/doc/api/https.md index f7e52ec99ab42a..06ce65867ad970 100644 --- a/doc/api/https.md +++ b/doc/api/https.md @@ -65,6 +65,14 @@ An [`Agent`][] object for HTTPS similar to [`http.Agent`][]. See