Nginx reverse cache 301 redirects (permanent)
13,046
Solution 1
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 with Expires
and Cache-Control
. So I ended up with this solution.
proxy_cache_path /tmp/nginx levels=1:2 keys_zone=main-cache:8m max_size=1000m inactive=600m;
proxy_temp_path /tmp/nginx/tmp;
proxy_cache main-cache;
proxy_cache_valid 301 60m;
proxy_cache_key "$scheme://$host$request_uri";
proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
Solution 2
nginx already can cache 301 redirects. You can change the amount of time they are cached with the proxy_cache_valid directive:
proxy_cache_valid 301 365d; # Cache permanent redirects for a whole year
Related videos on Youtube
Comments
-
Kendall Hopkins over 1 year
Is it possible to have Nginx cache all
301
redirect requests forproxy_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
.