node-http-proxy
node-http-proxy copied to clipboard
proxyReq event fails when behind a corporate proxy
We have been using http-proxy for a long time without a problem. (Congrats on that, BTW :-) )
Lately we have been working with a big enterprise customer, were our server runs behind a corporate proxy.
We used the corporate proxy recipe, but we ran into an issue where calling setHeader fails inside a proxyReq event, saying that Can't set headers after they are sent..
Note that this works fine in default circumstances, and only happens when using the HttpsProxyAgent as recommended in the recipe.
I was able to create a simple repro -
const express = require('express');
const proxy = require('http-proxy-middleware');
const HttpsProxyAgent = require('https-proxy-agent');
const app = express();
let httpsProxyAgent = new HttpsProxyAgent({host: '18.205.245.16', port: 3128, auth: 'shaia:shaia'});
app.use('/', proxy({target: 'https://amir-test-header.herokuapp.com', changeOrigin: true, xfwd: true, agent: httpsProxyAgent,
onProxyReq: (proxyReq, req) => {
proxyReq.setHeader('test', 'hello');
}}));
app.listen(3000);
And then
curl localhost:3000/headers/test
Commenting out the agent option will make the curl request work.
We thought of a workaround where we modify the original request object in a preceding middleware before the proxy middleware. This seems to work, but obviously is not the intended way of using it.
Anybody has other suggestions as to how to workaround/fix this issue?
Thanks Amir
Hello. After hours of searching and testing - i found solution for this.
var http = require('http'),
httpProxy = require('http-proxy'),
HttpsProxyAgent = require('https-proxy-agent');
var proxy = httpProxy.createProxyServer({});
var httpsProxyAgent = new HttpsProxyAgent({host: '18.205.245.16', port: 3128, auth: 'shaia:shaia'});
var server = http.createServer(function(req, res) {
req.setHeader('test', 'hello');
proxy.web(req, res, {
target: 'https://amir-test-header.herokuapp.com',
changeOrigin: true,
xfwd: true,
agent: httpsProxyAgent
});
});
server.listen(3000);
@amircodota , I also faced same issue with same headers related error when dealing with a react app passing these values via package.json. Just to check i directly modified the http-proxy-middleware under node_modules folder to use https-proxy-agent. This worked for me when accessing on Edge or IE browsers but NOT on Chrome or Firefox.
As of now I don't have a way to pass agent via package.json, see this question on stackoverflow, but these are the observations i came across.
@sandeepkumar03 I'm not sure I completely understand your use case, but I think you are using webpack, and the webpack-dev-server to run your react app in development. In such a case, you are indeed limited by what you can configure via package.json, but you will probably be able to achieve what you want if you use webpack-dev-middleware. This way you will have your own server.js, and you cn configure http-proxy-middleware exactly how you wanted
have nobody fixed this