Nginx 中获取cloudflare保护的网站的访问者的真正IP

一般来说,有三个参数可以使用:

  1. CF-Connecting-IP, 在nginx log中为 cf-connecting-ip.
  2. True-Client-IP, 仅给Cloudflare 企业用户使用,在nginx log中为true-client-ip
  3. X-Forwarded-For, 在nginx log 中比较常见,为x-forwarded-for. X-Forwarded-For 其实是一个数组,按顺序记录了用户的真正的IP和用户使用的Proxy.

举例说明:

X-Forwarded-For: 203.0.113.1 

这个表示用户的真实IP是203.0.113.1

X-Forwarded-For: 198.51.100.101,198.51.100.102,203.0.113.1

这个表示用户的真实IP是203.0.113.1,然后依次经过了198.51.100.102和198.51.100.101代理,然后才访问到了CF的edge 节点

综上所述, CF 推荐使用CF-COnnecting-IP 和 True-Client-IP 两种header,因为他们能够保证他们的value只有一个IP

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.