为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Wordpress 中创建动态页面而不遇到缓存问题?

如何在 Wordpress 中创建动态页面而不遇到缓存问题?

PHP
一只名叫tom的猫 2023-03-26 13:57:32
我正在运行一个带有 Wordpress 博客的 RoR 网站,我刚刚在 Wordpress 中使用用户登录时由主 (RoR) 站点设置的 cookie 实现了一个登录/注销标头。一切正常,除了当用户登录或注销(从 RoR 站点)时,我需要在 wordpress 站点上进行硬刷新以查看修改后的标头。我需要解决这个问题。我的问题是 -这是我的缓存设置的问题,还是我应该以不同的方式实施解决方案?我的解决方案我的 RoR 网站会在用户登录时创建一个名为“登录”的 cookie,并在用户注销时删除该 cookie。我编辑了我的子主题header.php以插入此代码:<?php if(isset($_COOKIE['login'])) : ?>  <!-- logged in header --><?php else : ?>  <!-- not logged in header --><?php endif; ?>缓存我使用了很多缓存/优化插件/服务/设置,包括:云焰WP超级缓存自动优化Apache 配置来设置 Cache-Control 和 Expires 标题首先,我禁用了 WP Super Cache,因为它的主要功能似乎是缓存 HTML 和 PHP,并且在插件处于活动状态的情况下,我需要在标头通过硬刷新更新之前删除缓存。然后我取消选中 Autoptimze 设置以禁用 HTML 缓存。然后我检查了我的 Cloudflare 设置——我正在使用标准缓存,使用现有的标头,并且没有进行任何缩小。最后我的 Apache 配置似乎是正确的:  <IfModule mod_headers.c> ...     <FilesMatch "\.(html|htm|php|pdf)$">       Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"     </FilesMatch>  </IfModule>作为附加测试,我直接(通过 IP 地址)访问了该网站,这似乎工作正常。我还在 Chrome 开发人员工具的网络选项卡上勾选了“禁用缓存”,并运行了一些测试,效果也很好。因此,我认为现在问题出在 Chrome 上。当我查看 HTTP 请求标头时,在注销或登录后返回到 Wordpress 站点后,我看到了:Status Code: 200  (from disk cache)当我单击浏览器刷新按钮时,页面刷新并且标题正确。以下是显示错误标头时的 HTTP 响应标头:cache-control: private, must-revalidatecf-cache-status: DYNAMICcf-ray: 593e0b2e0cc706c5-LHRcf-request-id: 02baa550c6000006c5e7912200000001content-encoding: brcontent-type: text/html; charset=UTF-8date: Fri, 15 May 2020 15:55:31 GMTexpect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"expires: Fri, 15 May 2020 16:05:30 GMTlink: <https://www.example.com/blog/wp-json/>; rel="https://api.w.org/"server: cloudflarestatus: 200vary: Accept-Encoding,User-Agent
查看完整描述

1 回答

?
慕哥9229398

TA贡献1877条经验 获得超6个赞

我终于能够wget像这样在服务器上使用来调试这个问题:


$ wget https://localhost/blog/ --no-check-certificate --server-response

一旦我禁用了我在 Wordpress 中用于缓存的两个插件,这个命令就允许我绕过 Cloudflare 并查看 Apache 设置的标头。


--2020-05-19 13:21:08--  https://localhost/blog/

Resolving localhost (localhost)... 127.0.0.1

Connecting to localhost (localhost)|127.0.0.1|:443... connected.

WARNING: cannot verify localhost's certificate, issued by ‘ST=California,L=San Francisco,OU=CloudFlare Origin SSL Certificate Authority,O=CloudFlare\\, Inc.,C=US’:

  Unable to locally verify the issuer's authority.

WARNING: no certificate subject alternative name matches

    requested host name ‘localhost’.

HTTP request sent, awaiting response... 

  HTTP/1.1 200 OK

  Date: Tue, 19 May 2020 12:21:08 GMT

  Server: Apache

  Link: <https://localhost/blog/wp-json/>; rel="https://api.w.org/"

  Cache-Control: private, must-revalidate

  Expires: Tue, 19 May 2020 12:31:08 GMT

  Vary: Accept-Encoding,User-Agent

  Content-Type: text/html; charset=UTF-8

  Keep-Alive: timeout=5, max=100

  Connection: Keep-Alive

  Transfer-Encoding: chunked

