はじめに
Dockerで環境構築をして、開発を進めていざブラウザから確認しようとしたら開発したどのページにリクエストを送っても404エラーしか返ってこない。
http://localhost/ にリクエストを送った時だけは お馴染みのWelcome画面(下記の画面)は表示される。
というエラーに直面したので、忘備録として残しておきます。
開発環境
言語・ツールなど | バージョン |
---|---|
PHP | 7.2.26 |
Laravel | 5.11.0 |
nginx | 1.17.7 |
docker | 19.03.5 |
ルーティングの確認
$ php artisan route:list
+--------+----------+----------------+---------+-----------------------------------------------+-------------+
| Domain | Method | URI | Name | Action | Middleware |
+--------+----------+----------------+---------+-----------------------------------------------+-------------+
| | GET|HEAD | / | | Closure | web |
| | GET|HEAD | authors/create | | App\Http\Controllers\AuthorsController@create | web |
http:localhost/authors/createページにGETメソッドをリクエストしている箇所は適切にルーティングされていて、特に問題なさそう。。。
パーミッションの確認
$ ls -l
-rw-r--r-- 1 root root 4455 Feb 1 11:49 README.md
drwxr-xr-x 10 root root 320 Feb 1 11:55 app
-rw-r--r-- 1 root root 1686 Feb 1 11:49 artisan
drwxr-xr-x 4 root root 128 Feb 1 11:49 bootstrap
-rw-r--r-- 1 root root 1531 Feb 1 11:55 composer.json
-rw-r--r-- 1 root root 184470 Feb 1 11:55 composer.lock
drwxr-xr-x 15 root root 480 Feb 1 11:49 config
drwxr-xr-x 7 root root 224 Feb 1 11:49 database
drwxr-xr-x 723 root root 23136 Feb 1 11:39 node_modules
-rw-r--r-- 1 root root 455603 Feb 1 11:55 package-lock.json
-rw-r--r-- 1 root root 1113 Feb 1 11:55 package.json
-rw-r--r-- 1 root root 1513 Feb 1 11:52 phpunit.xml
drwxr-xr-x 9 root root 288 Feb 1 11:49 public
drwxr-xr-x 6 root root 192 Feb 1 11:49 resources
drwxr-xr-x 6 root root 192 Feb 1 13:52 routes
-rw-r--r-- 1 root root 563 Feb 1 11:49 server.php
drwxr-xr-x 5 root root 160 Feb 1 11:49 storage
-rw-r--r-- 1 root root 82 Feb 1 11:03 tailwind.config.js
drwxr-xr-x 6 root root 192 Feb 1 11:54 tests
drwxr-xr-x 41 root root 1312 Jan 30 14:21 vendor
-rw-r--r-- 1 root root 712 Feb 1 11:55 webpack.mix.js
パーミッションに関してもrwが付与されているので特に問題はなさそう。
nginxの設定
結論としてはnginxの設定に問題がありました。
locationディレクティブでindex index.html index.php;としていたからでした。
これではindex.htmlとindex.phpしか開かないという設定になってしまうんですね。
適切には、URIのパスごとのパスを指定してあげる必要があったんですね。
そのため、locationディレクティブを下記に修正したら無事にレスポンスが返ってきました。
location / {
try_files $uri $uri/ /index.php?$query_string;
}
修正前のdefault.conf
server {
listen 80;
index index.php index.html;
root /var/www/public;
location / {
index index.html index.php;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
修正後のdefault.conf
server {
listen 80;
index index.php index.html;
root /var/www/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}