LoginSignup
71
72

More than 5 years have passed since last update.

nginxでwordpressのパーマリンクをデフォルト以外に対応する

Last updated at Posted at 2015-04-24

パーマリンク対応

wordpressのパーマリンクを変更すると、RestfulAPIみたいに、ディレクトリ階層のURLに変更することができます。

Screen Shot 2015-04-25 at 02.53.34.png

ngix + php−fpmでは、nginx側でURLを処理して適切にphp-fpmへ渡してあげないといけません。(JavaEEのようにコンテキストを持たないので。。。)

nginx本家のドキュメントに説明があったので、それを元に設定してみます。

nginx.conf
中略…

server {

中略…
# URL デフォルトのリンクから変更した場合、URLでトライて、見つからないソースは、@wordpressロケーションとして飛ばします。
location / {
    try_files $uri $uri/ @wordpress;
}
# index.php など、通常の.phpでのアクセス。見つからなければ@wordpressロケーションとして飛ばします。
location ~ \.php$ {
    try_files $uri @wordpress;
    fastcgi_pass …;
    fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name;
    その他fastcgiの設定…
}

# 階層型URLのパーマリンクでアクセスされた場合、(.phpで実行されなかった場合)wordpressのトップページのindex.phpへ投げてしまいます。
location @wordpress {
    fastcgi_pass …;

    fastcgi_param SCRIPT_FILENAME /path/to/index.php;

    その他fastcgiの設定…
}

実装例

nginx.conf

中略

server {
    listen       80;
    server_name  yourhost.com;
    root         /usr/share/nginx/wordpress;
    index        index.php

    charset utf-8;

    location / {
        try_files $uri $uri/ @wordpress;
    }

    location ~ \.php$ {
        try_files $uri @wordpress;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME  /usr/share/nginx/wordpress$fastcgi_script_name;
        include       fastcgi_params;
    }

    location @wordpress {
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME  /usr/share/nginx/wordpress/index.php;
        include       fastcgi_params;
    }

    error_page 404 /404.html;
    location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
}

中略

71
72
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
71
72