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