Using Nginx as a reverse proxy for the node. JS program, there is a problem: the client IP that gets in the program is always 127.0.0.1
What if you want to get the real client IP changed?
First, configure the Nginx reverse proxy Proxy_set_header
server { listen ; server_name chat.luckybing.top; / { proxy_set_header Host $host; Proxy_set_header X-real-IP $remote _addr; Proxy_set_header REMOTE-HOST $remote _addr; Proxy_set_header X-forwarded-for$proxy _add_x_forwarded_for; Proxy_pass http://127.0.0.1:4000; } }
In this case, the Nginx does the reverse proxy will write the client's head information into the requested head
Second, the Node.js+express program to obtain the client real IP
With req.headers[' x-forwarded-for ' | | req.headers[' X-real-ip ') to get the client IP
Note: If you still use Req.connection.remoteAddress to obtain the IP, we find that the 127.0.0.1 is obtained.
Nginx reverse proxy node. js Get Client IP