简体   繁体   English

对PHP生成的图像使用nginx静态文件缓存

[英]Use nginx static file cache for PHP generated images

I use this code in my nginx vhost configuration to cache all images, scrips etc. on my website: 我在Nginx虚拟主机配置中使用以下代码来缓存我网站上的所有图像,脚本等:

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";
}

The problem on this code is that it is not able to cache images which are generated by PHP dynamically. 此代码上的问题是它不能缓存PHP动态生成的图像。 The base for this is that these files have not a .png or .jpg ending. 这样做的基础是这些文件没有以.png或.jpg结尾的文件。 They have only this structure: domain.com/images/xyz.php?id=123. 它们只有以下结构:domain.com/images/xyz.php?id = 123。

So my question is: How can I cache these files as well? 所以我的问题是:我如何也可以缓存这些文件? Is there any option to do that or I have to edit my PHP script? 有什么选择吗,还是我必须编辑PHP脚本?

You can either add the caching directives to the location that handles PHP or create a location for PHP files in the images folder, add your PHP handling directives to this and put this new location above the one for general PHP requests. 您可以将缓存指令添加到处理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
    [....]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM