负载均衡器后面的 nginx $scheme 变量 [英] nginx $scheme variable behind load balancer
问题描述
如果 nxinx 在负载均衡器后面运行,是否可以将 nginx $scheme 值强制为https"?
Is it possible to force nginx $scheme value to "https" if nxinx is running behind load balancer?
在我的场景中,负载均衡器负责与客户端的 https 通信,并将请求作为原始 http 转发到 nginx.我知道我可以做这样的事情来检测 https
In my scenario Load balancer takes care of https communication with client and forwards requests to nginx as raw http. I know I can do something like this to detect https
set $my_scheme "http";
if ($http_x_forwarded_proto = "https") {
set $my_scheme "https";
}
但我只是好奇是否有类似 real_ip_header
的 IP 函数.
but I'm just curious if there is something like real_ip_header
function for IPs.
手动检测 https 时是否还需要更新一些标头?
Are there also some headers I need to update when detecting https manualy?
推荐答案
我们的设置和你的一样,只是使用 map
而不是 if/set
(推荐来自 nginx 开发者).
Our setup is the same as yours, only using map
instead of if/set
(as recommended by the nginx devs).
# Sets a $real_scheme variable whose value is the scheme passed by the load
# balancer in X-Forwarded-Proto (if any), defaulting to $scheme.
# Similar to how the HttpRealIp module treats X-Forwarded-For.
map $http_x_forwarded_proto $real_scheme {
default $http_x_forwarded_proto;
'' $scheme;
}
附言我同意,real_scheme
模块会很好!
P.S. I agree, a real_scheme
module would be nice!
这篇关于负载均衡器后面的 nginx $scheme 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!