Nginx: response processing and reverse proxy with cache

I am running Nginx as a simple reverse proxy cache, and now I want to set some headers based on the received response (body not header).

It looks like location_capture_by Lua does this, but it seems like I can't get the caching to work correctly.

Initial setup:

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

changed it to:


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

======

To the fact that I found that I lost all the original headers, the header was added as part of the Proxy_Header processing, including the upstream_cache_status header. However, I found that Nginx still makes repeated requests from the cache itself.

Any reason why this is so? I also start a little early here, so sorry for some basic mistakes.

+4
source share
1 answer

In fact, location.capture is not intended to do what you are doing, but if I get you right (you want to send the headers that the browser sends you to the subquery), you can probably hack it using ngx. ctx + set;)

But I would say that this is a very dirty curly way.

0
source

Source: https://habr.com/ru/post/1500737/


All Articles