Nginx+PHP-FPMでWordPressを動かすときに必要な設定を行う。
必須の設定が1つ、やっておいた方がいい設定が1つの計2つ。
・カスタムパーマリンク用の設定(必須)
・アクセスログ用の設定(できたら)
前提として以下の2つの設定は行ってある。
CentOS6.5でNginx1.7とPHP-FPM5.5(FastCGI)を連携してPHPを動かす設定
1. default.confの全体像
今回は、Nginxのバーチャルホストの設定ファイル(default.conf)のみ編集する。
sudo vim /etc/nginx/conf.d/default.conf
変更後のdefaulst.confの全体像がこちら。
server {
listen 80 default_server;
server_name runble1.com www.runble1.com;
if ($http_host = www.runble1.com) {
rewrite (.*) http://runble1.com$1;
}
root /var/www/wp;
index index.php;
access_log /var/log/nginx/runble1_access.log main;
error_log /var/log/nginx/runble1_error.log warn;
#error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# WordPress用のリダイレクト設定
try_files $uri $uri/ /index.php?q=$uri&$args;
# WordPress用アクセスログ設定
location ~* \.(gif|jpg|png|ico|css|js)$ {
access_log off;
}
location ~ /wp-admin {
access_log off;
include php_exec;
}
location ~ /wp-content {
access_log off;
include php_exec;
}
location ~ /wp-includes {
access_log off;
include php_exec;
}
location ~ /wp-cron.php {
access_log off;
include php_exec;
}
location ~ /wp-login.php {
access_log off;
include php_exec;
}
location ~ /wp-comments {
access_log off;
include php_exec;
}
# 拡張子がphpの場合
location ~ \.php$ {
# fastcgi(php-fpmの設定)
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
2. カスタムパーマリンクの設定
WordPressでカスタムパーマリンクを使っている場合、Nginxでそれに合わせたリダイレクト処理が必須となる。
この設定を行ってないと 404 Not Foundが返ってきてしまうので注意。
そのために、try_filesディレクティブを追加する。
$uri : URLのホスト名以降の情報(パラメータ情報を除く)
$args : URLのパラメータ情報
# WordPressのパーマリンクに対応 try_files $uri $uri/ /index.php?q=$uri&$args;
try_filesは、ファイルの存在チェックを行い、存在しない場合はリダイレクト処理を行う。
最後のパラメータ「/index.php?q=$uri&$args」は、確実に存在するものにしておく。
存在チェックは、パラメータで指定された順に行っていく。
この記述では、「$uri」→「$uri/」と判定する(最後のパラメータは判定されない)
リダイレクト処理は、最初に存在したパラメータに対して行われる。
「$uri」と「$uri/」が存在しない場合、最後の「/index.php?q=$uri&$args」にリダイレクトされる。
2. アクセスログの設定
必須ではないが設定しておこう。
アクセスログを取得しないファイル用のlocationを作成し、access_logをoffにしているだけ。
php_execはphpを動かすための設定になる(後述)。
# WordPress用アクセスログ設定
location ~* \.(gif|jpg|png|ico|css|js)$ {
access_log off;
}
location ~ /wp-admin {
access_log off;
include php_exec;
}
location ~ /wp-content {
access_log off;
include php_exec;
}
location ~ /wp-includes {
access_log off;
include php_exec;
}
location ~ /wp-cron.php {
access_log off;
include php_exec;
}
location ~ /wp-login.php {
access_log off;
include php_exec;
}
location ~ /wp-comments {
access_log off;
include php_exec;
}
locationは、そのスコープ内で処理を済ませる必要が有る。
そのため、各location内にPHP-FPMの設定を行わなければならない。
ただ、それだと設定ファイルが長くなるので、その部分だけ別ファイルに移し、includeするやり方が見やすい。
sudo vim /etc/nginx/php_exec
PHP-FPMの設定を記述。
lcation ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
このphp_execファイルをincludeしている。
# PHP-FPM用 include php_exec;
リバースプロキシを設定している人は、この2つ以外もやらないといけない。
それはリバースプロキシを設定するときにやろう。
参考
Nginx + php-fpm でWordpressを動かしてみる | レンタルサーバー・自宅サーバー設定・構築のヒント

コメント
[…] Nginx+PHP-FPMでWordPressを動かすときの設定(パーマリンク・アクセスログ対策) […]