【问题标题】:how can I use AND operator in IF statement in nginx?如何在 nginx 的 IF 语句中使用 AND 运算符?
【发布时间】:2019-09-11 04:23:56
【问题描述】:

我正在尝试根据响应标头缓存请求。 现在我想要的条件是,如果响应同时具有 2 个标头 client-device 和 client-location,那么响应应该缓存在 nginx 端

所以我尝试了这段代码

if ($http_client_device && $http_clocation) {
    set $cache_key "$request_uri$http_client_device$http_client_location";
}
proxy_cache_key $cache_key;

但是 nginx 不允许这样做 nginx:[emerg] 出现意外的“&&”...

无论如何要解决这个问题? 提前谢谢

【问题讨论】:

    标签: if-statement nginx and-operator


    【解决方案1】:

    搜索了一整天后,我找到了一种解决方法,参考这个话题 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 运算符

    【讨论】:

    • 有时候我觉得 NGINX 太傻了。我最终得到了同样的结果。
    猜你喜欢
    • 1970-01-01
    • 2012-11-05
    • 2015-06-27
    • 2020-06-08
    • 2012-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    相关资源
    最近更新 更多