2014-01-20 39 views
13

如果nxinx在负载平衡器后面运行,是否可以强制nginx $ scheme值为“https”?

在我的场景中,负载均衡器负责与客户端的https通信,并将请求作为原始http转发给nginx。我知道我可以做这样的事情来检测HTTPS

set $my_scheme "http"; 
if ($http_x_forwarded_proto = "https") { 
    set $my_scheme "https"; 
} 

,但我只是好奇,如果有类似real_ip_header功能的IP。

当检测https manualy时,是否还需要更新一些标题?

回答

18

我们的设置是一样的你,只用map代替if/set(所推荐的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; 
} 

P.S.我同意,一个real_scheme模块会很好!

0

尝试这样:

location/{ 
    ... 
    proxy_set_header X-Real-IP   $remote_addr; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header X-Forwarded-Proto $my_scheme; 
    proxy_set_header X-Forwarded-Ssl $my_ssl 
    ... 
}