nginx的获取ip的网页,只需要几行代码

/ 0评 / 0

套了CF的用法($http_cf_connecting_ip)
location /ip {<br>add_header Content-Type text/plain;<br>return 200 $http_cf_connecting_ip;}

套了CDN(包括CF)/有反代($http_x_forwarded_for)
location /ip {<br>add_header Content-Type text/plain;<br>return 200 $http_x_forwarded_for;}

直接用$remote_addr
location /ip {<br>add_header Content-Type text/plain;<br>return 200 $remote_addr;}

也可以这样(三个按顺序匹配,你们就用这个吧)
location /ip {<br>add_header Content-Type text/plain;<br>if ($http_cf_connecting_ip != ""){return 200 $http_cf_connecting_ip;}<br>if ($http_x_forwarded_for != ""){return 200 $http_x_forwarded_for;}<br>if ($remote_addr != ""){return 200 $remote_addr;}}

加在你的网站配置里就行了,/ip可以自己改成想要的

支持ipv4/ipv6(但示例没解析ipv6)