Length: unspecified [text/html]

Saving to: ‘index.html’

我注意到 Cache-Control 标头与我的 Apache 配置中的标头不同。


Cache-Control: max-age=0, private, no-store, no-cache, must-revalidate

这是因为 Cache-Control 标头是在根域 Apache 配置中设置的,而不是为博客设置的(它由反向代理托管)。


解决方案是将所有 Expires 和 Cache-Control 标头配置复制到我的博客 Apache 配置文件中,然后瞧瞧:


$ wget https://localhost/blog/ --no-check-certificate --server-response--2020-05-19 16:41:19--  https://localhost/blog/

Resolving localhost (localhost)... 127.0.0.1

Connecting to localhost (localhost)|127.0.0.1|:443... connected.

WARNING: cannot verify localhost's certificate, issued by ‘ST=California,L=San Francisco,OU=CloudFlare Origin SSL Certificate Authority,O=CloudFlare\\, Inc.,C=US’:

  Unable to locally verify the issuer's authority.

WARNING: no certificate subject alternative name matches

    requested host name ‘localhost’.

HTTP request sent, awaiting response... 

  HTTP/1.1 200 OK

  Date: Tue, 19 May 2020 15:41:20 GMT

  Server: Apache

  Vary: Accept-Encoding,Cookie,User-Agent

  Link: <https://localhost/blog/wp-json/>; rel="https://api.w.org/"

  Cache-Control: private, no-store, no-cache, must-revalidate

  Expires: Tue, 19 May 2020 15:41:20 GMT

  Content-Type: text/html; charset=UTF-8

  Keep-Alive: timeout=5, max=100

  Connection: Keep-Alive

  Transfer-Encoding: chunked

Length: unspecified [text/html]

Saving to: ‘index.html’

为了完整起见,请参阅我的博客的新 Apache 配置:


# avoids sending hackers too much info about the server

ServerTokens Prod


<VirtualHost *:8080>

  ServerName www.example.com

  ServerAdmin dagmar@example.com


  ErrorLog /var/log/apache2/blog/error.log

  CustomLog /var/log/apache2/blog/access.log common


  DocumentRoot /var/www/blog

  <Directory /var/www/blog>

    AllowOverride All

    Options -Indexes

  </Directory>


  # Enable Compression

  <IfModule mod_deflate.c>

    SetOutputFilter DEFLATE

    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

    SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary

    Header append Vary User-Agent

  </IfModule>


  # Enable expires headers

  <IfModule mod_expires.c>

    ExpiresActive On

    ExpiresByType image/jpg                     "access plus 1 year"

    ExpiresByType image/jpeg                    "access plus 1 year"

    ExpiresByType image/gif                     "access plus 1 year"

    ExpiresByType image/png                     "access plus 1 year"

    ExpiresByType text/css                      "access plus 1 month"

    ExpiresByType application/pdf               "access plus 1 month"

    ExpiresByType text/x-javascript             "access plus 1 month"

    ExpiresByType text/javascript               "access plus 1 month"

    ExpiresByType application/javascript        "access plus 1 month"

    ExpiresByType application/x-javascript      "access plus 1 month"

    ExpiresByType image/x-icon                  "access plus 1 year"

    ExpiresByType text/xml                      "access plus 0 seconds"

    ExpiresByType text/html                     "access plus 0 seconds"

    ExpiresByType text/plain                    "access plus 0 seconds"

    ExpiresByType application/xml               "access plus 0 seconds"

    ExpiresByType application/json              "access plus 0 seconds"

    ExpiresByType application/rss+xml           "access plus 1 hour"

    ExpiresByType application/atom+xml          "access plus 1 hour"

    ExpiresByType text/x-component              "access plus 1 hour"

    ExpiresDefault                              "access plus 0 seconds"

  </IfModule>


  # Enable caching headers

  <IfModule mod_headers.c>

     # Calculate etag on modified time and file size (could be turned off too ?)

     FileETag MTime Size

     # NEVER CACHE - notice the extra directives

     <FilesMatch "\.(html|htm|php)$">

       Header set Cache-Control "private, no-store, no-cache, must-revalidate"

     </FilesMatch>

  </IfModule>



查看完整回答
反对 回复 2023-03-26
  • 1 回答
  • 0 关注
  • 87 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信