nginxをWordPress用に設定する
当ブログ「Hinemosu」で使用しているnginx設定ファイルを公開します。
サーバは「さくらのVPS 2G」を利用し、構成は"nginx with FastCGI on Ubuntu"となっています。
まだnginxの勉強中なので、下記設定が最適というわけではありません。識者からのご意見をお待ちしています。
nginx.confの設定
現在のnginx.confを下記に示します。
$ cat /etc/nginx/nginx.conf
user www-data;
worker_processes 3;
worker_cpu_affinity 001 010 100;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
# tcp_nopush on;
# tcp_nodelay on;
keepalive_timeout 35;
# types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "MSIE [1-6]\.";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
client_max_body_size 32m;
}
worker_processes, worker_cpu_affinity は、サーバ構成に併せて調整が必要です。
worker_connections, keepalive_timeout, client_max_body_sizeは、ホスティングされるサイトの環境を考慮した調整が必要です。
tcp_nopush, tcp_nodelay, types_hash_max_size は、意味が良く分からないのでコメントアウトしました。
出力ログ形式をApache2互換にしました。
サイト設定ファイル
現状のサイト設定ファイルは以下の通りです。
WordPress用キャッシュプラグイン「WordPress Super Cache」用に最適化してあります。
$ cat /etc/nginx/sites-available/default
server {
listen 80; ## listen for ipv4; this line is default and implied
root /home/USER_NAME/www;
index index.php index.html index.htm;
server_name www.hide10.com;
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
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# WP Super Cache rules.
gzip_static on;
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 "";
}
# 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 "";
}
# Stop processing if the supercache file is valid.
if ($supercacheuri) {
rewrite ^ $supercacheuri break;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param PHP_VALUE "upload_max_filesize = 50M";
fastcgi_param PHP_VALUE "post_max_size = 51M";
include fastcgi_params;
}
location ^~ /munin/ {
auth_basic "basic authentication";
auth_basic_user_file /etc/nginx/.htpasswd;
}
location ^~ /visitors/ {
auth_basic "basic authentication";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
関連記事も参照してください。
☆ さくらのVPS 2G、”WordPress on nginx with FastCGI”でApache Bench – Hinemosu
☆ 「さくらのVPS 2G」+ Ubuntuで”WordPress on nginx with FastCGI”環境を構築する – Hinemosu
以下のサイトを参考にしました。
☆ WordPress [Nginx wiki]
☆ FullExample [Nginx wiki]
☆ nginx設定 | 楽しく情報処理技術者試験
☆ Nginx – WordPress Codex 日本語版





ディスカッション
コメント一覧
まだ、コメントがありません