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

Keep proxy IP rather than original caller IP

Open cawecoy opened this issue 9 months ago • 2 comments

Hello, thanks for the amazing work here!

I'm using an API that requires a static IP, which the original caller can't have for now. That's why I'm creating a proxy using this package.

My script so far:

const express = require('express');
const proxy = require('express-http-proxy');
require('dotenv').config();

const api_domain = process.env.API_DOMAIN;
const proxy_ip = process.env.PROXY_IP;

const app = express();

app.use('/', proxy(`https://${api_domain}`, {
    proxyReqOptDecorator: (proxyReqOpts, srcReq) => {
        proxyReqOpts.headers['X-Forwarded-For'] = proxy_ip;
        proxyReqOpts.headers['X-Real-IP'] = proxy_ip;
        proxyReqOpts.headers['Client-IP'] = proxy_ip;
        return proxyReqOpts;
    }
}));

app.listen(3000, () => {
    console.log('Proxy server listening on port 3000');
});

But the API is seeing the original caller IP, not the proxy IP... Am I missing something?

cawecoy avatar Mar 15 '25 00:03 cawecoy

Hi @cawecoy. I added a test to check this behavior, and as far as I can tell, it's working as I expect it to work. Let me know if you see something I'm missing.

https://github.com/villadora/express-http-proxy/blob/c5574ea5fb55dfdf2916d25ef0bbfffe9f457b62/test/headers.js#L54

monkpow avatar May 03 '25 06:05 monkpow

Hi @monkpow thanks!

I couldn't solve the problem even using other programming languages, so I decided to completely change my approach.

I used a simple Squid Cache on the proxy side, and on the original caller side, which was a WordPress site, I used WP_HTTP_proxy. Now the original caller makes requests THROUGH the proxy, and not TO the proxy. With this setup I achieved my goal.

So I can't help with more information now, sorry. Feel free to close the issue.

cawecoy avatar May 08 '25 23:05 cawecoy