node-http-proxy icon indicating copy to clipboard operation
node-http-proxy copied to clipboard

Create proxy server behind corporate proxy

Open ahmedabbas11 opened this issue 9 years ago • 5 comments

i have anodejs application that uses http-proxy to create a proxy to send the incoming requests for example: https://localhost/api/login to https://server1/api/login. here is the code used :

var httpProxy = require('http-proxy');

var proxy = httpProxy.createProxyServer();

self.routes['/api/*'] = function(req, res) { proxy.proxyRequest(req, res, { target: "https://server1", changeOrigin: true }); }; this is working just fine in my machine. Now when i deploy this on a server, i get error { [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' } the problem is that there is another proxy let say (corporate proxy let call it: localProxy) between myserver and server1. I don't know where to set the localProxy in my code above. and where to set the server1 url? and is there is a way to use http-proxy in this case

ahmedabbas11 avatar May 27 '15 13:05 ahmedabbas11

If you are using HTTPS, wich it seems so, you should use https-proxy-agent. You can provide a proxy server to https-proxy-agent:

var HttpsProxyAgent = require('https-proxy-agent'); var proxyServer = process.env.http_proxy;

var proxy = httpProxy.createProxyServer({ agent: new HttpsProxyAgent(proxyServer) });

alfonso-presa avatar Jun 10 '15 11:06 alfonso-presa

Great suggestion, it works.

colthreepv avatar Feb 18 '20 14:02 colthreepv

I agree, this should be the go-to solution when one wants to use node-http-proxy to send requests through another proxy.

barroudjo avatar Apr 28 '21 10:04 barroudjo

If you are using HTTPS, wich it seems so, you should use https-proxy-agent. You can provide a proxy server to https-proxy-agent:

var HttpsProxyAgent = require('https-proxy-agent'); var proxyServer = process.env.http_proxy;

var proxy = httpProxy.createProxyServer({ agent: new HttpsProxyAgent(proxyServer) });

this solution saves my day 8 years after 😆

yckbilly1929 avatar Jun 03 '23 16:06 yckbilly1929

Thansk @alfonso-presa !

By any chance this knowledge can be added to the documentation? The description for target, agent and forward are very vague. Unless you are very familiar with the code, it is unlikely you can connect these options together and understand when to use which.

davidshen84 avatar Sep 08 '23 03:09 davidshen84