NGINX + Lua:根据上游Response头设置caching键

我正在尝试使用上游响应头字段值来控制caching键。 问题中的标题字段在下面的configuration中是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; } 

您尝试在Lua中使用的nginxvariables必须先存在。 但是你试图使用一个variables而不创build它:

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

在包含你的Lua脚本的相同locationserver块的开始处,可以使用如下创build它:

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

从文档 :

请注意,只有已经定义的nginxvariables才能被写入。 例如:

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

也就是说,nginxvariables不能被即时创build。