Nginx reverse proxy node. js Get Client IP

Source: Internet
Author: User
Tags nginx reverse proxy

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.