Skip to content

Instantly share code, notes, and snippets.

@CooperLuan
Created May 9, 2015 08:44
Show Gist options
  • Select an option

  • Save CooperLuan/1be80782955d69cc4a51 to your computer and use it in GitHub Desktop.

Select an option

Save CooperLuan/1be80782955d69cc4a51 to your computer and use it in GitHub Desktop.
[博客摘抄] HTTP 请求头中的 X-Forwarded-For

HTTP 请求头中的 X-Forwarded-For

X-Forwarded-For 是由 Squid 引入的, 被引入 RFC7239 标准中,X-Forwarded-For 格式

X-Forwarded-For: client, proxy1, proxy2

值代表 离服务端最远的设备 IP + 每一级代理设备的 IP

上面的 XFF 代表访问由 client 发起,经过了 proxy1,proxy2 两台代理,此外还有终端代理 proxy3, 它的 IP 在 Remote Address

假设 proxy1/2/3 的 IP 分别为 IP1/1/3, proxy3 直连服务器,会给 XFF 追加 IP2, IP3 则通过 Remote Address 获取

Remote Address 无法伪造,因为建立 TCP 连接需要三次握手,如果伪造了源 IP 就无法建立 TCP 连接

一般 app 获取 Remote Address 是通过请求头 X-Real-IP, 而请求头是可以伪造的,

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;

    proxy_pass http://127.0.0.1:9009/;
    proxy_redirect off;
}

相关引用

  • HTTP 相关
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment