nginx, fastcgi_cache, php5-fpm, wordpress multisite(subdomain), super cache の設定
構成としては、 nginx が 80 番で受けて、php5-fpm を socket で動かす。
そして、php5-fpm 上で wordpress をサブドメインでサイトを分けるネットワーク利用にし、さらに wp_super_cache を使用した際の設定。
間違っているかもね。まぁ、適宜間違っていたら修正するかな。
Nginxを使ったもう一歩進んだWordPressチューニングを参考にしました。まぁ、そこの設定ファイルは、マルチサイト用じゃないけど。
/etc/nginx/nginx.conf
user nginx;
worker_processes 2;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 4 2k;
request_pool_size 4k;
if_modified_since before;
ignore_invalid_headers on;
server_tokens off;
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;
gzip_static on;
output_buffers 1 32k;
postpone_output 1460;
fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=wpcache:10m max_size=50M inactive=30m;
server {
listen 80;
server_name localhost;
charset utf-8;
location / {
return 403;
}
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
include /etc/nginx/sites-enabled/*;
}
ポイントはfastcgi_cache_pathを指定しているところかな。
次なんだけど、上のサイトと wordpress の codex のサイトを参考にするのがいいかな。 Nginx « WordPress Codex Codex の方が整理しているんだけど、私はベタっとはりつけておきますか。
/etc/nginx/sites-enables/example.conf
server unix:/var/run/php5-fpm.sock;
}
server {
listen 80;
server_name example.com;
server_name *.example.com;
#on server block
##necessary if using a multi-site plugin
server_name_in_redirect off;
##necessary if running Nginx behind a reverse-proxy
port_in_redirect off;
root /home/app/prod/example/wordpress;
index index.html index.htm index.php;
# Global restrictions configuration file.
# Designed to be included in any server {} block.
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Deny access to any files with a .php extension in the uploads directory
location ~* ^/wp-content/uploads/.*.php$ {
deny all;
access_log off;
log_not_found off;
}
# Deny access to any files with a .php extension in the uploads directory for multisite
location ~* /files/(.*).php$ {
deny all;
access_log off;
log_not_found off;
}
#on server block
##necessary if using a multi-site plugin
server_name_in_redirect off;
##necessary if running Nginx behind a reverse-proxy
port_in_redirect off;
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
access_log /home/shin/site/example/log/access.log;
error_log /home/shin/site/example/log/error.log;
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
# Pass uploaded files to wp-includes/ms-files.php.
rewrite /files/$ /index.php last;
# For multisite: Use a caching plugin that creates symlinks to the correct subdirectory structure to get som
e performance gains.
set $cachetest "$document_root/wp-content/ms-filemap/${host}${uri}";
if ($uri ~ /$) {
set $cachetest "";
}
if (-f $cachetest) {
# Rewrites the URI and stops rewrite processing so it doesn't start over and attempt to pass it to the next
rule.
rewrite ^ /wp-content/ms-filemap/${host}${uri} break;
}
if ($uri !~ wp-content/plugins) {
rewrite /files/(.+)$ /wp-includes/ms-files.php?file=$1 last;
}
# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-ms-subdir-wp-super-cache.conf;
#include global/wordpress-ms-subdir-w3-total-cache.conf;
# WP Super Cache rules.
# Designed to be included from a 'wordpress-ms-...' configuration file.
# Enable detection of the .gz extension for statically compressed content.
# Comment out this line if static gzip support is not compiled into nginx.
gzip_static on;
set $nocache "";
set $supercacheuri "";
set $supercachefile "$document_root/wp-content/cache/supercache/${http_host}${uri}index.html";
if (-e $supercachefile) {
set $supercacheuri "/wp-content/cache/supercache/${http_host}${uri}index.html";
}
# If this is a POST request, pass the request onto WordPress.
if ($request_method = POST) {
set $supercacheuri "";
set $nocache "1";
}
# If there is a query string, serve the uncached version.
if ($query_string) {
set $supercacheuri "";
}
# Logged in users and those who have posted a comment get the non-cached version.
if ($http_cookie ~* comment_author_|wordpress_logged_in|wp-postpass_) {
set $supercacheuri "";
set $nocache "1";
}
# Mobile browsers get the non-cached version.
# Wastes CPU cycles if there isn't a mobile browser WP theme for the site.
if ($http_x_wap_profile) {
set $supercacheuri "";
set $nocache "1";
}
if ($http_profile) {
set $supercacheuri "";
set $nocache "1";
}
if ($http_user_agent ~* (2.0\ MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|Eu
doraWeb|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|SonyE
ricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|i
Pod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800)) {
set $supercacheuri "";
set $nocache "1";
}
if ($http_user_agent ~* (w3c\ |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|do
co|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|w
inw|xda\ |xda-)) {
set $supercacheuri "";
set $nocache "1";
}
# Stop processing if the supercache file is valid.
if ($supercacheuri) {
rewrite ^ $supercacheuri break;
}
# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
rewrite ^/[_0-9a-zA-Z-]+(/wp-.*) $1 last;
rewrite ^/[_0-9a-zA-Z-]+.*(/wp-admin/.*\.php)$ $1 last;
rewrite ^/[_0-9a-zA-Z-]+(/.*\.php)$ $1 last;
}
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
# Zero-day exploit defense.
# http://forum.nginx.org/read.php?2,88845,page=3
# Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with
php-fpm/php-fcgi.
# Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine. And then cross your
fingers that you won't get hacked.
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# fastcgi_intercept_errors on;
fastcgi_pass phpfpm;
fastcgi_cache wpcache;
fastcgi_cache_key "$scheme://$host$request_uri";
fastcgi_cache_valid 200 10m;
fastcgi_cache_valid 404 1m;
fastcgi_cache_bypass $nocache;
}
}
ちょっとベタッと張りすぎかな。codex と fastcgi_cache の重ねワザですな。supercache を使わないところは、fastcgi_cache も使わないようにするとかそういったところかな、と。
とは言ってもこの記事を書いているサイトは激重で apache で super cache も使わずに動かしてます。。。自分のことは面倒になっちゃうんだよなー。機会を見て、なんとかしなきゃ。aws のマイクロでいいんじゃないか、と思っているし。というわけで、サーバを変えて、ちゃんと設定しなおしました。
適当に突っ込んでいただければ、適宜修正します。



Shin Ohno 2003-2012