LoginSignup
10
13

More than 5 years have passed since last update.

Nginx環境でのphpMyAdminに対する外部ホストからのアクセス制限でハマった部分

Posted at

結果的にNginxでのlocationの書き方が問題で起こった事です。

phpMyAdmin/のフォルダに対するアクセス制限が成功したのに、
phpMyAdmin/index.phpにアクセスすると見れてしまう。

ネットを探していた時によく見かけたのが下記の形

nginx.conf

    listen       80;
    server_name  [公開ドメイン];

        root   /var/www/html/;
        index  index.php index.html index.htm;

    location /phpMyAdmin {
        allow [接続許可したいIP];
        deny all;
    }

    location ~ \.php$ {
        fastcgi_keep_conn on;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

まずローカル内[許可しているIP]で
http://hoge/phpMyAdminに接続
...成功

次にiphone[許可していないIP]で
 http://hoge/phpMyAdminに接続
...失敗403

結果的にアクセス制御出来てるようなので、一見大丈夫なように見えたのですが・・・

今度はiphone[許可していないIP]から
 http://hoge/phpMyAdmin/index.phpに接続
...成功

ん?

・・・んん?

なんで繋がるんだ!?

「ネットに書いてある通りなんだが!」とよくあることを叫びつつ、試行錯誤を繰り返しながら結果的には下のコードでうまくいきました。

nginx.conf

    listen       80;
    server_name  [公開ドメイン];

        root   /var/www/html/;
        index  index.php index.html index.htm;

    location /phpMyAdmin {
        allow 127.0.0.1;
        deny all;
        location ~ \.php$  {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        
            include        fastcgi_params;
        }
    }

    location ~ \.php$ {
        fastcgi_keep_conn on;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

いろいろ試した結果
最初の設定はphpMyAdminのフォルダはアクセス制限をかけたが、そのフォルダ内の.phpファイルに対するアクセス自体は
 
location ~ .php$ {
}

の方が優先されてしまって
.phpファイルのみ参照できるようになっていたみたいです。

なので

location /phpMyAdmin { 
の中に
location ~ .php$ {
をネストとして

phpMyAdmin内の.phpファイルにアクセスしたときはphpMyAdminのネストを通るように優先順位を変更しました。

iphoneでアクセスしても無事アクセス制限がかかり、めでたし。

10
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
13