Apache 2.4 中的 htaccess 目录保护

Apache 2.4 中的 htaccess 目录保护

我正在尝试研究如何保护 Apache 2.4 中的文件夹。

我这部分工作正常:

AuthUserFile   /home/test/web/site.com/cgi-bin/hotels/admin/.htpasswd
AuthGroupFile  /dev/null
AuthType       Basic
AuthName       Protected

require valid-user

...但我还想添加允许我无需登录即可进入的选项。这是我在旧服务器上的选项:

Order Deny,Allow
Deny from all
Allow from 123.123.123.123
Satisfy Any

但新的却出现错误:

client denied by server configuration: /home/test/web/site.com/cgi-bin/hotels/admin/admin.cgi

看着https://wiki.apache.org/httpd/ClientDeniedByServerConfiguration,我本来以为这会起作用:

Require all denied
Allow from 123.123.123.123
Satisfy Any

...但似乎没有(现在甚至不需要登录,即使我将接受的 IP 更改为随机 IP)

我错过了什么?

答案1

不要将 2.2 和 2.4 指令混用。Satisfy/Order/Allow/Deny 都是 2.2.x

仅使用 2.4 并卸载 mod_access_compat:

AuthType Basic
AuthName "Authorized Users Only"
AuthBasicProvider file
AuthUserFile /home/test/web/site.com/cgi-bin/hotels/admin/.htpasswd
<RequireAny>
Require valid-user
Require ip 123.123.123.123
</RequireAny>

注意:htaccess 与目录保护无关,如果您有权访问 apache httpd 服务器的主要配置文件,请将这些文件全部定义在标签<Directory /filesystem/path/to/protected/dir>

注2:忘记提及,出于示例目的已指定RequireAny,但这是2.4.x中的默认行为,因此如果您不想,则实际上不需要指定它。

相关内容