LoginSignup
7
6

More than 5 years have passed since last update.

NginxとWordpressの404ページがproxy cacheできない

Posted at

Nginxとwordpressの組み合わせで、404ページもキャッシュをかけようとしたところうまく行きませんでした。
調査したところ下記のような投稿がみつかりました。

「WordPressは404の時、ヘッダーに “Cache-Control:no-cache, must-revalidate, max-age=0″ を付けてレスポンスを返す」
参照:http://cloudrop.jp/wordpress/more_tuning_on_nginx

wp-includes/class-wp.phpのソースを改変する方法が書かれていますが、出来るだけソースは触りたくないので、フックしてこれらのheaderを削除するようにします。

functions.phpに以下のフックを追加しましょう。

function remove_nocache_on_404(){
    if(is_404()){
        header_remove("Pragma");
        header_remove("Cache-Control");
        header_remove("Expires");
    }
}
add_action('wp','remove_nocache_on_404');

こうすることでnginxでキャッシュがかかるようになりました。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6