Is it possible to have Nginx cache all 301
redirect requests for proxy_pass
?
For example:
Request #1: Client A requests /some/path -> sent to proxy_pass
-> results in 301 redirect to /some/other/path
-> nginx caches this response since it's a 301 redirect.
Request #2: Client B requests /some/path -> nginx returns cached 301 redirect to /some/other/path
.
nginx already can cache 301 redirects. You can change the amount of time they are cached with the proxy_cache_valid directive:
I found that @Michael's answer got me most of the way there, but when I turned on
proxy_cache
, it would automatically cache files withExpires
andCache-Control
. So I ended up with this solution.If you want redirect all, you could use this way:
Your examples could need the combination of
rewrite
andproxy_pass
:Warning: you have got different rewrite
redirect
effect:break
returns 301 insteadpermanent
returns 302.EDIT Upstream this module/s you need use
proxy_cache_valid
diretive:Warning, upstream cache-related directives have priority over proxy_cache_valid value, in particular the order is:
Reference: HttpProxyModule, Nginx reverse proxy + URL rewrite