【问题标题】:Use nginx static file cache for PHP generated images对 PHP 生成的图像使用 nginx 静态文件缓存
【发布时间】:2015-07-11 17:00:27
【问题描述】:

我在我的 nginx vhost 配置中使用此代码来缓存我网站上的所有图像、脚本等:

location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

此代码的问题是它无法缓存 PHP 动态生成的图像。这样做的基础是这些文件没有 .png 或 .jpg 结尾。它们只有这个结构:domain.com/images/xyz.php?id=123。

所以我的问题是: 我怎样才能缓存这些文件呢?是否有任何选项可以这样做,或者我必须编辑我的 PHP 脚本?

【问题讨论】:

  • 能否显示处理 PHP 请求的位置?

标签: php nginx


【解决方案1】:

您可以将缓存指令添加到处理 PHP 的位置,也可以在 images 文件夹中为 PHP 文件创建一个位置,将您的 PHP 处理指令添加到此位置,然后将此新位置置于一般 PHP 请求的上方。

location ~* images/(+.)\.php$ {
    # Your PHP handling directives
    [....]

    expires 1M;
    access_log off;
    add_header Cache-Control "public";
}
location ~* \.php$ {
    # Your PHP handling directives
    [....]
}

【讨论】:

    猜你喜欢
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2015-09-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    相关资源
    最近更新 更多