【问题标题】:Nginx caching headers by file typeNginx 按文件类型缓存标头
【发布时间】:2014-04-18 06:04:32
【问题描述】:

我想为 nginx 传递的文件设置缓存头。有没有办法根据它们的类型为某些文件设置缓存头?例如。我想为所有 js 文件而不是其他文件设置缓存头。我不想根据文件所在的位置设置标题。

【问题讨论】:

  • 您可以使用Headers More module。它可以根据响应内容类型设置标头。
  • 如何设置每个文件类型的标题?我没有可缓存文件的特定位置,也没有我不想添加缓存标头的位置。
  • more_set_headers -t application/x-javascript 'Cache-Control: max-age=31536000'
  • 请写下这个作为答案。那我就可以给你一个'up':)

标签: caching nginx http-headers


【解决方案1】:

我知道这是旧的,但 map 和 expires 会自动使用 Cache-Control Header 执行此操作,无需任何额外模块,您仍然可以通过 add_header 添加公共或私有

map $sent_http_content_type $expires {
   
    application/pdf               42d;
    application/zip               42d;
    application/x-7z-compressed   42d;
    application/x-rar-compressed  42d;

    application/octet-stream  1M;

    application/javascript    3M;
    text/css                  3M;
    ~image/                   3M;
    ~video/                   3M;
    ~audio/                   3M;
    ~text/                    1d; # html/php

    default                  off;
}

expires $expires;

【讨论】:

    【解决方案2】:

    您可以使用Headers More 模块。它具有根据响应内容类型设置标头的选项。

    more_set_headers -t application/x-javascript 'Cache-Control: max-age=31536000'
    

    【讨论】:

      猜你喜欢
      • 2011-09-06
      • 1970-01-01
      • 2017-01-10
      • 2011-11-16
      • 2016-10-30
      • 1970-01-01
      • 1970-01-01
      • 2013-11-07
      • 2017-03-23
      相关资源
      最近更新 更多