9

I have a node server and based on some recent infrastructure changes, I need to make sure all outbound requests are going through a Squid proxy EXCEPT for traffic to hosts listed in the NO_PROXY environment variable.

Setting HTTP_PROXY, HTTPS_PROXY and NO_PROXY doesn't seem to affect the node server's behavior, but I need a way to do this without having to manually edit libraries. I have ~10 libraries that reach out to external services that need to respect this proxying behavior.

Is there some other way I can globally set proxying behavior including respecting NO_PROXY?

2

2 Answers 2

15

Unfortunately, the Node.js runtime does not support configuring HTTP proxying using environment variables out of the box (see nodejs/node#8381 and nodejs/node#15620). Request library support is mixed:

Any library which provides a way to pass an http.Agent which can support environmentally-configured proxies, such as:

For example:

import fetch from 'node-fetch';
import ProxyAgent from 'proxy-agent';

const response = await fetch('https://example.com', { agent: new ProxyAgent() });
const body = await response.text();

If you can not pass an Agent, or can't modify the code at all, you may want to consider global-agent which can be used as a pre-loaded module to proxy all requests (configured by the GLOBAL_AGENT_HTTP_PROXY environment variable):

export GLOBAL_AGENT_HTTP_PROXY=http://proxy.example.com
node --require global-agent/bootstrap mycode.js

Alternatively, if your system supports LD_PRELOAD or DYLD_INSERT_LIBRARIES, you could use a general-purpose proxifier such as ProxyChains-NG (or the original ProxyChains).

Sign up to request clarification or add additional context in comments.

Comments

0

Just in case someone arrives here, it seems it was already added to request.js in https://github.com/request/request/pull/1096

1 Comment

Request is a deprecated project now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.