元々の設定がこんな感じであったわけですが
~ 省略 ~
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
~ 省略 ~
あちこちで参照したところ「auth_basic」と「auth_basic_user_file」なるものを記述すればOKな感じで書いてあったので早速以下の記述を追加(赤字の箇所)
~ 省略 ~
auth_basic "secret";
auth_basic_user_file "/path/to/.htpasswd";
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
~ 省略 ~
するとまんまと全てのページが認証ページに、、、
サブドメイン切ってそこは全て認証ページにって事であればこれでOKなんでしょうが、特定のパスだけに適応したい場合はこれでは使えないので以下のように修正(赤字の箇所)
~ 省略 ~
location / {
try_files $uri $uri/ /index.html;
}
location /secret/ {
auth_basic "secret";
auth_basic_user_file "/path/to/.htpasswd";
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
~ 省略 ~
で、http://hoge/path/to/secret/ と実行したところBASIC認証のダイアログが!!
これでスッカリ安心しきっていたのですが次の日に認証パスに入っている「adminer.php」を開くと認証が出てこないで直接実行されてしまうという自体に、、、
で、あれこれ試行錯誤している時にみたこちらのページによると location の優先順位がなんたらと
要するにどの location が選ばれるかはプレフィックスが = > ^~ > ~,~* > なし の順番で評価され、同じ優先度であれば先に記述しているものでマッチするものが選ばれる
ただしプレフィックスなし(前方一致)の場合は最長でマッチするものが選ばれるということなようです
なので上記設定の場合 http://hoge.path/to/secret/ であれば location /secret/ が選択されますが、http://hoge/path/to/secret/adminer.php だと ~ を含んでいる location ~ \.php$ が選択されてしまう訳です、、、orz
なので以下のように修正してみる(赤字の箇所)
~ 省略 ~
location / {
try_files $uri $uri/ /index.html;
}
location ~ /secret/ {
auth_basic "secret";
auth_basic_user_file "/path/to/.htpasswd";
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
~ 省略 ~
すると http://hoge.path/to/secret/ でも http://hoge/path/to/secret/adminer.php でもBASIC認証のダイアログがシャキーン!と表示(この場合 /secret/ だと全て認証になっちゃいますが、、、)
ひとまずめでたしめでたし!!
が、喜びもつかの間、、、
IDとPWを入力したところ adminer.php のダウンロードが開始されるという自体に、、、
色々と考えた挙句 location ~ /tools/ が適用されているから location ~ \.php$ が適用されていないって結論で以下のように修正(赤字の箇所)
~ 省略 ~
location / {
try_files $uri $uri/ /index.html;
}
location ~ /secret/.*\.php$ {
auth_basic "secret";
auth_basic_user_file "/path/to/.htpasswd";
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /secret/ {
auth_basic "secret";
auth_basic_user_file "/path/to/.htpasswd";
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
~ 省略 ~
一応上手く実行されるようにはなったもののなんとも長ったらしい設定になってしまった、、、(この記事も)
もっとシンプルに書く方法ってないのかな?