最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to create a proxy that replaces the origin IP address with the proxy server IP address using Node.JS? - Stack Overflow

programmeradmin1浏览0评论

I'm trying to use Node.JS with http-proxy-middleware to build a proxy microservice because the Luminate Online API that the customer needs to use requires requests from a server with a static IP address added to their IP allow list, and the customer server can't have a static IP address for now.

So for the proxy microservice I created a server with a static IP address which was added to the API IP allow list. And I wrote this script to run on the server:

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
require('dotenv').config();

const api_domain = process.env.LUMINATE_DOMAIN;
const api_anization = process.env.LUMINATE_ORGANIZATION;
const proxy_ip = process.env.PROXY_IP;

const app = express();

const proxyMiddleware = createProxyMiddleware({
    target: 'https://'+api_domain,
    changeOrigin: true,
    onProxyReq: (proxyReq, req) => {
        proxyReq.setHeader('X-Forwarded-For', proxy_ip);
        proxyReq.setHeader('X-Real-IP', proxy_ip);
        proxyReq.setHeader('Client-IP', proxy_ip);
    },
    pathRewrite: (path, req) => {
        const newPath = `/${api_anization}/site/${req.params.path}`;
        return newPath;
    }
});

app.use('/site/:path(*)', proxyMiddleware);
app.listen(3000);

After properly executing the script and making a request from the customer server, the API response says: "Request not allowed from source IP address 170.246.33.111.".

Note: 170.246.33.111 is the customer server IP address, not the proxy server IP address.

Why is my script not successfully replacing origin IP address with the proxy server IP address? What could be wrong and how to fix it?

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论