Nginx:响应处理和反向代理与缓存

4
我正在使用Nginx作为简单的反向代理缓存,并且现在我想根据接收到的响应(而不是头部)设置一些标头。
看起来可以通过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仍然从缓存本身提供重复请求。

有什么原因会这样吗?另外,我在这里是一个初学者,请谅解一些基本问题。

1个回答

0

实际上,location.capture 不是为你所做的事情而设计的。但是,如果我理解正确(你想发送浏览器发送给你的标头到子请求),你可能可以使用 ngx.ctx + set 来绕过它;)

但我要说这是一种非常肮脏的方式。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接