WP Super CacheとWPtouch iPhone Themeを使うためのNginxの設定
ruin » Nginxサーバで、WordPressのプラグインWP Super Cacheを使ってみたという記事を参照すればWP Super Cacheを利用することはできますが、これだけではiPhoneなどからアクセスした場合にもキャッシュが適用され、PC向けのページが表示されてしまいます。
そこで、Nginxの設定を修正してUserAgentがモバイル端末のものであった場合にはキャッシュへ飛ばさないようにNginxの設定を編集してみました。
WP Super Cache側の設定
WP Super Cacheの設定画面でMobile device support using WordPress Mobile Edition.の項目にチェックを入れておきます。
Nginx側の設定
WP Super Cache向けの設定だけを別のファイルに切り出しておくことで、再利用が容易になります。
gzip_static on;gzip_http_version 1.1;gzip_proxied expired no-cache no-store private auth;gzip_disable "MSIE [1-6]\.";gzip_vary on;if (-f $request_filename) { break;}set $supercache_file '';set $supercache_uri $request_uri;# POSTの場合キャッシュを無効化if ($request_method = POST) { set $supercache_uri '';}# rewriteを利用していない場合キャッシュを無効化if ($query_string) { set $supercache_uri '';}# ログインしている場合キャッシュを無効化if ($http_cookie ~* "comment_author_|wordpress|wp-postpass_") { set $supercache_uri '';}# モバイル端末の場合キャッシュを無効化if ($http_user_agent ~* "2.0\ 2MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|PlayStation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800") { set $supercache_uri '';}# キャッシュファイルのパスを指定if ($supercache_uri ~ ^(.+)$) { set $supercache_file /wp-content/cache/supercache/$http_host$1/index.html;}# キャッシュが存在する場合はrewriteif (-f $document_root$supercache_file) { rewrite ^(.*)$ $supercache_file break;}# キャッシュが存在しない場合はWordPressに処理を渡すif (!-e $request_filename) { rewrite . /index.php last;}
設定ファイルを利用する側は、includeディレクティブで設定を読み込むだけで済みます。
server { listen 80; server_name blog.monoweb.info; location / { root /home/mono/public_html/blog.monoweb.info; index index.php index.html; # 用意しておいた設定ファイルを利用 include /etc/nginx/wordpress_super_cache_params; } location ~ \.php$ { gzip off; include /etc/nginx/fastcgi_params; fastcgi_pass localhost:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/mono/public_html/blog.monoweb.info$fastcgi_script_name; }}