Apache Basic Auth 在 .htaccess 或 Directory 块中不起作用;在位置块中工作正常

Apache Basic Auth not working in .htaccess or Directory blocks; works fine in Location blocks

好的,运行 Ubuntu 14.04 上的 Apache 2.4.7,全新安装。

如果我将以下内容添加到 /etc/apache2/sites-available 中的 .conf 文件中,则基本身份验证可以正常工作:

<Location /test>
  AuthType Basic
  AuthName ".htaccess Basic Auth Test"
  AuthUserFile "/var/www/passwords/test"
  Require valid-user
</Location>

但是,如果我在相关目录的.htaccess文件中只有以下内容:

AuthType Basic
AuthName ".htaccess Basic Auth Test"
AuthUserFile "/var/www/passwords/test"
Require valid-user

授权似乎被忽略了;没有提示,页面就像不存在一样。目录块也会发生同样的情况:

<Directory /var/www/default/test>
  AuthType Basic
  AuthName ".htaccess Basic Auth Test"
  AuthUserFile "/var/www/passwords/test"
  Require valid-user
</Directory>

所以,我检查了 .htaccess 文件是否真的被读取了,它们确实被读取了;添加

Options -Indexes

.htaccess 文件确实删除了目录中的索引列表。

我打开了调试功能,但这并不是特别有用:

[Tue Apr 21 13:04:14.082275 2015] [authz_core:debug] [pid 8914] mod_authz_core.c(721): [client 144.32.48.10:64109] AH01625: authorization result of <RequireAny>: granted (directive limited to other methods)
[Tue Apr 21 13:04:14.082538 2015] [authz_core:debug] [pid 8914] mod_authz_core.c(721): [client 144.32.48.10:64109] AH01625: authorization result of <RequireAny>: granted (directive limited to other methods)

/etc/apache2 中进行 Grep 显示没有任何限制;没有其他 Auth 语句,AllowOverride All 在正确的位置(如选项测试所示;他们说 All,没有任何其他可能限制覆盖的内容)。

我觉得我一定遗漏了一些明显的东西。这是加载的模块:

root@linode:/etc/apache2# apachectl -M
Loaded Modules:
 core_module (static)
 so_module (static)
 watchdog_module (static)
 http_module (static)
 log_config_module (static)
 logio_module (static)
 version_module (static)
 unixd_module (static)
 alias_module (shared)
 auth_basic_module (shared)
 authn_core_module (shared)
 authn_file_module (shared)
 authz_core_module (shared)
 authz_host_module (shared)
 authz_user_module (shared)
 autoindex_module (shared)
 deflate_module (shared)
 dir_module (shared)
 env_module (shared)
 filter_module (shared)
 headers_module (shared)
 mime_module (shared)
 mpm_prefork_module (shared)
 negotiation_module (shared)
 php5_module (shared)
 proxy_module (shared)
 proxy_http_module (shared)
 rewrite_module (shared)
 setenvif_module (shared)
 status_module (shared)
 substitute_module (shared)
 xml2enc_module (shared)

我知道我做了一些有趣的事情。我的服务器正在接受代理测试,因此网站建议我将其添加到 apache2.conf:

<Location />
   <Limit CONNECT>
     Require all denied
   </Limit>
</Location>

删除了它(一时兴起 - 看起来不会改变它)并且基本身份验证再次起作用!

为了保护站点,我将块移动到默认(仅 IP 地址)虚拟主机,因此其他的都可以正常运行。

另一个解决方案是安装 WP htaccess Control plugin 并在 Custom htaccess 中添加此行:

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=