Node express cannot obtain the customer ip public network after binding the domain name.

  node.js, question

Node code

var express = require('express');
 var app = express();
 var os = require('os');
 var http = require('http');
 var cors = require('cors');
 
 
 app.use(cors());
 
 
 app.get('/', cors(),function(req, res){
 Varip = getclientip (req). match (/\ d plus \.\d plus \.\d plus \.\d plus \. \ d plus/);
 console.log(ip);
 });
 
 
 function getClientIp(req) {
 return req.headers['x-forwarded-for'] ||
 req.connection.remoteAddress ||
 req.socket.remoteAddress ||
 req.connection.socket.remoteAddress;
 bracket
 
 app.listen(8081);
 console.log('listening on port http://localhost:8081/');

Nginx agent

server  {
 listen 80;
 server_name  nnn.com ;
 
 location / {
 proxy_set_header  X-real-IP $remote_addr;
 proxy_pass      http://127.0.0.1:8081;
 bracket
 
 access_log  /home/wwwlogs/nnn.com.log  access;
 bracket

The ip that directly accesses the server is the ip that normally returns to the client.

However, the ip returned by the domain name server visited by nginx agent is 127.0.01

The proxy_pass address that nginx turns to is what it returns.

How can node bind domain names better?

图片描述

req.ip
http://expressjs.com/en/4x/api.html#req.ip

trust proxy
http://expressjs.com/en/4x/api.html#trust.proxy.options.table

nginx.conf

location / {
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 bracket