nginx设置header使用谓词
proxy_set_header
,比如proxy_set_header X-Real-IP $request_addr
,但是如何实现只有当X-Real-IP
没有值时才设置为$request_addr
,如果有值则直接透传呢?可以使用map指令
map 指令是由 ngx_http_map_module 模块提供的,默认情况下安装 nginx 都会安装该模块。
map $key $value {
"china" "中国",
"usa" "美国",
default "其他"
}
http {
...
map $http_x_real_ip $real_ip {
default $http_x_real_ip;
"" $remote_addr;
}
server {
location / {
proxy_set_header X-Request-ID $real_ip;
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有