简体   繁体   English

使用nginx lua获取客户端IP地址

[英]Get client ip address with nginx lua

I have a lua script for nginx and would like to get the client's IP address. 我有一个nginx的lua脚本,想得到客户端的IP地址。

Hovever 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 在我的情况下,但如果我检查我的IP地址,它是86.123.XXX.XXX

How can I get the 86.123.XXX.XXX address with nginx lua? 如何使用nginx lua获取86.123.XXX.XXX地址?

Looks like your current Nginx is behind a reverse proxy. 看起来你当前的Nginx落后于反向代理。 You need to pass user IP through reverse proxy, something like this: 您需要通过反向代理传递用户IP,如下所示:

    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 . 之后,您可以从标题X-Real-IPX-Forwarded-For$http_x_real_ip获取用户IP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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