NGINX + Lua:根据上游响应标头设置缓存键

NGINX + Lua:根据上游响应标头设置缓存键

我正在尝试使用上游响应标头字段值来控制缓存键。问题中的标头字段位于Common-Api下面的配置中。

我一直这样nginx: [emerg] unknown "ck" variable。知道我做错了什么吗?

 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;


        }

答案1

您尝试在 Lua 中使用的 nginx 变量必须首先已存在。但您尝试使用变量而未创建它:

                    ngx.var.ck = "$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_x_akosha_auth$http_x_helpchat_auth$http_origin_api_cache_$user_agent_helpchat$host$request_uri";

location在包含你的 Lua 脚本的相同或块的开头server,你可以使用以下命令创建它,例如:

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

来自文档

请注意,只有已定义的 nginx 变量才可以写入。例如:

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;
        ...
    }
}

也就是说,nginx 变量不能动态创建。

相关内容