koa-proxy
koa-proxy copied to clipboard
Host header overwritten when proxying
I want to send a x-forwarded-host
header in the proxy request, and tried to implement like this:
app.use(proxy({
host: 'somethingelse.com',
match: '\/something\//,
requestOptions: (req, options) => {
options.headers['X-Forwarded-Host'] = req.headers.host;
return options;
},
})
However the host I get is the one I am forwarding to, not the original host. I believe that when the headers are added to the new request opt
it is copying the whole object, so when the host is rewritten there immediately after it is being rewritten on the original context too.
Have worked around this:
app.use((ctx, next) => {
const originalHost = ctx.headers.host;
const somethingProxy = proxy({
host: 'somethingelse.com',
match: '\/something\//,
requestOptions: (req, options) => {
options.headers['X-Forwarded-Host'] = originalHost;
return options;
},
});
return somethingProxy(ctx, next);
});