Nginx(kusanagi)にWordPressでサブディレクトリ運用サイトを追加する際のパーマリンク設定

表題の件、単純にディレクトリを作って、そこにデータを移設、DBを移設してwp-config.phpを用意するだけではダメだった。

どうダメだったかと言うと、トップページはアクセスでき、ログインもできるが、パーマリンクが機能しなかった。

上記の事から、WordPress上の問題ではなく、Nginxのパーマリンク解釈に問題があることがわかった。WordPressのパーマリンクは、Apacheでは.htaccessがmod_rewriteで処理していたが、Nginxでは.htacessが機能しないので、Nginxのconfにちゃんと書いておく必要がある。
URLとサイトの関係

用途 URL ディレクトリ
メインサイト https://dacelo.space/ /home/kusanagi/dacelo/DocumentRoot/
サブディレクトリサイト https://dacelo.space/hoge/ /home/kusanagi/dacelo/DocumentRoot/hoge/

まずは、ディレクトリサイト用のConfを別立てで作っておく。

$sudo vi /etc/nginx/conf.d/sub/hoge.conf
location /hoge {
    root /home/kusanagi/dacelo/DocumentRoot/hoge;
 
    access_log /home/kusanagi/dacelo/log/nginx/hoge_access.log  main;
    error_log /home/kusanagi/dacelo/log/nginx/hoge_error.log;
 
    # wordpress パーマリンク設定
    try_files $uri $uri/ /hoge/index.php?q=$uri&$args;
 
    index index.php;
    charset utf-8;
}

広告

次に、メインのサイトのConfからincludeする設定を記載する。

$sudo vi /etc/nginx/conf.d/dacelo_ssl.conf
server {
    (略)
    include conf.d/sub/hoge.conf
}

あとはNginxを再起動すればOk。

$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo systemctl restart nginx

About: dacelo


Leave a Reply

Your email address will not be published. Required fields are marked *