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

这是套了CF的用法($http_cf_connecting_ip)

location /ip {
add_header Content-Type text/plain;
return 200 $http_cf_connecting_ip;}

这是套了CDN(包括CF)/有反代($http_x_forwarded_for)

location /ip {
add_header Content-Type text/plain;
return 200 $http_x_forwarded_for;}

啥也没套直接用$remote_addr

location /ip {
add_header Content-Type text/plain;
return 200 $remote_addr;}

也可以这样(三个按顺序匹配,你们就用这个吧)

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;}}

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

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

© 版权声明
THE END
如果喜欢,可以【点赞】【分享】【收藏】
点赞15赞赏 分享
评论 抢沙发
头像
人生坎坎,山山而峦,不过尔尔;心路漫漫,水水而川,如此悠悠
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容