NGINX + Lua: define a chave de cache com base nos cabeçalhos de resposta do fluxo ascendente

1

Estou tentando controlar a chave do cache usando o valor do campo de cabeçalho de resposta upstream. O campo de cabeçalho nas perguntas é Common-Api na configuração abaixo.

Eu continuo recebendo nginx: [emerg] unknown "ck" variable . Alguma ideia do que estou fazendo errado?

 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;


        }
    
por Madhur Ahuja 26.12.2016 / 19:31

1 resposta

1

Uma variável nginx que você tenta usar em Lua já deve existir primeiro. Mas você tentou usar uma variável sem criá-la:

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

e

                    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";

No início do mesmo bloco location ou server contendo seu script Lua, você pode criá-lo com, por exemplo:

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

Na documentação :

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.

    
por 26.12.2016 / 20:35

Tags