Answer
Yes – you can make every injected HttpService use a custom DNS resolver by supplying a pre‑configured Axios instance that contains an https.Agent (and optionally an http.Agent) with a custom lookup function. NestJS does not expose a global flag; the resolver must be baked into the agent that the HttpService’s underlying Axios instance uses.
Confirmed facts
- HttpService is a thin wrapper around Axios; the actual HTTP calls go through the Axios instance’s
httpAgent and httpsAgent.
- Node’s
http.Agent and https.Agent accept a lookup option (available since Node 0.11.14) that replaces the default DNS resolution.
- When you register
HttpModule with a custom Axios instance via HttpModule.register({ axios: customInstance }), every subsequent HttpService injection receives that instance, so the custom resolver applies globally.
Steps to configure a global custom DNS resolver
- Create a lookup function that forwards to your internal DNS servers, e.g.:
const dns = require('dns');
function customLookup(hostname, options, callback) {
// Use your internal DNS servers (e.g., 10.0.0.53)
const servers = ['10.0.0.53'];
dns.setServers(servers); // optional, affects fallback
dns.resolve4(hostname, { ...options, servers }, (err, addresses) => {
if (err) return callback(err);
callback(null, addresses, 4); // family 4 for IPv6 use resolve6 similarly
});
}
- Build an
https.Agent (and http.Agent if you need plain HTTP) that uses this lookup:
const https = require('https');
const http = require('http');
const httpsAgent = new https.Agent({ lookup: customLookup, keepAlive: true });
const httpAgent = new http.Agent({ lookup: customLookup, keepAlive: true });
- Create an Axios instance that injects those agents:
const axios = require('axios');
const customAxios = axios.create({
httpAgent,
httpsAgent,
// optional: relax TLS for internal services
httpsAgent: { ...httpsAgent, rejectUnauthorized: false },
});
- Register the HttpModule with this Axios instance (typically in
AppModule):
@Module({
imports: [
HttpModule.register({
axios: customAxios,
}),
// other modules…
],
})
export class AppModule {}
- Now any
HttpService injected throughout the app will use the custom DNS resolver.
Likely explanation / uncertainties
The approach above relies on the lookup option being honoured by the Node version you run. If you are on an older Node (< 0.11.14) the agent will ignore the custom function and fall back to the system resolver. Verify your Node version; if it is insufficient, you would need to upgrade or use a global dns.setServers call (which only affects the dns module, not the underlying c‑ares resolver used by http/https in some builds).
Verification
- Inject
HttpService in a test controller, make a request to a known host, and temporarily replace dns.resolve4 with a mock that returns a specific IP; assert the mock was called.
- Inspect the agent:
httpService.axiosRef.defaults.httpsAgent.lookup should reference your customLookup function.
- Optionally run
tcpdump -n port 53 to see DNS queries directed to your internal DNS server IP.
If you are unsure about the Node version in your deployment, please confirm the exact version so we can advise whether the lookup option is guaranteed to work.