亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 [email protected]


  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 關注
  • 117 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號