Nginx: Add “Age” header, with Lua. Is this a good solution?

柔情痞子 提交于 2019-12-24 18:29:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!