Change cache behavior

378 views
Skip to first unread message

Guilherme

unread,
Feb 1, 2016, 1:50:51 PM2/1/16
to openre...@googlegroups.com
Hello,

Actually I'm using proxy_cache_valid, proxy_ignore_headers with named locations to control cache behavior in openresty.

Now I need to change this in a more dynamic approach, so I'm trying to overwrite/set Cache-Control  header sent from upstream server.

I tried to do this in header_filter_by_lua but its not working properly. It overwrites Cache-Control, but  nginx does not change cache behavior. I think that I doing this in a wrong phase.

Minimal conf:

location / {

proxy_cache     static;
proxy_cache_key     $scheme$proxy_host$uri$is_args$args;
proxy_ignore_headers    Set-Cookie;
proxy_hide_header   Set-Cookie;

header_filter_by_lua_block {
 ngx.header["Cache-Control"] = nil
 ngx.header["Cache-Control"] = "public, max-age=5"
 ngx.header["X-Cache"] = ngx.var.upstream_cache_status
}

# I tried to use more_set_headers and it didnt work as well
# more_set_headers 'Cache-control: public, max-age=5';
proxy_pass http://1.1.1.1;

}


# curl localhost -i
Content-Type: text/html; charset=utf-8
Content-Length: 29435
Connection: keep-alive
* Cache-Control: public, max-age=5
** X-Cache: MISS

* Original Cache-Control header from upstream server was "no-cache"
** nginx never caches this request 

If I change this minimal conf in this way, everything works fine:

-proxy_ignore_headers    Set-Cookie;
+proxy_ignore_headers    Set-Cookie Cache-Control;
+proxy_hide_header   Cache-Control;
+proxy_cache_valid 200 404 5s;
 
-header_filter_by_lua_block {
- ngx.header["Cache-Control"] = nil
- ngx.header["Cache-Control"] = "public, max-age=5"
- ngx.header["X-Cache"] = ngx.var.upstream_cache_status
-}


Where should I change header Cache-Control to instruct nginx how to cache these requests?


# nginx -V
nginx version: openresty/1.9.7.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/opt/openresty/nginx --with-debug --with-cc-opt='-DNGX_LUA_USE_ASSERT -DNGX_LUA_ABORT_AT_PANIC -O2 -I/usr/lib64/perl5/CORE -O2 -Wno-deprecated-declarations' --add-module=../ngx_devel_kit-0.2.19 --add-module=../echo-nginx-module-0.58 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.29 --add-module=../form-input-nginx-module-0.11 --add-module=../encrypted-session-nginx-module-0.04 --add-module=../srcache-nginx-module-0.30 --add-module=../ngx_lua-0.10.0 --add-module=../ngx_lua_upstream-0.04 --add-module=../headers-more-nginx-module-0.29 --add-module=../array-var-nginx-module-0.04 --add-module=../memc-nginx-module-0.16 --add-module=../redis2-nginx-module-0.12 --add-module=../redis-nginx-module-0.3.7 --add-module=../rds-json-nginx-module-0.14 --add-module=../rds-csv-nginx-module-0.07 --with-ld-opt=-Wl,-rpath,/opt/openresty/luajit/lib --user=nginx --group=nginx --with-pcre-jit --with-http_stub_status_module --with-http_gzip_static_module --with-http_geoip_module --with-http_image_filter_module --with-http_realip_module --with-http_v2_module --with-http_ssl_module


Tks,

Guilherme

Yichun Zhang (agentzh)

unread,
Feb 3, 2016, 8:08:21 PM2/3/16
to openresty-en
Hello!

On Mon, Feb 1, 2016 at 10:50 AM, Guilherme wrote:
> I tried to do this in header_filter_by_lua but its not working properly. It
> overwrites Cache-Control, but nginx does not change cache behavior. I think
> that I doing this in a wrong phase.
>
[...]
> proxy_cache static;
> proxy_cache_key $scheme$proxy_host$uri$is_args$args;
[...]
>
> header_filter_by_lua_block {
> ngx.header["Cache-Control"] = nil
> ngx.header["Cache-Control"] = "public, max-age=5"
> ngx.header["X-Cache"] = ngx.var.upstream_cache_status
> }
>
> # I tried to use more_set_headers and it didnt work as well
> # more_set_headers 'Cache-control: public, max-age=5';
> proxy_pass http://1.1.1.1;

Nope, both header_filter_by_lua* and more_set_headers run way after
proxy_cache. The proxy_cache is in the content handler anyway.

We're planning to opensource the cache_by_lua* directives in ngx_lua
soon in which context you can do such kind of dynamic control I
believe. Stay tuned :)

Best regards,
-agentzh

Nejc Lovrenčič

unread,
Sep 14, 2023, 2:33:32 AM9/14/23
to openresty-en
Hi!

Are there any updates on this? Will cache_by_lua* ever be available? I have a similar case where I want to modify response headers before caching takes place.

Thanks!

Best,
Nejc

četrtek, 4. februar 2016 ob 02:08:21 UTC+1 je oseba Yichun Zhang (agentzh) napisala:

Junlong li

unread,
Sep 29, 2023, 6:05:43 AM9/29/23
to openresty-en
There are not updates on this issue.
Reply all
Reply to author
Forward
0 new messages