【问题标题】:What's the purpose of setting "X-Forwarded-For" header in nginx在 nginx 中设置“X-Forwarded-For”标头的目的是什么
【发布时间】:2020-07-20 08:37:36
【问题描述】:

我的 Django 应用程序有以下 Nginx 配置:

upstream api {
    server localhost:8000;
}

server {
    listen 80;

    location / {
        proxy_pass http://api;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /staticfiles {
        alias /app/static/;
    }
}

我将此配置基于tutorial here。经过一番研究,似乎设置Host 标头允许Django API 确定原始客户端的IP 地址(而不是代理的IP 地址)。

X-Forwarded-For 标头有什么意义?我在 nginx 日志中看到一个名为 $http_x_forwarded_for 的字段,但我不确定它是否相关。

【问题讨论】:

    标签: django nginx x-forwarded-for


    【解决方案1】:

    来自Mozilla docs

    X-Forwarded-For (XFF) 标头是一个事实上的标准标头,用于识别通过 HTTP 代理或负载平衡器连接到 Web 服务器的客户端的原始 IP 地址。当客户端和服务器之间的流量被截获时,服务器访问日志仅包含代理或负载均衡器的 IP 地址。要查看客户端的原始IP地址,使用X-Forwarded-For请求头。

    其实我觉得你误解了Host这个标头。我的理解是,它将是nginx服务器的IP。

    【讨论】:

      猜你喜欢
      • 2021-06-08
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多