proxy_cache_convert_head
The 'proxy_cache_convert_head' directive controls whether a HEAD request can return cached data for a GET request. — NGINX HTTP Core
proxy_cache_convert_head
httpserverlocation
语法proxy_cache_convert_head on | off;
默认值off
上下文http, server, location
模块NGINX HTTP Core
参数flag
说明
'proxy_cache_convert_head' 指令用于 NGINX 配置中,以确定是否应基于相应的 GET 请求为 HEAD HTTP 请求提供缓存响应。该指令允许缓存系统在存在有效缓存版本时将 HEAD 请求在内部转换为 GET 请求,从而通过避免每次 HEAD 请求都从 upstream 重新获取数据来提升性能。对于大量使用 HEAD 请求的应用,这种行为尤其有用,使其能够受益于缓存,而无需 upstream 每次都生成新的响应。 当设置为 'on' 时,该功能会改变请求处理方式,可能使 HEAD 请求获得更快的响应。相反,如果设置为 'off',服务器不会为 HEAD 请求检索为 GET 请求生成的缓存响应,这可能导致 HEAD 请求的响应时间变长,因为它们需要直接访问 upstream。该指令可应用于包括 http、server 和 location 在内的多个上下文,在 NGINX 的不同 HTTP 配置中提供使用灵活性。
配置示例
http {
proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m;
server {
location / {
proxy_pass http://backend;
proxy_cache my_cache;
proxy_cache_convert_head on;
}
}
}⚠
确保上游服务器正确地对同一资源处理 GET 和 HEAD 请求。
⚠
注意,过度依赖缓存的 HEAD 响应在某些情况下可能导致数据过时。