[经验] nginx的获取ip的网页,只需要几行 只看楼主

这是套了CF的用法($http_cf_connecting_ip)
  1. location /ip {
  2. add_header Content-Type text/plain;
  3. return 200 $http_cf_connecting_ip;}



这是套了CDN(包括CF)/有反代($http_x_forwarded_for)
  1. location /ip {
  2. add_header Content-Type text/plain;
  3. return 200 $http_x_forwarded_for;}


啥也没套直接用$remote_addr
  1. location /ip {
  2. add_header Content-Type text/plain;
  3. return 200 $remote_addr;}


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



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

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

示例 haoduck.com/ip

  • 推荐 HOH
  • 2023-4-27 00:48:06
有现成的ipinfo.io还整这些花里胡哨的
  • 2# Mio
  • 2023-4-27 00:44:31
用了很多年了
很有用 马克了
收藏
  • 6# 234
  • 2023-4-27 08:37:27
有用。收藏
解析ipv6怎么添加啊
  • 8# whl32
  • 2023-4-27 10:00:34
这个方法好,我之前还用PHP写了几行
本帖最后由 wang3y2 于 2023-4-27 10:36 编辑

返回的是代理IP啊
  • 10# colla
  • 2023-4-27 12:37:59
引用: wang3y2 发表于 2023-4-27 10:34
返回的是代理IP啊

这是获取访问网站的IP,你用代理访问就是代理IP
12下一页