快適なnginxでWordPress、サブディレクトリで動かすサンプル提供
2017/11/13更新
nginxでWordPressをサブディレクトリで動作させる設定に6時間もかかってしまいました。
nginx wordpress サブディレクトリ
と検索をかけると、海外・日本語共に結構な情報が得られます。
ですが、、、
全てを試しましたが、そのままでは動きませんでした。
(↑もうh1タグだろうと階層なんてどうでもいいと思える境地)
単独ページと、サブディレクトリのWordPressが正常に動作しているように見えても、WordPressでトップページ以外は親ディレクトリに飛ばされたり、サイトから管理ページへリンクで戻ろうとすると403になったりと苦戦しました。
私の環境、そんなに特殊では無いと思うんですけどね。
- nginx 1.12.2 + fastcgi
- php 7.1
- WordPress 4.8.3
- oyaji.ii-sys.jp でphp単体のページ、oyaji.ii-sys.jp/blogでWordPressを動かす。
以下がとりあえず動いた設定ファイル(抜粋)です。
このページ内の目次
virtual.conf
server {
listen 80;
listen [::]:80;
server_name HOST.DOMAINNAME;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
root /home/USER/DOCUMENTROOT;
server_name HOST.DOMAINNAME;
# ~ SSL関連の記述省略 ~
#
# ~ ログ出力の記述省略 ~
#
# ~ セキュリティ設定省略 ~
index index.php index.html index.htm;
set $is_mobile '';
if ($http_user_agent ~* '(iPhone|iPod|Android.*Mobile|Windows.*Phone|dream|CUPCAKE|blackberry9500|blackberry9530|blackberry9520|blackberry9550|blackberry9800|webOS|incognito|webmate|Googlebot-Mobile)') {
set $is_mobile 'mobile.';
}
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.(jpe?g|png|gif|ico|css|js)$ {
access_log off;
expires 6M;
}
location ~ \.php {
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_no_cache $do_not_cache;
fastcgi_cache_bypass $do_not_cache;
fastcgi_cache $keys_zone;
fastcgi_cache_key $is_mobile$scheme://$host$request_uri;
fastcgi_cache_valid 200 2h;
fastcgi_cache_valid 301 302 1h;
fastcgi_cache_valid 404 1m;
fastcgi_cache_valid 500 1s;
fastcgi_cache_valid any 5m;
}
location ^~ /blog {
error_page 404 /index.php?error=404;
if ($request_method != GET) {
set $do_not_cache 1;
}
if ($request_uri ~* '/(wp-admin/|wp-login.php|wp-cron.php|xmlrpc.php|\??feed|sitemap.xml)') {
set $do_not_cache 1;
}
location ~ \.(txt|dat)$ {
return 404;
}
location ~ /wp-config.php$ {
return 404;
}
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_no_cache $do_not_cache;
fastcgi_cache_bypass $do_not_cache;
fastcgi_cache $keys_zone;
fastcgi_cache_key $is_mobile$scheme://$host$request_uri;
fastcgi_cache_valid 200 2h;
fastcgi_cache_valid 301 302 1h;
fastcgi_cache_valid 404 1m;
fastcgi_cache_valid 500 1s;
fastcgi_cache_valid any 5m;
}
}
}
locationディレクティブにindex入っていたり、php部分が冗長な気がしますが、今のところこれで精一杯です。
2017/11/13に修正加えました。
Cookie関係を入れると404吐くので外してますが、調べてまた更新します。
ご指摘あればぜひコメント欄にお願いします。
locationディレクティブのプレフィックスについておかげで理解が深まりました。
nginx非公式のようですが、日本語訳サイト助かりました。


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