【问题标题】:Unable to disable PHP cache despite thorough settings and tuning尽管进行了彻底的设置和调整,但仍无法禁用 PHP 缓存
【发布时间】:2023-11-15 09:36:01
【问题描述】:

周日快乐,

我在 Ubuntu 16.x 上运行在 Azure 上的 Bitnami 灯封装实例的缓存管理问题上苦苦挣扎了 3 天(担心到月底几乎会变得大胆)。

我搞了一个大系统的PHP5到PHP7的迁移几周了,即将完成任务。

我使用系统的测试站点可以在这里访问:https://stephanedeluca.com供你查看缓存。

不幸的是,我看到的是,我的 PHP 脚本并没有立即反映我在部署(简单上传)到服务器后所做的更改。因此,UX 有时会因为用户经常需要双重刷新而破坏,在最坏的情况下,必须通过浏览器重新加载所有内容。

在以前的系统上,一切都很好,但在这个新盒子上,我遇到了问题。该框使用php-fpm。

到目前为止,我在缓存管理解决方案方面取得的成就:

  • 在 php.ini 中,我禁用了 OPCache; — 来自.htaccess,我使用了 mod_expires(我还安装了 mod_headers 和 mod_expires) 如下:

    过期活动在 ExpiresDefault "访问加 0 秒" ExpiresDefault "访问加 1 年" #Header 附加缓存控制“公共” 文件匹配> ExpiresDefault "访问加 1 周" #Header 附加缓存控制“公共” 文件匹配> ExpiresDefault "访问加 2 小时" #Header 附加缓存控制“代理重新验证” 文件匹配> FileETag 无 标头未设置 ETag #Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate" 标头集 Pragma "no-cache" #标头集过期“1966 年 3 月 15 日星期二 10:00:00 GMT+1”
  • 我所有的 PHP 脚本(通过 prepend)生成以下标头(在 session_start() 被调用之后):

    // === No caching ===
    session_cache_limiter('private');
    session_cache_expire(0);
    
    // === Start session (must be after session_cache_X())
    session_start();
    
    header('Cache-Control: private, max-age=0, s-max-age=0, no-cache, no-store, must-revalidate', true);
    header("Last-Modified: $headerNow", true);
    

查看浏览器请求和响应时,我发现一切看起来都不错,恕我直言:

请求(由 Chrome 报告)

Request URL: https://stephanedeluca.com/
Request Method: GET
Status Code: 200 OK (from ServiceWorker)
Referrer Policy: no-referrer-when-downgrade

回复:

Cache-Control: private, max-age=0, s-max-age=0, no-cache, no-store, must-revalidate
Connection: Keep-Alive
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Date: Sun, 28 Apr 2019 12:36:57 GMT
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Keep-Alive: timeout=5, max=100
Last-Modified: Sun, 28 Apr 2019 12:36:57 GMT
Pragma: no-cache
Server: Apache
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Frame-Options: SAMEORIGIN
X-Generated-On: Sun, 28 Apr 2019 12:36:57 GMT
X-Powered-By: ZID/Webengine v24.0b27 -- Copyright (c) 1995-2019 MagicApps (http://MgcApps.com) -- All Rights Reserved
Provisional headers are shown
Referer: https://stephanedeluca.com/map.php
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36

我还使用https://www.giftofspeed.com/cache-checker/检查了缓存,报告符合预期。

我的想法不多了。

【问题讨论】:

    标签: php browser-cache php-7 bitnami


    【解决方案1】:

    Bitnami 工程师在这里。为了禁用服务器中的缓存并让文件每次都被提供,请禁用 Apache 的 PageSpeed 和 PHP 的 OPCache,默认启用。

    要禁用 PageSpeed,请在您的 httpd.conf (/opt/bitnami/apache2/conf/httpd.conf) 中注释掉以下行

    #Include conf/pagespeed.conf
    #Include conf/pagespeed_libraries.conf
    

    要禁用 OPCache,请更改 php.ini 文件中的 opcache.enable 并将其设置为 0 (/opt/bitnami/php/etc/php.ini)

    之后重启所有服务

    sudo /opt/bitnami/ctlscript.sh restart
    

    【讨论】:

    • 其他浏览器也会发生同样的事情吗?可能是浏览器缓存的原因。我不确定还有什么可能