Hello,
I'm working behind a corporate proxy, and I can't seem to make redirection work through it.
Here is my config:
{
context: '/api',
options: {
pathRewrite: { '^/api' : '' },
target: 'http://api.stuff.com',
changeOrigin: true,
toProxy: 'http://x.x.x.x:8080/'
}
Maybe it's a misuse of the toProxy option, but I can't find a good example usage.
In another hand, it would be nice to make use of the standard http_proxy and https_proxy environment variables to make this work automatically without having to fiddle through the config (most node modules use these to work through corporate proxies).
Thanks for your help
Hi,
I've never used the toProxy option before...
http-proxy-middleware is just a small middleware wrapper around http-proxy.
From the http-proxy's code and tests I noticed toProxy is expecting a boolean. Perhaps changing it to a boolean wil solve the issue.
http-proxy code comment:
https://github.com/nodejitsu/node-http-proxy/blob/76051032e7dcea522a691e88ffbc2f8d03f1365c/lib/http-proxy.js#L40
http-proxy test case:
https://github.com/nodejitsu/node-http-proxy/blob/76051032e7dcea522a691e88ffbc2f8d03f1365c/test/lib-http-proxy-common-test.js#L238
Interesting idea.
Could you clarify or give an example on how http_proxy and https_proxy environment variables could be used and configured automatically?
I already tried using true as a value for toProxy, still no luck.
I'll explain what I would like to achieve:
/api URI, with the same host as the web app./api to http://other_host.com/api, another host with the backend (integration or production server).http://other_host.com/api should go through the proxy.My problem is currently the last point, and it would be where the http_proxy environment variables should be used (to redirect or not through the proxy). Even manually, I am currently unable to make the last point work, I know it should be possible but I don't know how :-/
I looked at the issues in node-http-proxy and found a possible solution to my issue: https://github.com/nodejitsu/node-http-proxy/issues/832
Seems like it could be fixed in code and not in the config (example for https with env variable):
var HttpsProxyAgent = require('https-proxy-agent');
var proxyServer = process.env.http_proxy;
var proxy = httpProxy.createProxyServer({
agent: new HttpsProxyAgent(proxyServer)
});
Interesting. If that solves the issue, it probably will work as well when you provide agent to http-proxy-middleware as such:
var proxyMiddleware = require('http-proxy-middleware');
var HttpsProxyAgent = require('https-proxy-agent');
var proxyServer = process.env.http_proxy;
var proxy = proxyMiddleware({
context: '/api',
options: {
pathRewrite: { '^/api' : '' },
target: 'http://api.stuff.com',
changeOrigin: true,
agent: new HttpsProxyAgent(proxyServer)
})
That's what I'm currently doing, with added env.http_proxy detection:
function setupCorporateProxy(options) {
var proxyServer = process.env.http_proxy || process.env.HTTP_PROXY;
if (proxyServer) {
options.agent = new HttpProxyAgent(proxyServer);
gutil.log(gutil.colors.cyan('Using corporate proxy server: ' + proxyServer));
}
return options;
}
The request seems to be sent to the proxy just fine, but now the URL rewriting is skipped for the request sent to the proxy: according to my config, the /api should be removed when sent to the proxy but it is not :-/
Think I found the pathRewrite issue...
I pushed a patch into this branch:
https://github.com/chimurai/http-proxy-middleware/tree/proxy-agent-path-rewrite-fix
Would be great if you could test this patch, before merging the patch.
I just tested, it works!!
Good job, and thanks for the quick fix :+1:
Np. Thanks for reporting the issue!
Just pushed v0.8.1 with the fix.
Spent half the day fighting with my corporate proxy, until I found this. Thank you! Works perfectly.
Glad to hear this thread helped you out.
I've added a corporate proxy configuration example to the recipes:
https://github.com/chimurai/http-proxy-middleware/blob/master/recipes/corporate-proxy.md
Perfect. Thanks!
Most helpful comment
Spent half the day fighting with my corporate proxy, until I found this. Thank you! Works perfectly.