找回密码
 立即注册
CeraNetworksBGVM服务器主机交流IP归属甄别会员请立即修改密码
查看: 47|回复: 5

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

[复制链接]

341

主题

879

回帖

3809

积分

论坛元老

积分
3809
发表于 2023-4-27 00:42:55 | 显示全部楼层 |阅读模式
这是套了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

  • 回复

    使用道具 举报

    24

    主题

    7070

    回帖

    1万

    积分

    论坛元老

    积分
    15378
    发表于 2023-4-27 00:48:06 | 显示全部楼层
    有现成的ipinfo.io还整这些花里胡哨的
    回复

    使用道具 举报

    48

    主题

    1083

    回帖

    3010

    积分

    论坛元老

    积分
    3010
    发表于 2023-4-27 00:44:31 | 显示全部楼层
    用了很多年了

    回复

    使用道具 举报

    22

    主题

    42

    回帖

    288

    积分

    中级会员

    积分
    288
    发表于 2023-4-27 00:48:07 | 显示全部楼层
    很有用 马克了

    回复

    使用道具 举报

    5

    主题

    157

    回帖

    887

    积分

    高级会员

    积分
    887
    发表于 2023-4-27 08:15:52 | 显示全部楼层
    收藏
    回复

    使用道具 举报

    12

    主题

    91

    回帖

    374

    积分

    中级会员

    积分
    374
    发表于 2023-4-27 08:37:27 | 显示全部楼层
    有用。收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|手机版|小黑屋|HS2V主机综合交流论坛

    GMT+8, 2024-5-1 11:09 , Processed in 0.178351 second(s), 3 queries , Gzip On, Redis On.

    Powered by Discuz! X3.5

    © 2001-2023 Discuz! Team.

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