简体   繁体   中英

Make Node.js use http_proxy, https_proxy and no_proxy environment variables

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?

以防万一有人到达这里,它似乎已经添加到https://github.com/request/request/pull/1096 中的request.js

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM