如何将no-cache标头添加到由apache和nginx提供的所有404页面?

我最近在切换到Cloudflare后遇到了一个问题,解决scheme是基本上阻止Cloudflarecaching404响应。

在我们的负载均衡的多服务器设置中,偶尔会出现404s,但是它们可以通过rsync(通过lsyncd)快速修复。 在Cloudflare之前,随着rsync的工作,对404ed文件的重新请求将很快变为200。

但是,由于Cloudflarecaching基于caching头的所有数据,并且apache和nginx都没有发送404caching头,所以Cloudflare最终caching了404响应。

我一直在寻找一个解决scheme,在apache和nginx(全球所有托pipe域名)上为404s全局添加这样一个头文件,但到目前为止都是空白的。

谁能帮忙?

谢谢。

难道你不能通过使用error_page指令,然后处理单独的位置添加标题?

例如在Nginx中:

server { ... error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; add_header Cache-Control "no-cache" always; } } 

你也可以这样做:

 map $status $cache_header { default <for_other_codes>; 404 "no-cache"; } server { [ ... ] add_header "Cache-Control" $cache_header always; } 

我在这个问题上的五分钱 –

在我们的PHP项目中,我们只有几个404页面,所以我决定使用PHP header()函数在PHP级别上执行此操作