在 nginx 中配置 x-forwarded-proto 协议传递,需要在 `location` 或 `server` 块中使用 `proxy_set_header` 指令将请求头设置为当前连接的协议。具体做法是添加 `proxy_set_header x-forwarded-proto $scheme;`,这样后端应用就能通过该头部识别客户端最初使用的是 http 还是 https。

X-Forwarded-Proto 头部如何被 Nginx 设置
Nginx 使用内置变量 `$scheme` 来获取当前请求的协议(http 或 https),并将其赋值给自定义的请求头。当 Nginx 作为反向代理时,它会将这个值传递给上游服务器。

Nginx 配置与调优技能。支持反向代理、负载均衡、SSL/TLS 终止、高性能静态资源服务。触发词:Nginx 配置、代理设置、SSL 证书、性能调优、负载均衡、反向代理。
server {
listen 443 ssl;
server_name example.com;
location / {
proxy_pass http://backend_app;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
预期结果:当客户端通过 HTTPS 访问时,后端收到的 X-Forwarded-Proto 值为










