nginxを勉強中です。WordPressのパーマリンク設定を「月と投稿名」にしたのですが、404でエラーになります。
おそらく、nginx.confの設定が原因だと思うのですが、どのように設定すればいいか調べてもイマイチわかりません。教えていただけると幸いです。
nginx.conf
server {
listen 80;
server_name ドメイン名/blog;
return 301 https://ドメイン名/blog$request_uri;
}
server {
listen 443 ssl;
server_name ドメイン名/blog;
ssl_certificate ****;
ssl_certificate_key ****;
access_log /var/log/nginx/blog.access.log main;
location / {
root /var/www/html/blog;
index index.html index.htm index.php;
}
location ~ \.php$ {
root /var/www/html/blog;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}
# WordPressの設定
location @wordpress {
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/blog/index.php;
include fastcgi_params;
}
}
追記1 02:53
ay03さん
ご回答ありがとうございます。
とりあえず参考にしながら修正してみたところエラー表示が変わり、File not found.となりました。
サーバ側は、単にアドレス通りにディレクトリを読んでいるのでこのエラーに変わったんだと思いますが、原因はnginx.confの設定に間違いないと思います。
WordPressは、作成したサイトのブログページ部分のみに導入しているので、バーチャルホストを利用して設定しています。
一応、nginx.conf全文を載せておきます。
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name ドメイン名;
return 301 https://ドメイン名$request_uri;
}
server {
listen 443 ssl;
server_name ドメイン名;
ssl_certificate ***;
ssl_certificate_key ***;
charset UTF-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
root /var/www/html;
index index.html index.html index.php;
if ( -f $request_filename ) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /index.php last;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# ブログ
server {
listen 80;
server_name ドメイン名/blog;
return 301 https://ドメイン名/blog$request_uri;
}
server {
listen 443 ssl;
server_name ドメイン名/blog;
ssl_certificate ***;
ssl_certificate_key ***;
access_log /var/log/nginx/blog.access.log main;
location / {
root /var/www/html/blog;
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?$query_string;
if ( -f $request_filename ) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /index.php last;
}
}
location ~ \.php$ {
root /var/www/html/blog;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
fastcgi_read_timeout 300;
}
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PAHT_INFO $fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}
location ^~ /phpmyadmin {
alias /var/www/phpmyadmin;
index index.php index.html;
location ~ ^/phpmyadmin/(.+\.php)$ {
alias /var/www/phpmyadmin/$1;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# WordPressの設定
location @wordpress {
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME /var/www/html/blog/index.php;
include fastcgi_params;
}
}
}
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
0
突っ込み所があるかもしれないですが、NginxでSSLを使ってWordPressとphpMyAdminを動かしてる設定ファイルの例です。
パーマリンクがカスタム構造でも問題ないはずです。
参考になるかどうか分かりませんが、上手く読み替えて試してみてください。
/etc/nginx/conf.d/nginx.conf
server {
listen 80;
server_name 192.168.100.100;
root /var/www/html;
index index.php index.html index.htm;
listen 443 default ssl;
ssl on;
ssl_certificate /etc/pki/tls/certs/server.crt;
ssl_certificate_key /etc/pki/tls/certs/server.key;
location / {
try_files $uri $uri/ /index.php?$query_string;
root /var/www/html;
index index.html index.html index.php;
if ( -f $request_filename ) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /index.php last;
}
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PAHT_INFO $fastcgi_script_name;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}
location ^~ /phpmyadmin {
alias /var/www/phpmyadmin;
index index.php index.html;
location ~ ^/phpmyadmin/(.+\.php)$ {
alias /var/www/phpmyadmin/$1;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
設定ファイルを拝見したところ、rewriteの部分に問題がありそうな気がします。下記の部分をコメントアウトしてみてはいかがでしょうか?
if (!-e $request_filename) {
rewrite ^.+?(/.*\.php)$ $1 last;
rewrite ^ /index.php last;
}
あと気になった点がありまして、 server_nameディレクティブはホスト名を指定するものなので、
server_name ドメイン名/blog;というのはちょっと不思議に見えます。
もし書くとしたら
server {
server_name ドメイン名;
location /blog {
~wordpress向けの設定
}
というのが正攻法?かなと思います。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.33%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる