Nginx の設定ファイルの server セクションで次のように設定する。
# User dir (PHP)
location ~ ^/~([^/]+?)/(.+\.php)$ {
alias /home/$1/public_html/$2;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi_params;
}
# User dir (static)
location ~ ^/~(.+?)(/.*)?$ {
alias /home/$1/public_html$2;
index index.html index.htm;
autoindex on;
}
# For PHP
location ~ \.php$ {
root /var/www/html;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
キモは User dir (PHP) のセクションを For PHP のセクションよりも前に書くこと。そうしないとブラウザで PHP のファイルにアクセスしても Not found になってしまう。なぜなら location ~ \.php$ に先にマッチしてしまってファイルが見つからないから(たぶん)。