I'm trying to cache the request based on the response header. Right now my desire condition is if the response has both 2 headers client-device and client-location, then the response should be cached on nginx side
So I tried with this code
if ($http_client_device && $http_clocation) {
set $cache_key "$request_uri$http_client_device$http_client_location";
}
proxy_cache_key $cache_key;
but nginx doesn't allow that nginx: [emerg] unexpected "&&" in condition in...
anyway to work around for this one? thanks in advance
The Nginx add_header directive allows you to define an arbitrary response header and value to be included in all response codes, which are equal to 200 , 201 , 204 , 206 , 301 , 302 , 303 , 304 , or 307 . This can be defined from within your nginx.
Through a simple command you can verify the status of the Nginx configuration file: $ sudo systemctl config nginx The output will show if the configuration file is correct or, if it is not, it will show the file and the line where the problem is.
By default, the configuration file is named nginx. conf and placed in the directory /usr/local/nginx/conf , /etc/nginx , or /usr/local/etc/nginx .
The map module of NGINX enables you to compare values of the NGINX variable against several different conditions before letting you link a new value to it according to the match. This makes the map module a more concise and elegant solution.
After searching around the whole day, I reach a work around way, reference to this topic http://rosslawley.co.uk/archive/old/2010/01/04/nginx-how-to-multiple-if-statements/
So in general my code will look like this:
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 ...;
}
but still wonder is there any cleaner way to do AND operator in nginx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With