NGINX は広範囲の様々なアプリケーションと一緒に完璧に動作し、WordPress も確かにそのうちの一つです。NGINXの設定言語は、良く知っていると、とても強力かつ率直ですが、他のサーバから来た人はしばしばNGINX内でどう動くが分からず、単純に必要を満たすと思われるものをブログからコピーアンドペーストします。全ての人、特にNGINXをよく知らない人は、NGINX内でどのように物事が動き、されなければならないかの概要のためにnginx.org ドキュメントを確認するべきです。
Hopefully you have read the documentation above and maybe worked on setting up a virtual server or two in NGINX already - if not there are a few notes below, but you should still read the documentation.
まず、phpのために名前付きのupstreamをセットアップします。これによりバックエンドを抽出し、簡単にポートを変更あるいはもっと多くのバックエンドを追加することができます。その跡で、domain.tldのためのバーチャルホスト設定をセットアップします。
# Upstream to abstract backend connection(s) for php
upstream php {
server unix:/tmp/php-cgi.socket;
server 127.0.0.1:9000;
}
server {
## Your website name goes here.
server_name domain.tld;
## Your only path reference.
root /var/www/wordpress;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
この設定を使って、wordpressをとても簡単に提供できるはずです。一旦バックエンド(php-cgiあるいはphp-fpm)をセットアップすると、完璧に動作するはずです。
There are many ways to declare your locations in your configuration that allow you to do basically whatever you want with your URLs. 通常は、クエリ文字列やスクリプトファイルを隠す"かわいい"URLを持ちたいと思います。異なる目的に基づいた異なる2,3の戦略があります。望む結果を達成するために、上の基本的なlocationを置き換えるために使われるべきloctionを定義します。
WordPressをサブディレクトリとして提供したい場合、以下の変更(上の設定に関係)をしたいと思うでしょう。
location /wordpress {
try_files $uri $uri/ /wordpress/index.php?$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(/wordpress)(/.*)$;
}
0.8.30未満のバージョンを使っている場合、fastcgi_paramsファイルにこれを追加したいと思うでしょう。
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
WordPress マルチサイト は複数のやり方で使われることができます。もっとも多いのは"subdirectories"モードと"subdomains"モードです。
NGINX は2つの特別なディレクティブを提供します: X-Accel-Redirect <x-accel.redirect_> と map。これら2つのディレクティブを使って、Wordpressのマルチサイトネットワーク上での静的ファイルの提供のためのパフォーマンスの打撃を取り除くことができます。
map $uri $blogname{
~^(?P<blogpath>/[^/]+/)files/(.*) $blogpath ;
}
map $blogname $blogid{
default -999;
#Ref: http://wordpress.org/extend/plugins/nginx-helper/
#include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;
}
server {
server_name example.com ;
root /var/www/example.com/htdocs;
index index.php;
location ~ ^(/[^/]+/)?files/(.+) {
try_files /wp-content/blogs.dir/$blogid/files/$2 /wp-includes/ms-files.php?file=$2 ;
access_log off; log_not_found off; expires max;
}
#avoid php readfile()
location ^~ /blogs.dir {
internal;
alias /var/www/example.com/htdocs/wp-content/blogs.dir ;
access_log off; log_not_found off; expires max;
}
if (!-e $request_filename) {
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
rewrite ^(/[^/]+)?(/wp-.*) $2 last;
rewrite ^(/[^/]+)?(/.*\.php) $2 last;
}
location / {
try_files $uri $uri/ /index.php?$args ;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass php;
}
#add some rules for static content expiry-headers here
}
map $http_host $blogid {
default -999;
#Ref: http://wordpress.org/extend/plugins/nginx-helper/
#include /var/www/wordpress/wp-content/plugins/nginx-helper/map.conf ;
}
server {
server_name example.com *.example.com ;
root /var/www/example.com/htdocs;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args ;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass php;
}
#WPMU Files
location ~ ^/files/(.*)$ {
try_files /wp-content/blogs.dir/$blogid/$uri /wp-includes/ms-files.php?file=$1 ;
access_log off; log_not_found off; expires max;
}
#WPMU x-sendfile to avoid php readfile()
location ^~ /blogs.dir {
internal;
alias /var/www/example.com/htdocs/wp-content/blogs.dir;
access_log off; log_not_found off; expires max;
}
#add some rules for static content expiry-headers here
}
注意