如何使用 Apache2 使 PHP 文件仅对本地主机可见,404 对其他 IP 可见


How to make a PHP file visible only to localhost, 404 for other IPs, using Apache2

我正在尝试限制哪些IP可以访问 example.com/adminer.php 用于管理系统的MySQL服务器。

我一直在尝试使用 apache 的 conf 文件、设置或工具来实现这一点,以便以后可以将此方法应用于其他文件或目录。

我已经按照网站上的说明安装了 Adminer,除了无法将其限制为 localhost,一切似乎都运行良好。

我尝试将以下内容添加到 apache2 security.conf 文件中,重新启动后没有运气。

系统: Debian 8.1

<Files /adminer.php>
   Order Deny,Allow
   Deny from all
   Allow from localhost
</Files>

我能想到的一种快速方法是在 php 文件中插入一些代码。

<?php
if ( ! ($_SERVER['HTTP_HOST'] == 'localhost' || $_SERVER['REMOTE_ADDR'] == '127.0.0.1') ) {
    header("HTTP/1.1 404 Not Found");   
    exit();
}

http://php.net/manual/en/function.header.php