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

查看 66|回复 3
作者:嗷嗷   
这是套了CF的用法($http_cf_connecting_ip)
[ol]location /ip {
add_header Content-Type text/plain;
return 200 $http_cf_connecting_ip;}[/ol]复制代码
这是套了CDN(包括CF)/有反代($http_x_forwarded_for)
[ol]location /ip {
add_header Content-Type text/plain;
return 200 $http_x_forwarded_for;}[/ol]复制代码
啥也没套直接用$remote_addr
[ol]location /ip {
add_header Content-Type text/plain;
return 200 $remote_addr;}[/ol]复制代码
也可以这样(三个按顺序匹配,你们就用这个吧)
[ol]location /ip {
add_header Content-Type text/plain;
if ($http_cf_connecting_ip != ""){return 200 $http_cf_connecting_ip;}
if ($http_x_forwarded_for != ""){return 200 $http_x_forwarded_for;}
if ($remote_addr != ""){return 200 $remote_addr;}}[/ol]复制代码
加在你的网站配置里就行了,/ip可以自己改成想要的
支持ipv4/ipv6(但示例没解析ipv6)
示例 haoduck.com/ip

这是, 代码, 示例

Mio   
用了很多年了
HOH   
有现成的ipinfo.io还整这些花里胡哨的
seeindo4   
很有用 马克了
您需要登录后才可以回帖 登录 | 立即注册

返回顶部