简体   繁体   中英

Get client ip address with nginx lua

I have a lua script for nginx and would like to get the client's IP address.

Hovever

ngx.var.remote_addr is 10.0.2.2 

in my case but if I check my ip address, it is 86.123.XXX.XXX

How can I get the 86.123.XXX.XXX address with nginx lua?

Looks like your current Nginx is behind a reverse proxy. You need to pass user IP through reverse proxy, something like this:

    location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass http://YOU_CURRENT_SERVER;
    }

After that, you can get user IP from header X-Real-IP or X-Forwarded-For , or $http_x_real_ip .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM