Skip to content

Commit 8b4bb44

Browse files
committed
docs(learn): add enterprise network configuration guide
Add a guide on how to configure proxies and system CA certificates using the new built-in support.
1 parent 43ffe41 commit 8b4bb44

File tree

1 file changed

+265
-0
lines changed

1 file changed

+265
-0
lines changed
Lines changed: 265 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,265 @@
1+
---
2+
title: Enterprise Network Configuration
3+
layout: learn
4+
authors: Joyee Cheung
5+
---
6+
7+
# Enterprise Network Configuration
8+
9+
## Overview
10+
11+
Enterprise environments often require applications to operate behind corporate proxies and use custom certificate authorities (CAs) for SSL/TLS validation. Node.js provides built-in support for these requirements through environment variables and command-line flags, eliminating the need for third-party proxy libraries in many cases.
12+
13+
This guide covers how to configure Node.js applications to work in enterprise network environments:
14+
15+
- Configuring proxies via the `NODE_USE_ENV_PROXY` environment variable or the `--use-env-proxy` flag
16+
- Adding certificate authorities from system storage via the `NODE_USE_SYSTEM_CA` environment variable or the `--use-system-ca` flag.
17+
18+
## Proxy Configuration
19+
20+
In many enterprise environments, internet access to external services may need to be routed through HTTP/HTTPS proxies for security and monitoring. This requires applications to be aware of and use these proxies when making network requests.
21+
22+
Proxy settings are often provided via environment variables such as `HTTP_PROXY`, `HTTPS_PROXY`, and `NO_PROXY`. Node.js supports these when `NODE_USE_ENV_PROXY` or `--use-env-proxy` is enabled. This works with `node:http` and `node:https` (v22.21.0 or v24.5.0+) methods as well as `fetch()` (v22.21.0 or v24.0.0+).
23+
24+
Example (POSIX shells):
25+
26+
```bash
27+
# The proxy settings might be configured in the system by your IT department
28+
# and shared across different tools.
29+
export HTTP_PROXY=http://proxy.company.com:8080
30+
export HTTPS_PROXY=http://proxy.company.com:8080
31+
export NO_PROXY=localhost,127.0.0.1,.company.com
32+
33+
# To enable it for Node.js applications.
34+
export NODE_USE_ENV_PROXY=1
35+
node app.js
36+
```
37+
38+
Alternatively, enable it via the command-line flag `--use-env-proxy` on Node.js v22.21.0 or v24.5.0 and above:
39+
40+
```bash
41+
# The proxy settings might be configured in the system by your IT department
42+
# and shared across different tools.
43+
export HTTP_PROXY=http://proxy.company.com:8080
44+
export HTTPS_PROXY=http://proxy.company.com:8080
45+
export NO_PROXY=localhost,127.0.0.1,.company.com
46+
47+
# To enable it for Node.js applications.
48+
node --use-env-proxy app.js
49+
```
50+
51+
Or, if `--env-file` is used to load environment variables from a file:
52+
53+
```txt
54+
# In .env file
55+
HTTP_PROXY=http://proxy.company.com:8080
56+
HTTPS_PROXY=http://proxy.company.com:8080
57+
NO_PROXY=localhost,127.0.0.1,.company.com
58+
NODE_USE_ENV_PROXY=1
59+
```
60+
61+
```bash
62+
node --env-file ./.env app.js
63+
```
64+
65+
Once enabled, `http`, `https`, and `fetch()` requests use the configured proxies by default, unless an agent is overridden or the target matches `NO_PROXY`.
66+
67+
### Configure the Proxy Programmatically
68+
69+
To configure the proxy programmatically, override the agents. This is currently supported by `https.request()` and other methods built upon it such as `https.get()`.
70+
71+
To override the agent on a per-request basis, use the `agent` option for `http.request()`/`https.request()` and similar methods:
72+
73+
```cjs
74+
const https = require('node:https');
75+
76+
// Creating a custom agent with custom proxy support.
77+
const agent = new https.Agent({ proxyEnv: { HTTPS_PROXY: 'http://proxy.company.com:8080' } });
78+
79+
https.request({
80+
hostname: 'www.external.com',
81+
port: 443,
82+
path: '/',
83+
agent,
84+
}, (res) => {
85+
// This request will be proxied through proxy.company.com:8080 using the HTTP protocol.
86+
});
87+
```
88+
89+
```mjs
90+
import https from 'node:https';
91+
92+
// Creating a custom agent with custom proxy support.
93+
const agent = new https.Agent({ proxyEnv: { HTTPS_PROXY: 'http://proxy.company.com:8080' } });
94+
95+
https.request({
96+
hostname: 'www.external.com',
97+
port: 443,
98+
path: '/',
99+
agent,
100+
}, (res) => {
101+
// This request will be proxied through proxy.company.com:8080 using the HTTP protocol.
102+
});
103+
```
104+
105+
To override the agent globally, reset `http.globalAgent` and `https.globalAgent`:
106+
107+
<!-- TODO(joyeecheung): update this when Node.js has a method that supports global configuration for all requesters -->
108+
109+
**Note**: Global agents do not affect `fetch()`.
110+
111+
```cjs
112+
const http = require('node:http');
113+
const https = require('node:https');
114+
115+
http.globalAgent = new http.Agent({ proxyEnv: { HTTP_PROXY: 'http://proxy.company.com:8080' } });
116+
https.globalAgent = new https.Agent({ proxyEnv: { HTTPS_PROXY: 'http://proxy.company.com:8080' } });
117+
118+
// Subsequent requests will all use the configured proxies, unless they override the agent option.
119+
http.request('http://external.com', (res) => { /* ... */ });
120+
https.request('https://external.com', (res) => { /* ... */ });
121+
```
122+
123+
```mjs
124+
import http from 'node:http';
125+
import https from 'node:https';
126+
127+
http.globalAgent = new http.Agent({ proxyEnv: { HTTP_PROXY: 'http://proxy.company.com:8080' } });
128+
https.globalAgent = new https.Agent({ proxyEnv: { HTTPS_PROXY: 'http://proxy.company.com:8080' } });
129+
130+
// Subsequent requests will all use the configured proxies, unless they override the agent option.
131+
http.request('http://external.com', (res) => { /* ... */ });
132+
https.request('https://external.com', (res) => { /* ... */ });
133+
```
134+
135+
### Using Proxies with Authentication
136+
137+
If the proxy requires authentication, include credentials in the proxy URL:
138+
139+
```bash
140+
export HTTPS_PROXY=http://username:[email protected]:8080
141+
```
142+
143+
**Security Note**: Avoid committing credentials in env files. Prefer a secret manager and programmatic configuration.
144+
145+
### Proxy Bypass Configuration
146+
147+
The `NO_PROXY` variable supports:
148+
149+
* `*` - Bypass proxy for all hosts
150+
* `company.com` - Exact host name match
151+
* `.company.com` - Domain suffix match (matches `sub.company.com`)
152+
* `*.company.com` - Wildcard domain match
153+
* `192.168.1.100` - Exact IP address match
154+
* `192.168.1.1-192.168.1.100` - IP address range
155+
* `company.com:8080` - Hostname with specific port
156+
157+
If a target matches `NO_PROXY`, the request bypasses the proxy.
158+
159+
## Certificate Authority Configuration
160+
161+
By default, Node.js uses Mozilla’s bundled root CAs and does not consult the OS store. In many enterprise environments, internal CAs are installed in the OS store and are expected to be trusted when connecting to internal services; connections to certificates signed by those CAs can fail validation with errors such as:
162+
163+
```
164+
Error: self signed certificate in certificate chain
165+
```
166+
167+
From Node.js v22.15.0, v23.9.0, v24.0.0 and above, Node.js can be configured to trust these custom CAs using the system's certificate store.
168+
169+
### Adding CA Certificates from the System Store
170+
171+
- From environment variable: `NODE_USE_SYSTEM_CA=1 node app.js`
172+
- From command-line flag: `node --use-system-ca app.js`
173+
174+
When enabled, Node.js loads system CAs and uses them in addition to its bundled CAs for TLS validation.
175+
176+
Node.js reads certificates from different locations depending on the platform:
177+
178+
- Windows: Windows Certificate Store (via Windows Crypto API)
179+
- macOS: macOS Keychain
180+
- Linux: OpenSSL defaults, typically via `SSL_CERT_FILE`/`SSL_CERT_DIR`, or paths like `/etc/ssl/cert.pem` and `/etc/ssl/certs/` depending on the OpenSSL build
181+
182+
Node.js follows a policy similar to that of Chromium. See [the Node.js documentation](https://nodejs.org/api/cli.html#--use-system-ca) for more details.
183+
184+
### Adding additional CA Certificates
185+
186+
To add specific CA certificates without relying on the system store:
187+
188+
```bash
189+
export NODE_EXTRA_CA_CERTS=/path/to/company-ca-bundle.pem
190+
node app.js
191+
```
192+
193+
The file should contain one or more PEM-encoded certificates.
194+
195+
#### Combining Options
196+
197+
You can combine `NODE_USE_SYSTEM_CA` with `NODE_EXTRA_CA_CERTS`:
198+
199+
```bash
200+
export NODE_USE_SYSTEM_CA=1
201+
export NODE_EXTRA_CA_CERTS=/path/to/additional-cas.pem
202+
node app.js
203+
```
204+
205+
With both enabled, Node.js trusts bundled CAs, system CAs, and the additional certificates specified by `NODE_EXTRA_CA_CERTS`.
206+
207+
### Configure CA Certificates Programmatically
208+
209+
#### Configure Global CA Certificates
210+
211+
Use [`tls.getCACertificates()`](https://nodejs.org/api/tls.html#tlsgetcacertificatestype) and [`tls.setDefaultCACertificates()`](https://nodejs.org/api/tls.html#tlssetdefaultcacertificatescerts) to configure global CA certificates. For example, to add system certificates into the default store:
212+
213+
```cjs
214+
const tls = require('node:tls');
215+
const https = require('node:https');
216+
const currentCerts = tls.getCACertificates('default');
217+
const systemCerts = tls.getCACertificates('system');
218+
tls.setDefaultCACertificates([...currentCerts, ...systemCerts]);
219+
220+
// Subsequent requests use system certificates during verification.
221+
https.get('https://internal.company.com', (res) => { /* ... */ });
222+
fetch('https://internal.company.com').then((res) => { /* ... */ });
223+
```
224+
225+
```mjs
226+
import tls from 'node:tls';
227+
import https from 'node:https';
228+
const currentCerts = tls.getCACertificates('default');
229+
const systemCerts = tls.getCACertificates('system');
230+
tls.setDefaultCACertificates([...currentCerts, ...systemCerts]);
231+
232+
// Subsequent requests use system certificates during verification.
233+
https.get('https://internal.company.com', (res) => { /* ... */ });
234+
fetch('https://internal.company.com').then((res) => { /* ... */ });
235+
```
236+
237+
#### Configure CA Certificates for Individual Requests
238+
239+
To override CA certificates per request, use the `ca` option. This is currently only supported by `tls.connect()`/`https.request()` and methods built upon them such as `https.get()`.
240+
241+
```cjs
242+
const https = require('node:https');
243+
const specialCerts = ['-----BEGIN CERTIFICATE-----\n...'];
244+
https.get({
245+
hostname: 'internal.company.com',
246+
port: 443,
247+
path: '/',
248+
method: 'GET',
249+
// The `ca` option replaces defaults; concatenate bundled certs if needed.
250+
ca: specialCerts,
251+
}, (res) => { /* ... */ });
252+
```
253+
254+
```mjs
255+
import https from 'node:https';
256+
const specialCerts = ['-----BEGIN CERTIFICATE-----\n...'];
257+
https.get({
258+
hostname: 'internal.company.com',
259+
port: 443,
260+
path: '/',
261+
method: 'GET',
262+
// The `ca` option replaces defaults; concatenate bundled certs if needed.
263+
ca: specialCerts,
264+
}, (res) => { /* ... */ });
265+
```

0 commit comments

Comments
 (0)