全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 3624|回复: 18

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

  [复制链接]
发表于 2023-4-27 00:42:55 | 显示全部楼层 |阅读模式
这是套了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

发表于 2023-4-27 00:48:06 | 显示全部楼层
有现成的ipinfo.io还整这些花里胡哨的
发表于 2023-4-27 00:44:31 来自手机 | 显示全部楼层
用了很多年了
发表于 2023-4-27 00:48:07 | 显示全部楼层
很有用 马克了
发表于 2023-4-27 08:48:18 | 显示全部楼层
解析ipv6怎么添加啊
发表于 2023-4-27 10:00:34 | 显示全部楼层
这个方法好,我之前还用PHP写了几行
发表于 2023-4-27 10:34:40 | 显示全部楼层
本帖最后由 wang3y2 于 2023-4-27 10:36 编辑

返回的是代理IP啊
发表于 2023-4-27 12:37:59 | 显示全部楼层
wang3y2 发表于 2023-4-27 10:34
返回的是代理IP啊

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

点评

wcn
为啥要获取网站ip?不明白这个需求  发表于 2023-4-27 21:19
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2024-4-28 22:02 , Processed in 0.063117 second(s), 11 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表