前提
http { # デバッグ用 log_format debug "[DEBUG][$time_local] $debug_data";
を使うとデバッグしやすので、nginx.conf に仕込んでおく。
/home/gallu/test
ってディレクトリを用意して、ここを起点にする(あとで追加するけど)。
既知部分の確認
閲覧できるようにする
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # root /home/gallu/test/; set $debug_data ""; #access_log /var/log/nginx/debug.log debug; }
うんまぁ普通に見れる。
location でくくっておく
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # location / { root /home/gallu/test/; } set $debug_data ""; #access_log /var/log/nginx/debug.log debug; }
OK。
ディレクトリによっては「別のDocumentRootを見る」ようにする
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # location /foo/ { alias /home/gallu/test2/; } # location / { root /home/gallu/test/; } set $debug_data ""; #access_log /var/log/nginx/debug.log debug; }
これもOK。
なお、 /foo/ の所、rootにすると上手くいかないのでaliasにするとよいです。
細かいあたりは "nginx root alias" あたりでググると色々出てくるので省略。
ちょっとこの辺りで多少なりデバッグログ出してみよう。
setの位置間違えると色々齟齬るっぽいので、少しその辺修正。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # location /foo/ { alias /home/gallu/test2/; set $debug_data "$document_root $request_uri $uri"; } # location / { root /home/gallu/test/; set $debug_data "$document_root $request_uri $uri"; } # access_log /var/log/nginx/debug.log debug; }
これで
[DEBUG][24/Feb/2023:16:43:22 +0900] /home/gallu/test / /index.html
[DEBUG][24/Feb/2023:16:43:30 +0900] /home/gallu/test2/ /foo/ /foo/index.html
って出てきたので、まぁ大体意図通り。
index.htmlが勝手に付与されているのがうっすら気になるので一応軽く調べてみる。
http://mogile.web.fc2.com/nginx/http/ngx_http_index_module.html#index
……うん「デフォルトがindex.html」になってるっぽいので、んじゃ納得。
ここで
・rootとかaliasとかで、意図通りのdocument_rootになっている
・$request_uriには「リクエスト時のURI(のpath以降部分)」が入ってる
・$uriには「(今回は、indexで補完された)path名が入っている」
あたりまでが担保できてる感じ。
一応、変数のことも調べておく。
http://nginx.org/en/docs/http/ngx_http_core_module.html
$document_root root or alias directive’s value for the current request
$request_uri full original request URI (with arguments)
$uri current URI in request, normalized
うん大体認識一致。
「uriはnormalizedされてる」から、indexで補完されてるんだね。
怪しい目な既知部分の確認
さて。まずは単純に「PHPを動かす」から。
なんとなくざっくり書いてみる。
fastcgi_passでphp-fpmの指定、includeで「必要な諸々のデータ」の設定、fastcgi_indexでは「pathがディレクトリだけの時のデフォルトのindexファイル名」、fastcgi_param SCRIPT_FILENAMEで「SCRIPT_FILENAMEの設定の上書き(fastcgi.confにも書いてあるから)」。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # location /foo/ { alias /home/gallu/test2/; set $debug_data "foo) $document_root $request_uri $uri"; } # location / { root /home/gallu/test/; set $debug_data "/) $document_root $request_uri $uri"; } # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name"; } # access_log /var/log/nginx/debug.log debug; }
で……駄目。
[DEBUG][24/Feb/2023:18:16:17 +0900] / php) /usr/share/nginx/html /t.php /t.php /t.php
ほむ……document_rootが、意図している location / じゃなくて、デフォルト向いてるねぇ。
locationの設定、なんとなく「1つしか」選ばれない、んだとすると、location / の中に入れないとかしらん?
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # location /foo/ { alias /home/gallu/test2/; set $debug_data "foo) $document_root $request_uri $uri"; } # location / { root /home/gallu/test/; set $debug_data "/) $document_root $request_uri $uri"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name"; } } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:18:18:01 +0900] / php) /home/gallu/test /t.php /t.php /t.php
動いた。当たりっぽい。
んじゃtest2も同じように。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # location /foo/ { alias /home/gallu/test2/; set $debug_data "foo) $document_root $request_uri $uri"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name"; } } # location / { root /home/gallu/test/; set $debug_data "/) $document_root $request_uri $uri"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name"; } } # access_log /var/log/nginx/debug.log debug; }
「File not found.」ありゃ?
[DEBUG][24/Feb/2023:18:19:48 +0900] foo php) /home/gallu/test2/ /foo/t.php /foo/t.php /foo/t.php
む。
これだと、SCRIPT_FILENAME に /home/gallu/test2//foo/t.php って入ってる。fooが邪魔やな。
調べると $request_filename ってのがあるらしい。
http://nginx.org/en/docs/http/ngx_http_core_module.html
$request_filename
file path for the current request, based on the root or alias directives, and the request URI
使えるか?
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # location /foo/ { alias /home/gallu/test2/; set $debug_data "foo) $document_root $request_uri $uri"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename"; } } # location / { root /home/gallu/test/; set $debug_data "/) $document_root $request_uri $uri"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name"; } } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:18:22:21 +0900] foo php) /home/gallu/test2/ /foo/t.php /foo/t.php /foo/t.php /home/gallu/test2/t.php
よし、乗った。
……ってことは、 location / のほうもこっちでいいんぢゃね?
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # location /foo/ { alias /home/gallu/test2/; set $debug_data "foo) $document_root $request_uri $uri"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename"; } } # location / { root /home/gallu/test/; set $debug_data "/) $document_root $request_uri $uri"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name $request_filename"; } } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:18:23:49 +0900] / php) /home/gallu/test /t.php /t.php /t.php /home/gallu/test/t.php
いけたいけた。
よしとりあえずこれを1つの起点にしよう。
フレームワークでルーティングとかしない系なら、大体これでいけそうだ。
……とはいえ世間的には「フレームワーク? 使わない理由ないよね?」くらいの勢いも多いので、ここからさらに魔窟に踏み込んでみよう。
未知部分への踏み込み
try_files を使った内部的な移動
鍵になるのは try_files というヤツですね。
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the file parameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made.
大雑把には「ファイルの存在をざっくりナメていって、なかったら一番最後の引数に従う」的な感じだと思われます。
ちょうどtest.htmlがあるので、「なかったらtest.htmlにする」って感じのを書いてみましょう。
ほぼまっさらな所から、改めて書いてみます。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # root /home/gallu/test/; # try_files $uri $uri/ /test.html ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename"; # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:18:38:50 +0900] /) /home/gallu/test / /index.html /index.html /home/gallu/test/index.html
[DEBUG][24/Feb/2023:18:38:58 +0900] /) /home/gallu/test /test.html /test.html /test.html /home/gallu/test/test.html
[DEBUG][24/Feb/2023:18:39:14 +0900] /) /home/gallu/test /foo/ /test.html /test.html /home/gallu/test/test.html
[DEBUG][24/Feb/2023:18:39:58 +0900] /) /home/gallu/test /foo/hoge /test.html /test.html /home/gallu/test/test.html
うん、意図通り。
見る限りでは「request_uriは変わらず。uriとfastcgi_script_nameとrequest_filenameは書き換わる」感じなんだ。
あと一応、HTMLの時点で「URLに付けるパラメタ」あたりを確認しておこう。
変数的には以下があるぽい。
$is_args
“?” if a request line has arguments, or an empty string otherwise$args
arguments in the request line$query_string
same as $args
ざっとconfigに書いて、値を確認。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # root /home/gallu/test/; # try_files $uri $uri/ /test.html ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args is:$is_args (fin"; # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:18:47:38 +0900] /) /home/gallu/test /index.html /index.html /index.html /home/gallu/test/index.html ar: is: (fin
[DEBUG][24/Feb/2023:18:47:47 +0900] /) /home/gallu/test /index.html?hoge=hoge /index.html /index.html /home/gallu/test/index.html ar:hoge=hoge is:? (fin
[DEBUG][24/Feb/2023:18:48:06 +0900] /) /home/gallu/test /index.html?hoge=hoge&foo=foo /index.html /index.html /home/gallu/test/index.html ar:hoge=hoge&foo=foo is:? (fin
[DEBUG][24/Feb/2023:18:48:27 +0900] /) /home/gallu/test /foo/hoge=hoge&foo=foo /test.html /test.html /home/gallu/test/test.html ar: is: (fin
ほむ。……あぁtry_filesで書き換わるから、最後のやつ、パラメタ引き継いでないのか。
みると「is_argsで?の有無、argsで(あったら)パラメタ」って感じだなぁ。
修正。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; # root /home/gallu/test/; # try_files $uri $uri/ /test.html$is_args$args ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args is:$is_args (fin"; # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:18:50:54 +0900] /) /home/gallu/test /foo/?hoge=hoge&foo=foo /test.html /test.html /home/gallu/test/test.html ar:hoge=hoge&foo=foo is:? (fin
try_files でPHPを絡めてみる
パラメタ付きでちゃんと動くか、まずはざっくり思いつきで。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location / { root /home/gallu/test/; try_files $uri $uri/ /test.php$is_args$args ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args is:$is_args (fin"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args is:$is_args (fin"; } } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:18:56:02 +0900] / php) /home/gallu/test /foo/?hoge=hoge&foo=foo /test.php /test.php /home/gallu/test/test.php ar:hoge=hoge&foo=foo is:? (fin
あ、動いた。
うん……なんとなくわかってはきたなぁ。
&
PHP側で確認をするに、「$_SERVER['REQUEST_URI']」でちゃんと「元々のcallしているURL(とパラメタ)」はとれるので。
多分、フレームワーク側の処理はそんなに問題ないんじゃなかろうか? と予想してみる感じ。
未知の本番
ルート直下にLaravelを入れてみる
composer create-project laravel/laravel
でLaravelをざっくりinstall。
rootを少し書き換えて、まずは動かしてみる。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location / { root /home/gallu/test/public/ ; try_files $uri $uri/ /index.php$is_args$args ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:20:46:49 +0900] /) /home/gallu/test/public / / / /home/gallu/test/public/ ar:
あれ? 403になる……あぁ index 入れないとだめか。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location / { root /home/gallu/test/public/ ; index index.php index.html; try_files $uri $uri/ /index.php$is_args$args ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:20:48:46 +0900] / php) /home/gallu/test/public / /index.php /index.php /home/gallu/test/public/index.php ar:
よしOK。
(Laravelの設定なんもやってないからエラー出てるけどその辺は範疇外なので除外)
念のために、雑に
Route::get('/hoge', function () { return 'hoge'; });
のルーティングを追加して確認……挙動OK。
サブディレクトリに「別のフレームワーク」をぶちこんでみる
さてメインの本題のど真ん中。
まずは準備。
手持ちのSlim-Skeletonを入れる。
composer create-project gallu/slim4-skeleton test2
今までの前提を込めて、まずはベタっと書いてみよう。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location /foo/ { alias /home/gallu/test2/public/ ; index index.php index.html; try_files $uri $uri/ /index.php$is_args$args ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } } location / { root /home/gallu/test/public/ ; index index.php index.html; try_files $uri $uri/ /index.php$is_args$args ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } } # access_log /var/log/nginx/debug.log debug; }
あと、PHP側で
$app->setBasePath('/foo');
を書いてサブディレクトリを指定。
[DEBUG][24/Feb/2023:21:02:54 +0900] foo php) /home/gallu/test2/public/ /foo/ /foo/index.php /foo/index.php /home/gallu/test2/public/index.php ar:
よしOK。
……ただここからパラメタを渡すと
[DEBUG][24/Feb/2023:21:04:01 +0900] / php) /home/gallu/test/public /foo/sample /index.php /index.php /home/gallu/test/public/index.php ar:
ってなる。うん元々の話に戻った。location /foo/ に入ってこずに location / に入っていってしまっているから、DocumentRootが「向いて欲しい所とは別に」なる。
try_filesから先を一足飛びにしすぎたなぁ。
ちょっと戻そう。
location + try_files をもうちょっと深掘りする
いったんFWを外してまたhtmlだけのシンプルな状態にしてテスト。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location /foo/ { alias /home/gallu/test2/ ; index index.php index.html; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } location / { root /home/gallu/test/ ; index index.php index.html; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:21:13:34 +0900] /) /home/gallu/test / /index.html /index.html /home/gallu/test/index.html ar:
[DEBUG][24/Feb/2023:21:13:36 +0900] foo) /home/gallu/test2/ /foo/ /foo/index.html /foo/index.html /home/gallu/test2/index.html ar:
うん動いてる。
ここからこのまま、まずは / のほうだけ try_files を追加して動かしてみよう。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location /foo/ { alias /home/gallu/test2/ ; index index.php index.html; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } location / { root /home/gallu/test/ ; index index.php index.html; try_files $uri $uri/ /test.html ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:21:15:46 +0900] /) /home/gallu/test / /index.html /index.html /home/gallu/test/index.html ar:
[DEBUG][24/Feb/2023:21:15:53 +0900] /) /home/gallu/test /test.html /test.html /test.html /home/gallu/test/test.html ar:
[DEBUG][24/Feb/2023:21:15:58 +0900] /) /home/gallu/test /hogera /test.html /test.html /home/gallu/test/test.html ar:
うん、ここまでは想定通り。
さて……fooにも同じように適用してみよう。多分、ここが鍵。
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location /foo/ { alias /home/gallu/test2/ ; index index.php index.html; try_files $uri $uri/ /test.html ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } location / { root /home/gallu/test/ ; index index.php index.html; try_files $uri $uri/ /test.html ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:21:17:26 +0900] foo) /home/gallu/test2/ /foo/ /foo/index.html /foo/index.html /home/gallu/test2/index.html ar:
[DEBUG][24/Feb/2023:21:17:31 +0900] foo) /home/gallu/test2/ /foo/test.html /foo/test.html /foo/test.html /home/gallu/test2/test.html ar:
[DEBUG][24/Feb/2023:21:17:38 +0900] /) /home/gallu/test /foo/hogera /test.html /test.html /home/gallu/test/test.html ar:
うん、ここだ。
/foo/hogera のアクセスの時に、 location /foo/ に入らずに location / に入ってるんだ。ここが元凶。
……あぁ違う、/foo/ の中のtry_filesの書き方か?
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location ^~ /foo/ { alias /home/gallu/test2/ ; index index.php index.html; try_files $uri $uri/ /foo/test.html ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } location / { root /home/gallu/test/ ; index index.php index.html; try_files $uri $uri/ /test.html ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:21:24:04 +0900] foo) /home/gallu/test2/ /foo/hogera /foo/test.html /foo/test.html /home/gallu/test2/test.html ar:
[DEBUG][24/Feb/2023:21:24:06 +0900] foo) /home/gallu/test2/ /foo/ /foo/index.html /foo/index.html /home/gallu/test2/index.html ar:
[DEBUG][24/Feb/2023:21:24:10 +0900] foo) /home/gallu/test2/ /foo/test.html /foo/test.html /foo/test.html /home/gallu/test2/test.html ar:
よし!!
念のため。
[DEBUG][24/Feb/2023:21:24:28 +0900] /) /home/gallu/test / /index.html /index.html /home/gallu/test/index.html ar:
[DEBUG][24/Feb/2023:21:24:37 +0900] /) /home/gallu/test /test.html /test.html /test.html /home/gallu/test/test.html ar:
[DEBUG][24/Feb/2023:21:24:42 +0900] /) /home/gallu/test /hogera /test.html /test.html /home/gallu/test/test.html ar:
いけた。
改めてフレームワークで確認
server { listen 80; server_name dev.example.net; access_log /var/log/nginx/dev.example.net.access.log main; error_log /var/log/nginx/dev.example.net.error.log; location /foo/ { alias /home/gallu/test2/public/ ; index index.php index.html; try_files $uri $uri/ /foo/index.php$is_args$args ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } } location / { root /home/gallu/test/public/ ; index index.php index.html; try_files $uri $uri/ /index.php$is_args$args ; set $debug_data "/) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "/ php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } } # access_log /var/log/nginx/debug.log debug; }
[DEBUG][24/Feb/2023:21:26:38 +0900] / php) /home/gallu/test/public / /index.php /index.php /home/gallu/test/public/index.php ar:
[DEBUG][24/Feb/2023:21:27:01 +0900] / php) /home/gallu/test/public /hoge /index.php /index.php /home/gallu/test/public/index.php ar:
[DEBUG][24/Feb/2023:21:27:09 +0900] foo php) /home/gallu/test2/public/ /foo/ /foo/index.php /foo/index.php /home/gallu/test2/public/index.php ar:
[DEBUG][24/Feb/2023:21:27:14 +0900] foo) /home/gallu/test2/public/ /foo/sample /foo/sample /foo/sample /home/gallu/test2/public/sample ar:
サブディレクトリ側。とりあえずlocationはちゃんと入ったので、もうちょいだなぁ。
[DEBUG][24/Feb/2023:21:29:03 +0900] foo) /home/gallu/test2/public/ /foo/css/dummy /foo/css/dummy /foo/css/dummy /home/gallu/test2/public/css/dummy ar:
うん、存在しているファイルに対しては問題なく動くんだ。
なので「存在していないファイルの時にtry_filesが適用されて、request_filename が index.phpになる」ところさえ担保できればよい感じだなぁ。
なんだろ……とりあえず「上手くいってる」パターンで書き換えてみよう。
location /foo/ { alias /home/gallu/test2/public/ ; index index.php index.html; #try_files $uri $uri/ /foo/index.php$is_args$args ; try_files $uri $uri/ /foo/test.html ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } }
[DEBUG][24/Feb/2023:21:37:26 +0900] foo) /home/gallu/test2/public/ /foo/aaa /foo/test.html /foo/test.html /home/gallu/test2/public/test.html ar:
うまくいくんかい……なんぞ???
ちょっとずつ進めていこう。
とりあえず「test.php」とかってやったらどうなるのかしらん?
location /foo/ { alias /home/gallu/test2/public/ ; index index.php index.html; #try_files $uri $uri/ /foo/index.php$is_args$args ; #try_files $uri $uri/ /foo/test.html ; try_files $uri $uri/ /foo/test.php ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } }
[DEBUG][24/Feb/2023:21:40:08 +0900] foo php) /home/gallu/test2/public/ /foo/aaa /foo/test.php /foo/test.php /home/gallu/test2/public/test.php ar:
うごくやん……次。
location /foo/ { alias /home/gallu/test2/public/ ; index index.php index.html; #try_files $uri $uri/ /foo/index.php$is_args$args ; #try_files $uri $uri/ /foo/test.html ; #try_files $uri $uri/ /foo/test.php ; try_files $uri $uri/ /foo/index.php ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } }
[DEBUG][24/Feb/2023:21:41:52 +0900] foo php) /home/gallu/test2/public/ /foo/aaa /foo/index.php /foo/index.php /home/gallu/test2/public/index.php ar:
あれ? 動く?
……戻してみよう。
location /foo/ { alias /home/gallu/test2/public/ ; index index.php index.html; #try_files $uri $uri/ /foo/index.php$is_args$args ; #try_files $uri $uri/ /foo/test.html ; try_files $uri $uri/ /foo/test.php ; #try_files $uri $uri/ /foo/index.php ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } }
[DEBUG][24/Feb/2023:21:45:42 +0900] foo) /home/gallu/test2/public/ /foo/sample /foo/sample /foo/sample /home/gallu/test2/public/sample ar:
うん戻った……$is_args$args が悪さしてる? パラメタないのに……これ、付けないとパラメタ渡らないんかなぁ???
試してみよう。
location /foo/ { alias /home/gallu/test2/public/ ; index index.php index.html; #try_files $uri $uri/ /foo/index.php$is_args$args ; #try_files $uri $uri/ /foo/test.html ; try_files $uri $uri/ /foo/test.php ; #try_files $uri $uri/ /foo/index.php ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } }
結果……うん$_SERVER['REQUEST_URI']には入ってるんだけど、$_GETには入っていかないねぇ……念のため追試。
location /foo/ { alias /home/gallu/test2/public/ ; index index.php index.html; #try_files $uri $uri/ /foo/index.php$is_args$args ; #try_files $uri $uri/ /foo/test.html ; #try_files $uri $uri/ /foo/test.php ; try_files $uri $uri/ /foo/test.php$is_args$args ; #try_files $uri $uri/ /foo/index.php ; set $debug_data "foo) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; include fastcgi.conf; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $request_filename; set $debug_data "foo php) $document_root $request_uri $uri $fastcgi_script_name $request_filename ar:$args"; } } >|| [DEBUG][24/Feb/2023:21:50:50 +0900] foo) /home/gallu/test2/public/ /foo/aaa?a=b&c=d /foo/aaa /foo/aaa /home/gallu/test2/public/aaa ar:a=b&c=d
うん、駄目だ orz
「もうちょい」なんだが、そこで煮詰まったなぁ……ちょっと放置。
後で見直したらなんか気づくかもしれないし誰かが突っ込んでくれるかもしれないので、いったん、寝かせよう orz