To make all HTTP and HTTPS connections go through an outbound HTTP proxy:
var globalTunnel =require('global-tunnel');
sockets:50// optional pool size for each http and https
This will use the CONNECT method for HTTPS requests and absolute-URIs for HTTP requests, which is how many network proxies are configured.
Optionally, to tear-down the global agent and restore node's default global agents:
Any active connections will be allowed to run to completion, but new connections will use the default global agents.
The complete list of options to globalTunnel.initialize:
host - the hostname or IP of the HTTP proxy to use
port - the TCP port to use on that proxy
tunnel(optional) controls what protocols use the CONNECT method. It has three possible values (strings):
neither - don't use CONNECT; just use absolute URIs
https - (the default) only use CONNECT for HTTPS requests
both - use CONNECT for both HTTP and HTTPS requests
protocol - the protocol that the proxy speaks, either http: or https:.
sockets - (optional) maximum number of TCP sockets to use in each pool. There are two pools: one for HTTP and one for HTTPS. Uses node's default (5) if falsy.
Here's a few interesting variations on the basic config.
Absolute URI Proxies
Another common proxy configuration is one that expects clients to use an absolute URI for the Request-URI for all HTTP and HTTPS requests. This is common for networks that use a proxy for security scanning and access control.
What does this mean? It means that instead of ...
GET / HTTP/1.1Host: example.com
... your proxy expects ...
GET https://example.com/ HTTP/1.1
You'll need to specify tunnel: 'neither' if this is the case. If the proxy speaks HTTP (i.e. the connection from node --> proxy is not encrypted):
If tunnelling both protocols, you can use different HTTPS client configurations for the two phases of the connection.
// use this config for app -> proxy
// use this config for proxy -> origin
The http_proxy environment variable will be used if the first parameter to globalTunnel.initialize is null or an empty object.