網元では Nginx のリバースプロキシキャッシュを使用して、サイトの高速化を実現しています。
そのため、デフォルト状態で使用している場合は、クライアントの User Agent に関係なく同じキャッシュを使用して表示します。
このまま使用すると、
モバイル用に違うキーでキャッシュをさせる方法
網元で自動設定される nginx の設定ファイルを修正してください。
/etc/nginx/conf.d/default.conf
または、/etc/nginx/conf.d/example.com.conf
( example.com は、バーチャルドメイン名です ) を以下のように修正します。
server {
listen 80 default;
server_name _;
root /var/www/vhosts/$host;
index index.html index.htm;
charset utf-8;
access_log /var/log/nginx/$host.access.log main;
error_log /var/log/nginx/$host.error.log;
include /etc/nginx/drop;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
#rewrite ^(.*)(index|home|default)\.html? $1 permanent;
set $mobile '';
include /etc/nginx/mobile-detect; # 行頭の "#" を取り除いてください
:
#include /etc/nginx/mobile-detect;
の行頭の # を取ってください。
その後、設定を反映させるために # service nginx restart
で Nginx を再起動してください。
Nginx Cache Controller でモバイル用のキャッシュも削除する
テーマの functions.php 等に以下のコードを追加してください。
<?php
add_filter('nginxchampuru_get_cache', 'nginxchampuru_get_cache', 10, 2);
function nginxchampuru_get_cache($key, $url = null) {
global $nginxchampuru;
if (!$url) {
$url = $nginxchampuru->get_the_url();
}
$keys = array(
$key,
$nginxchampuru->get_cache_key($url.'@ktai'),
$nginxchampuru->get_cache_key($url.'@smartphone'),
);
if ($key !== $nginxchampuru->get_cache_key($url)) {
$keys[] = $nginxchampuru->get_cache_key($url);
}
return $nginxchampuru->get_cache_file($keys);
}
これにより Nginx Cache Controller でキャッシュファイルを削除する時にスマートフォン用のキャッシュファイルも同時に削除されるようになります。