Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I use AND operator in IF statement in nginx?

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

like image 421
zadops Avatar asked Sep 11 '19 04:09

zadops


People also ask

What is Add_header in nginx?

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.

How do I test 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.

Where is Nginx config file located?

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 .

What is map in 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.


1 Answers

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

like image 196
zadops Avatar answered Sep 16 '22 12:09

zadops