ApacheからNginxへの移行
Nginxを業務(ソーシャルゲームなど)では使っていたのですが、このたび、自宅サーバーにもNginxを導入しました。NginxとPHP-FPMでWordPressを動かしています。OSはopenSUSE Linuxなので、NginxとPHP-FPMはYaSTで入れました。
NginxでWordPressを動かし、Apacheのmod_rewriteを代替させ、WP Super Cacheプラグインを動かすために、nginx.confに以下を記述しました(参考:Nginxを使ったもう一歩進んだWordPressチューニング)。
nginx.confの中身
http {
include mime.types;
default_type application/octet-stream;
proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache-space:4m max_size=50m inactive=120m;
proxy_temp_path /usr/local/nginx/tmp;
fastcgi_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=wpcache:10m max_size=50M inactive=30m;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 0;
gzip_buffers 4 8k;
gzip_types text/plain text/xml application/x-javascript text/css;
gzip_disable "msie6";
gzip_vary on;
# HttpStaticGzipModuleをオンに
gzip_static on;
include conf.d/*.conf;
server {
listen 80;
server_name localhost;
location / {
root /srv/www/html/;
index index.php index.html index.htm;
proxy_cache cache-space;
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 20m;
if ( -f $request_filename ) {
break;
}
if (!-e $request_filename) {
rewrite ^/(.*)$ /index.php?q=$1 last;
}
set $nocache "";
set $supercache_file $document_root/wp-content/cache/supercache/${http_host}${uri}/index.html;
set $supercache_uri "";
if (-f $supercache_file) {
set $supercache_uri /wp-content/cache/supercache/${http_host}${uri}/index.html;
}
if ($request_method = "POST") {
set $supercache_uri "";
set $nocache "1";
}
if ($query_string ~ .*=.*) {
set $supercache_uri "";
}
if ($http_cookie ~ ^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$) {
set $supercache_uri "";
set $nocache "1";
}
if ($http_x_wap_profile ~ ^[a-z0-9\"]+) {
set $supercache_uri "";
set $nocache "1";
}
if ($http_profile ~ ^[a-z0-9\"]+) {
set $supercache_uri "";
set $nocache "1";
}
if ($http_user_agent ~ ^.*(2.0\ MMP|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|iPad|iPod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800).*) {
set $supercache_uri "";
set $nocache "1";
}
if ($http_user_agent ~ ^(w3c\ |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|htc_|inno|ipaq|ipod|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|lg/u|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|palm|pana|pant|phil|play|port|prox|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda\ |xda-).*) {
set $supercache_uri "";
set $nocache "1";
}
if ($http_user_agent ~ ^(DoCoMo/|J-PHONE/|J-EMULATOR/|Vodafone/|MOT(EMULATOR)?-|SoftBank/|[VS]emulator/|KDDI-|UP\.Browser/|emobile/|Huawei/|IAC/|Nokia|mixi-mobile-converter/)) {
set $supercache_uri "";
set $nocache "1";
}
if ($http_user_agent ~ (DDIPOCKET\;|WILLCOM\;|Opera\ Mini|Opera\ Mobi|PalmOS|Windows\ CE\;|PDA\;\ SL-|PlayStation\ Portable\;|SONY/COM|Nitro|Nintendo)) {
set $supercache_uri "";
set $nocache "1";
}
if ($supercache_uri) {
rewrite ^ $supercache_uri last;
break;
}
}
Apacheよりも体感速度が向上した感じがします。しばらく計測して使ってみます。