Nginx : Response handling and Reverse Proxy with Cache
I am running Nginx as an simple Reverse Proxy cum cache.
Now I want to know set some headers based on the response received(body
not header).
It looked simple to get the location_capture_by LUA do that but seems I
cant get the caching working properly.
Initial set up was :
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 what I found that I have lost all the original headers an header added
as part of Proxy_Header handling including the upstream_cache_status
header. However I found that Nginx still serves repeat requests from the
cache itself.
Any reason why it would be so : also I am a bit of early starter here : so
excuse for some basic gotchas.
No comments:
Post a Comment