大网数据广告活动
服务器运维问答

NGINX proxy default cache time with Cache-Control and no expiration

· 52CE 我爱测

I have caching enabled on an NGINX proxy. As I understand it, as long as I'm not ignoring the Cache-Control header in my proxy configuration ( proxy_ignore_headers ), the cache time before expiration can be set from the origin se…

问题原文

原帖链接 · dlrust · CC BY-SA 4.0

I have caching enabled on an NGINX proxy.

As I understand it, as long as I'm not ignoring the Cache-Control header in my proxy configuration (proxy_ignore_headers), the cache time before expiration can be set from the origin server using Cache-Control = max-age=XXXX.

But what would be the default cache time it would keep and use for something like Cache-Control: public? Is there a way to set a default maximum cache time for these resources?

社区高分解答

原帖链接 · maxschlepzig · CC BY-SA 4.0

That is right, by default, with just proxy_cache configured, nginx only caches responses that have max-age set in the Cache-Control header.

Without any Cache-Control header or just Cache-Control: public nginx doesn't cache the response (i.e. you get each time X-Cache-Status: MISS when you also configure add_header X-Cache-Status $upstream_cache_status;).

You can configure a default caching time for responses without a Cache-Control header or ones without a max-age field in a Cache-Control header:

    # for 200, 301, 302 responses
    proxy_cache_valid     10m;
    # for all other responses
    proxy_cache_valid any 1m;

That means a Cache-Control header has precedence over a proxy_cache_valid setting and there are no defaults for proxy_cache_valid.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。