簡體   English   中英

NodeJS - 返回客戶端 IP 地址而不是服務器的

[英]NodeJS - Return client ip address instead of server's

問題信息

我使用CORS,以確保只有特定的IP的可GETPOST我的REST API的數據。

每次有人想要GETPOST數據時,返回的 IP 是托管應用程序的服務器的 IP,而不是此人自己的 IP 地址。

此時大家都可以訪問REST API,因為我的前端運行在同一台服務器上,前端需要有GETPOST數據的權限,所以我將服務器的IP地址列入白名單。

由於每個嘗試連接的人,其 IP 地址自動成為服務器的地址,因此每個人都可以訪問 REST API。

技術信息

我的 REST API 在端口 8080 上使用 pm2 運行,我使用 Nginx 將其重新路由到使用 SSL 的端口 8877。 8877是我的rest api的端點,也許這就是為什么客戶端IP地址自動成為服務器IP地址的原因?

# General HTTP to HTTPS
server {
        listen 80;
        listen [::]:80;
        server_name www.example.com default_server;

        location / {
                return 302 https://$host$request_uri;
        }
}

#Web Application
server {
    listen 443 ssl;
    listen [::]:443 ssl;
    server_name www.example.com default_server;

    root /var/www/example.com;
    index index.html;

    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_certificate /etc/nginx/ssl/example.com/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
                #...
                try_files $uri $uri/ /index.html$is_args$args;
                #...
    }
}

#Rest API http to https 
server {
    listen 8877 ssl;
    listen [::]:8877 ssl;
    server_name example.com;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_certificate /etc/nginx/ssl/example.com/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/example.com.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

    location / {
            proxy_pass http://example.com:8080;
    }
}

休息 API cors

const whitelist = [
    '::ffff:111.11.11.111', etc.];

var corsOptions = {
    origin: (origin, callback) => {
        let trafficAllowed = {};
        console.log("IP: ", origin);
        if (whitelist.indexOf(origin) !== -1) {
            trafficAllowed = {origin: false}
        } else {
            trafficAllowed = {origin: true}
        }
        callback(null, trafficAllowed)
    }
};

app.use((req, res, next) => {
    req.headers.origin =
        req.connection.remoteAddress ||
        req.headers['x-forwarded-for'] ||
        req.socket.remoteAddress ||
        (req.connection.socket ? req.connection.socket.remoteAddress : null);
    next();
});

app.use(cors(corsOptions));

可能的解決方法

  • 是否可以將 SSL 直接分配給 8080 端口(REST API 使用的是 pm2)?
  • 如何獲取客戶端的IP地址而不是服務器的IP地址。

嘗試將這些配置放在您的 nginx 中:

proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $proxy_x_forwarded_proto;

沒有這個,您的后端將只會收到 nginx IP 地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM