我正在尝试使用上游响应标头字段值来控制缓存键。有问题的标题字段在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 中使用的 nginx 变量必须首先已经存在。但是您尝试使用变量而不创建它:
和
在包含你的 Lua 脚本的相同
location
或server
块的开头,你可以创建它,例如:从文档中: