NGINX + Lua : set cache key based on upstream Response headers

luanginx

I am trying to control the cache key using the upstream response header field value. The header field in questions is Common-Api in the configuration below.

I keep getting nginx: [emerg] unknown "ck" variable . Any idea what I am doing wrong?

 location ~^/(deals-new)/ {
            set $no_cache 0;
            proxy_cache helpchat_cache;

            proxy_cache_min_uses 1;
            #proxy_cache_valid  200 302 10m;
            proxy_cache_valid  404 1m;
            #proxy_cache_lock on;
            proxy_cache_use_stale error timeout updating invalid_header http_500 http_502 http_503 http_504;

            client_max_body_size 25M;
            #auth_basic "closed website";
            #auth_basic_user_file /etc/nginx/htpasswd;
            root   /usr/share/nginx/html;
            index  index.html index.htm;

            # if ($request_uri ~* ".(jsp|sh|pl|jsp|sh|pl|jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)(\?v=[0-9.]+)?$") {
            #   return 404;
            #   break;
            # }

            #Proxy all the requests to Tomcat
            proxy_set_header X-Forwarded-Host $host;
            proxy_set_header X-Forwarded-Server $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            # Go to next upstream after if server down.
            #proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
            #proxy_cache_methods GET HEAD POST;
            proxy_ignore_headers Expires Cache-Control;
            proxy_set_header Accept-Language 'en-US';
            add_header X-Upstream $upstream_addr always;

            add_header X-Cache-Status $upstream_cache_status always;
            add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
            #add_header X-Cache-Expiry $http_x_accel_expires;
            #Pass to tomcat backend
            proxy_pass http://tomcatcluster;
            if ($cors = true ) {
                add_header Access-Control-Allow-Origin "$http_origin"; # <- needs to be updated
                add_header Access-Control-Allow-Methods "GET, OPTIONS";
                add_header Access-Control-Allow-Headers "Authorization,User-Agent,Keep-Alive,Content-Type,x-akosha-auth";   # <- You may not need this...it's for Basic Auth
                add_header Access-Control-Allow-Credentials "true";        # <- Basic Auth stuff, again
            }
            if ($request_method = OPTIONS ) {
                add_header Access-Control-Allow-Origin "$http_origin"; # <- needs to be updated
                add_header Access-Control-Allow-Methods "GET, OPTIONS";
                add_header Access-Control-Allow-Headers "Authorization,User-Agent,Keep-Alive,Content-Type,x-akosha-auth";   # <- You may not need this...it's for Basic Auth
                add_header Access-Control-Allow-Credentials "true";        # <- Basic Auth stuff, again
                return 200;
            }

             header_filter_by_lua_block {

                ngx.header["test"] = "madhur"
                ngx.header["test1"]=ngx.resp.get_headers()['Common-Api'] 
                if ngx.resp.get_headers()['Common-Api'] == "true" then
                    ngx.var.proxy_cache_key = "$http_x_device_type$http_x_app_version_code$http_origin_api_cache_$user_agent_helpchat$host$request_uri";
                    ngx.var.ck = "$http_x_device_type$http_x_app_version_code$http_origin_api_cache_$user_agent_helpchat$host$request_uri";
                end

                if ngx.resp.get_headers()['Common-Api'] == nil then
                    ngx.var.proxy_cache_key = "$http_x_device_type$http_x_app_version_code$http_x_akosha_auth$http_x_helpchat_auth$http_origin_api_cache_$user_agent_helpchat$host$request_uri";
                    ngx.var.ck = "$http_x_device_type$http_x_app_version_code$http_x_akosha_auth$http_x_helpchat_auth$http_origin_api_cache_$user_agent_helpchat$host$request_uri";
                end

            }
            add_header X-Cache-Key $ck always;


        }

Best Answer

An nginx variable that you attempt to use in Lua must already exist first. But you tried to use a variable without creating it:

                    ngx.var.ck = "$http_x_device_type$http_x_app_version_code$http_origin_api_cache_$user_agent_helpchat$host$request_uri";

and

                    ngx.var.ck = "$http_x_device_type$http_x_app_version_code$http_x_akosha_auth$http_x_helpchat_auth$http_origin_api_cache_$user_agent_helpchat$host$request_uri";

At the beginning of the same location or server block containing your Lua script, you can create it with, e.g.:

location ~^/(deals-new)/ {
    set $ck "";

From the documentation:

Note that only already defined nginx variables can be written to. For example:

location /foo {
    set $my_var ''; # this line is required to create $my_var at config time
    content_by_lua_block {
        ngx.var.my_var = 123;
        ...
    }
}

That is, nginx variables cannot be created on-the-fly.