gan*_*est 5 nginx cache redirect
我想知道是否有办法控制 Nginx 中重定向的生命周期?
我们会在 CDN 中缓存 301 重定向一段特定的时间,比如说 20 分钟,并且 CDN 由标准缓存标头控制。默认情况下,Nginx 重定向没有 Cache-control 或 Expires 指令。这可能会导致重定向被缓存很长时间。通过具有特定的重定向生命周期,系统可以有机会自我纠正,因为知道即使是“永久”重定向也会不时发生变化。
另一件事是,这些重定向包含在 Server 块中,根据 nginx 规范,应在位置之前对其进行评估。
我尝试添加 add_header Cache-Control "max-age=1200, public"; 到重定向文件的底部,但问题是缓存控制被添加了两次 - 第一个来自后端脚本,另一个是由 add_header 指令添加的。
在 Apache 中,有一个环境变量技巧来控制重写的标头:
RewriteRule /taxonomy/term/(\d+)/feed /taxonomy/term/$1 [R=301,E=expire:1] 标头始终设置 Cache-Control“store, max-age=1200” env=expire
但我不知道如何在 Nginx 中实现这一点。
您是否在 nginx 配置中尝试过 Cache-Control 标志?
配置示例:
upstream yourappserver{
server 0.0.0.0:6677;
}
proxy_cache_path /tmp/cache levels=1:2 keys_zone=my-test-cache:8m max_size=5000m inactive=300m;
server {
listen 80;
server_name your.domain.tld;
root /path/to/the/document/root/;
access_log /var/log/nginx/access.log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_cache my-test-cache;
proxy_cache_valid 200 404 1m;
proxy_cache_valid 302 20m;
proxy_cache_use_stale error timeout invalid_header updating;
proxy_redirect off;
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if (!-f $request_filename) {
proxy_pass http://yourappserver;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为您正在寻找这个特定的配置片段
proxy_cache_valid 302 20m;
Run Code Online (Sandbox Code Playgroud)