Использование оператора if с командой nginx mirror - PullRequest
0 голосов
/ 04 августа 2020

Как я могу написать оператор if в конфигурации NGINX? Вот как я это сделал.

   location = /mirror {
        internal;
        if ($upstream_cache_status = "HIT") {
            rewrite ^/([A-Za-z0-9]+) /stats/websiteStats break;
            proxy_pass http://localhost:3001;
        }
    }

Но это не работает. Вышеупомянутый оператор if никогда не становится true, хотя я проверил, что значение внутри этого заголовка - HIT. Что мне не хватает?

location / {
        mirror_request_body off;
        proxy_cache my_cache; # mapping the path
        proxy_cache_key "${server_name}"; # adding a key, to find the specific cache
        proxy_cache_valid 200 1m; # cache only if 200 and expires in 120min
        proxy_cache_methods GET HEAD; # methods to cache on
        proxy_cache_min_uses 1; # no.of rallies excess to cache it
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; 
        mirror /mirror;

       proxy_pass http://localhost:3001;
       proxy_set_header Host $host;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       add_header Last-Modified $date_gmt;
       set $me "HIT"; 
       if ($upstream_cache_status = $me) {
                add_header X-uri $upstream_cache_status;
       }

      location = /mirror {
        internal;
        if ($upstream_cache_status = "HIT") {
            rewrite ^/([A-Za-z0-9]+) /stats/websiteStats break;
            proxy_pass http://localhost:3001;
        }
    }

}
...