AWSのALBを使用してプライベートインスタンスに接続を行おうとすると404エラーが発生してしまう。
404エラー画面
※写真の補足
- Load balancerは指定してあります。
- Targetにはプライベートインスタンスを設定しております。
- 現状、デプロイ中の為、インスタンスは一つのみとなっております。
- 後述しておりますが、EC2事態にアクセスを行おうと試みてはいるようです。
ヘルスチェック
簡単にやっていることの説明
フロントエンドにAngular
バックエンドにLaravel sanctumを使った
アプリケーションをデプロイしようと試みている
実現したいこと
- Target画面に表示されている404エラーを解消したい。
- インスタンス内にある、Laravel sanctumに接続したい。
自分が思うエラーの原因
Laravelのindex.phpをALB側で確認できていないために404エラーになっていると思われる。
環境
nginx: 1.22.1
PHP: 8.1.14
Composer: 2.5.2
Laravel: 9.31.0
確認が取れているもの
- sudo systemctl status nginx.service実行結果
nginx.serviceのステータス
1● nginx.service - The nginx HTTP and reverse proxy server 2 Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled) 3 Drop-In: /usr/lib/systemd/system/nginx.service.d 4 mqphp-fpm.conf 5 Active: active (running) since Tue 2023-02-21 12:21:01 UTC; 2h 57min ago 6
- sudo systemctl status php-fpm実行結果
php
1● php-fpm.service - The PHP FastCGI Process Manager 2 Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled) 3 Active: active (running) since Tue 2023-02-21 12:20:59 UTC; 2h 59min ago 4 Main PID: 2951 (php-fpm) 5 Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec" 6 CGroup: /system.slice/php-fpm.service 7 tq2951 php-fpm: master process (/etc/php-fpm.conf) 8
設定周りのファイル一覧
ディレクトリ
/etc/nginx/nginx.conf
nginx.conf
1# For more information /var/www/src/publicon configuration, see: 2# * Official English Documentation: http://nginx.org/en/docs/ 3# * Official Russian Documentation: http://nginx.org/ru/docs/ 4 5user nginx; 6worker_processes auto; 7error_log /var/log/nginx/error.log; 8pid /run/nginx.pid; 9 10# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. 11include /usr/share/nginx/modules/*.conf; 12 13events { 14 worker_connections 1024; 15} 16 17http { 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"'; 21 22 access_log /var/log/nginx/access.log main; 23 24 sendfile on; 25 tcp_nopush on; 26 tcp_nodelay on; 27 keepalive_timeout 65; 28 types_hash_max_size 4096; 29 30 include /etc/nginx/mime.types; 31 default_type application/octet-stream; 32 33 # Load modular configuration files from the /etc/nginx/conf.d directory. 34 # See http://nginx.org/en/docs/ngx_core_module.html#include 35 # for more information. 36 include /etc/nginx/conf.d/*.conf; 37 38 server { 39 listen 80; 40 listen [::]:80; 41 server_name _; 42 root /var/www/src/public; 43 44 add_header X-Frame-Options "SAMEORIGIN"; 45 add_header X-XSS-Protection "1; mode=block"; 46 add_header X-Content-Type-Options "nosniff"; 47 48 index index.php; 49 50 charset utf-8; 51 52 location / { 53 try_files $uri $uri/ /index.php?$query_string; 54 } 55 56 location ~ \.php$ { 57 fastcgi_pass unix:/run/php-fpm/www.sock; 58 fastcgi_index index.php; 59 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 60 include fastcgi_params; 61 } 62 # Load configuration files for the default server block. 63 include /etc/nginx/default.d/*.conf; 64 } 65 66}
index.phpディレクトリ
1var 2└── www 3 └── src 4 └── public 5 └── index.php 6
発生している問題・エラーメッセージ
/var/log/nginx/access.logにあるaccess.logを確認するとELBからtargetに対して、アクセスを行おうとしていることは確認ができます。
Nginxのアクセスログ
access.log
1172.16.24.58 - - [21/Feb/2023:14:14:21 +0000] "GET / HTTP/1.1" 404 153 "-" "ELB-HealthChecker/2.0" "-" 2172.16.23.109 - - [21/Feb/2023:14:14:21 +0000] "GET / HTTP/1.1" 404 153 "-" "ELB-HealthChecker/2.0" "-" 3172.16.24.58 - - [21/Feb/2023:14:14:51 +0000] "GET / HTTP/1.1" 404 153 "-" "ELB-HealthChecker/2.0" "-" 4172.16.23.109 - - [21/Feb/2023:14:14:51 +0000] "GET / HTTP/1.1" 404 153 "-" "ELB-HealthChecker/2.0" "-"
※アクセスログをみて、アクセスが飛んできていることは確認できているのですが、
/var/log/nginx/error.log にはエラーが一切記載されておりません。
試したこと
nginx.confを書き換え以下のように書換え→404エラーのまま
nginx.conf
1root /var/www/src/public; //変更前 2root /var/www/src/public/; //変更後
理由 index.phpをALBが認識できていないために、実行できていないと思った為、上記のように修正
nginx.confを書き換え以下のように書換え→404エラーのまま
nginx.conf
1 location / { 2 try_files $uri $uri/ /index.php?$query_string;//変更前 3 } 4 5 location / { 6 root /var/www/src/public; //追加 7 try_files $uri $uri/ /index.php?$query_string;//変更後 8 }
理由 index.phpをALBが認識できていないために、実行できていないと思った為、上記のように修正
下記URLを参照した際、記述ミスなのではないかと思った為。
https://cloudsmith.co.jp/blog/2022/02/2030266.html
AbeTakashi様のご回答のもとをにして、修正した結果。
修正方法
・nginx.confを※1のURLを参考にして修正
・healthのcheck pathを/nginxに修正
・nginxをsudo nginx -s reloadで再起動
※1のURLを参考にnginx.confを修正させていただきました。
※1https://qiita.com/oogaki_newmedia/items/749c855ad985c8258e66
そしてhealth PathのURLを
/nginxに変更しました。
しかし404が解消されていないようです。
index.htmlは/usr/share/nginx/html/index.htmlに配置しております。
修正後のaccess.log
172.16.23.100 - - [22/Feb/2023:16:57:12 +0000] "GET /nginx HTTP/1.1" 404 153 "-" "ELB-HealthChecker/2.0" "-" 172.16.24.168 - - [22/Feb/2023:16:57:42 +0000] "GET /nginx HTTP/1.1" 404 153 "-" "ELB-HealthChecker/2.0" "-" 172.16.23.100 - - [22/Feb/2023:16:57:42 +0000] "GET /nginx HTTP/1.1" 404 153 "-" "ELB-HealthChecker/2.0" "-"
修正後のnginx.conf
1 # For more information /var/www/src/publicon configuration, see: 2 # * Official English Documentation: http://nginx.org/en/docs/ 3 # * Official Russian Documentation: http://nginx.org/ru/docs/ 4 5 user nginx; 6 worker_processes auto; 7 error_log /var/log/nginx/error.log; 8 pid /run/nginx.pid; 9 10 # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. 11 include /usr/share/nginx/modules/*.conf; 12 13 events { 14 worker_connections 1024; 15 } 16 17 http { 18 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 19 '$status $body_bytes_sent "$http_referer" ' 20 '"$http_user_agent" "$http_x_forwarded_for"'; 21 22 access_log /var/log/nginx/access.log main; 23 24 sendfile on; 25 tcp_nopush on; 26 tcp_nodelay on; 27 keepalive_timeout 65; 28 types_hash_max_size 4096; 29 30 include /etc/nginx/mime.types; 31 default_type application/octet-stream; 32 33 # Load modular configuration files from the /etc/nginx/conf.d directory. 34 # See http://nginx.org/en/docs/ngx_core_module.html#include 35 # for more information. 36 include /etc/nginx/conf.d/*.conf; 37 38 server { 39 listen 80; 40 listen [::]:80; 41 server_name _; 42 root /var/www/src/public/; 43 44 add_header X-Frame-Options "SAMEORIGIN"; 45 add_header X-XSS-Protection "1; mode=block"; 46 add_header X-Content-Type-Options "nosniff"; 47 48 index index.php index.html; 49 50 charset utf-8; 51 52 location / { 53 root /var/www/src/public/; 54 try_files $uri $uri/ /index.php?$query_string; 55 } 56 57 location /nginx { 58 root /usr/share/; 59 try_files $uri $uri/ /nginx/html/index.html; 60 } 61 62 location ~ \.php$ { 63 fastcgi_pass unix:/run/php-fpm/www.sock; 64 fastcgi_index index.php; 65 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 66 include fastcgi_params; 67 } 68 # Load configuration files for the default server block. 69 include /etc/nginx/default.d/*.conf; 70 } 71 72 }
また、Laravelのエラーログは表示されていませんでした。
一番最新のログです。
[2023-02-10 23:56:10] production.ERROR: Call to undefined function Termwind\ValueObjects\mb_strimwidth() {"exception":"[object] (Error(code: 0): Call to undefined function Termwind\\ValueObjects\\mb_strimwidth() at /var/www/src/vendor/nunomaduro/termwind/src/ValueObjects/Styles.php:1043)
最後に
足りていない情報などがありましたら、コメントしていただければ、早めに添付させていただきたいと思います。
また、言葉として足りない箇所やわかりにくい箇所がありましたら、コメントいただければ、修正したいと思います。
何か、ヒントや参考になりますURLなどがありましたら、教えていただけれると幸いです。
よろしくお願いいたします。
回答2件
あなたの回答
tips
プレビュー