You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Apache2仅保护父目录不保护子目录的配置问题

解决Apache2父目录密码验证、子目录免密访问的问题

你的问题出在使用了Apache 2.2的过时指令,Debian 12搭载的Apache 2.4已经不再支持Satisfy、Order这类语法,导致子目录的免密规则无法覆盖父目录的验证要求。

修正后的配置文件

直接替换/etc/apache2/sites-available/000-default.conf里的子目录配置块,完整配置如下:

<VirtualHost *:80>
         ServerAdmin webmaster@localhost
         DocumentRoot /var/www/html
 
         ErrorLog ${APACHE_LOG_DIR}/error.log
         CustomLog ${APACHE_LOG_DIR}/access.log combined
 
         <Directory "/var/www/html/dir1">
                 AuthType Basic
                 AuthName "Only for members"
                 AuthUserFile /etc/apache2/.htpasswd
                 Require valid-user
         </Directory>
         <Directory "/var/www/html/dir1/subdir1">
                 # 取消当前目录的认证要求
                 AuthType None
                 # 允许所有用户访问
                 Require all granted
                 # 禁止.htaccess文件覆盖当前配置
                 AllowOverride None
         </Directory> 
</VirtualHost>

验证并重启服务

  1. 先检查配置语法是否正确:
sudo apache2ctl configtest
  1. 如果输出Syntax OK,重启Apache服务生效:
sudo systemctl restart apache2

这样修改后,/var/www/html/dir1会保持密码验证,而其子目录subdir1将允许所有人免密访问。

内容的提问来源于stack exchange,提问作者T0xxic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 01:39:55