[英]how can I use AND operator in IF statement in nginx?
我正在嘗試根據響應 header 緩存請求。 現在我想要的條件是,如果響應有兩個標頭客戶端設備和客戶端位置,那么響應應該緩存在 nginx 端
所以我嘗試了這段代碼
if ($http_client_device && $http_clocation) {
set $cache_key "$request_uri$http_client_device$http_client_location";
}
proxy_cache_key $cache_key;
但是 nginx 不允許在...
無論如何要解決這個問題? 提前致謝
經過一整天的搜索,我找到了解決該問題的方法,請參考此主題http://rosslawley.co.uk/archive/old/2010/01/04/nginx-how-to-multiple-if-statements/
因此,一般而言,我的代碼如下所示:
if ($http_client_device) {
set $temp_cache 1;
}
if ($http_client_location) {
set $temp_cache 1$temp_cache;
}
if ($temp_cache = 11) {
set $cache_key ...;
}
但是仍然想知道在nginx中還有沒有更清潔的方式做AND運算符
我使用這種語法
set $and 1;
if (<not condition>) {
set $and 0;
}
if (<not condition>) {
set $and 0;
}
if ($and) {
# ...
}
例如:
if ($arg_a = 1 && $arg_b = 2) { ... }
可以實現
set $ab 1
if ($arg_a != 1) {
set $ab 0;
}
if ($arg_b != 2) {
set $ab 0;
}
if ($ab) {
# ...
}
參考: Nginx
if
,and
對於您的情況
set $set_cache_key 1;
if ($http_client_device = "") {
set $set_cache_key 0;
}
if ($http_clocation = "") {
set $set_cache_key 0;
}
if ($set_cache_key) {
set $cache_key "$request_uri$http_client_device$http_client_location";
}
proxy_cache_key $cache_key;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.