问题
I’m finding that Nginx does not have features that I would consider basic, like send an Age header from proxy cache.
In order to don’t let a browser store a cache which is max-age 60min, for 60min more if it’s already 59min old. I want to send an Age Header. (I prefer to use the same max-age for browsers and proxies)
I came up with this solution, but I feel is a little bit ghetto, so I would like your opinions.
I had to use LUA and then map{} because there was no other way I could make any LUA block access to that $upstream variable, and it worked only with map, which as a gift provides some safety, because if the upstream date is not set as a correct time string, the age calculation will crash the request.
map $upstream_http_Date $mapdate {
default $upstream_http_Date;
'' 'Sat, 21 Dec 2019 00:00:00 GMT';
}
Inside location:
header_filter_by_lua_block {
ngx.header["Age"] = ngx.time() - ngx.parse_http_time(ngx.var.mapdate);
}
This question has been asked before, but never answered, and my solution, is the only working solution that I know at this point.
I know Varnish is more powerful, a possible answer cloud be “Use Varnish”, but let’s focus on Nginx for this question, Cloudflare had a good run with it.
And obviously I'm looking for any improvements to my solution or a completely different approach if you have a better one.
来源:https://stackoverflow.com/questions/59437878/nginx-add-age-header-with-lua-is-this-a-good-solution