nginx设置"X-Forwarded-For" header有什么用

What's the purpose of setting "X-Forwarded-For" header in nginx

我的 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 header 允许 Django API 确定原始客户端的 IP 地址(而不是代理的 IP 地址)。

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

来自Mozilla docs

The X-Forwarded-For (XFF) header is a de-facto standard header for identifying the originating IP address of a client connecting to a web server through an HTTP proxy or a load balancer. When traffic is intercepted between clients and servers, server access logs contain the IP address of the proxy or load balancer only. To see the original IP address of the client, the X-Forwarded-For request header is used.

其实我觉得你误会了Hostheader。我的理解是nginx服务器的IP。