Nginx反向caching301redirect(永久)

是否有可能让Nginxcaching所有301redirectproxy_pass请求?

例如:

请求#1:客户端A请求/某些/path – >发送到proxy_pass – >导致301redirect/some/other/path – > nginxcaching这个响应,因为它是301redirect。

请求#2:客户端B请求/ some / path – > nginx返回caching301redirect到/some/other/path

nginx已经可以caching301redirect了。 您可以更改使用proxy_cache_valid指令进行caching的时间:

 proxy_cache_valid 301 365d; # Cache permanent redirects for a whole year 

我发现@迈克尔的答案让我大部分的方式,但是当我打开proxy_cache ,它会自动caching文件与ExpiresCache-Control 。 所以我结束了这个解决scheme。

 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; 

如果你想redirect所有,你可以这样使用:

 location / { proxy_pass http://127.0.0.1:8080/VirtualHostBase/https/$server_name:443/some/path/VirtualHostRoot; } 

您的示例可能需要rewriteproxy_pass的组合:

 location / { rewrite ^(.*)$ /VirtualHostBase/https/$server_name:443/some/path/VirtualHostRoot$1 break; proxy_pass http://127.0.0.1:8080; } 

警告 :你有不同的重写redirect效果: break返回301而不是permanent返回302。

编辑上游这个模块/你需要使用proxy_cache_valid直接:

 proxy_cache_valid 301 1h; 

警告 ,与上游caching相关的指令优先于proxy_cache_valid值,具体顺序是:

  1. X-加速,到期
  2. 到期/caching控制
  3. proxy_cache_valid

参考: HttpProxyModule , Nginx反向代理+ URL重写