Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proxyReq event fails when behind a corporate proxy #1287

Open
amircodota opened this issue Jul 30, 2018 · 4 comments
Open

proxyReq event fails when behind a corporate proxy #1287

amircodota opened this issue Jul 30, 2018 · 4 comments

Comments

@amircodota
Copy link

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

@b44x
Copy link

b44x commented Oct 18, 2018

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

@sandeepkumar03
Copy link

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

@amircodota
Copy link
Author

@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

@Toolsclub
Copy link

have nobody fixed this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants