我正在使用Nginx作为简单的反向代理缓存,并且现在我想根据接收到的响应(而不是头部)设置一些标头。
看起来可以通过Lua的location_capture_by实现,但似乎我无法正确地使缓存工作。
最初的设置是:
看起来可以通过Lua的location_capture_by实现,但似乎我无法正确地使缓存工作。
最初的设置是:
location / {
..
try_files $uri @upstream_server
}
location @upstream_server {
proxy_pass "http://web_lb";
proxy_cache small;
proxy_cache_methods POST;
proxy_cache_key "$request_uri|$request_body";
client_max_body_size 500M;
add_header X-Cached $upstream_cache_status;
set_real_ip_from 10.86.102.0/24;
real_ip_header X-Forwarded-For;
proxy_ignore_headers Set-Cookie;
proxy_ignore_headers Cache-Control;
}
将其更改为:
location /{
content_by_lua '
local res = ngx.location.capture(
"/new-location",
{ method = ngx.HTTP_POST, body = ngx.var.request_body})
#update response body here and header etc based on content
ngx.say(res.body)
'; }
location new-location{
try_files $uri @upstream_server
}
location @upstream_server {
proxy_pass "http://web_lb;"
proxy_cache small;
proxy_cache_methods POST;
proxy_cache_key "$request_uri|$request_body";
client_max_body_size 500M;
add_header X-Cached $upstream_cache_status;
set_real_ip_from 10.86.102.0/24;
real_ip_header X-Forwarded-For;
proxy_ignore_headers Set-Cookie;
proxy_ignore_headers Cache-Control;
}
======
我发现我丢失了所有原始的头文件和作为代理标头处理的标头,包括上游缓存状态标头。然而,我发现Nginx仍然从缓存本身提供重复请求。
有什么原因会这样吗?另外,我在这里是一个初学者,请谅解一些基本问题。