首页 > 开发 > Apache > 正文

Apache设置禁止访问网站目录,目录列表显示文件

2020-10-12 18:48:27
字体:
来源:转载
供稿:网友

默认apache在当前目录下没有index.html入口就会显示目录,让目录暴露在外面是非常危险的事,如下操作禁止apache显示目录,希望文章对各位有帮助.

进入apache的配置文件 httpd.conf 找到如下代码:

Options Indexes FollowSymLinks 修改为:Options FollowSymLinks

其实就是将Indexes去掉,Indexes表示若当前目录没有index.html就会显示目录结构.

1. 禁止访问某些文件/目录,增加Files选项来控制,比如要不允许访问 .inc 扩展名的文件,保护php类库,代码如下:

  1. <Files ~ ".inc$"
  2. Order allow,deny 
  3. Deny from all 
  4. </Files> 
 

禁止访问某些指定的目录,可以用 <DirectoryMatch> 来进行正则匹配,代码如下:

  1. <Directory ~ "^/var/www/(.+/)*[0-9]{3}"
  2. Order allow,deny 
  3. Deny from all 
  4. </Directory> 

通过文件匹配来进行禁止,比如禁止所有针对图片的访问,代码如下:

  1. <FilesMatch .(?i:gif|jpe?g|png)$> 
  2. Order allow,deny 
  3. Deny from all 
  4. </FilesMatch> 

针对URL相对路径的禁止访问,代码如下:

  1. <Location /dir/> 
  2. Order allow,deny 
  3. Deny from all 
  4. </Location> 

配置示例,代码如下:

  1. <Directory "E:/Program Files/Apache Software Foundation/Apache2.2/htdocs"
  2. # Possible values for the Options directive are "None""All"
  3. or any combination of: 
  4. # Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews 
  5. # Note that "MultiViews" must be named *explicitly* --- "Options All" 
  6. # doesn't give it to you. 
  7. # The Options directive is both complicated and important. Please see 
  8. # http://httpd.apache.org/docs/2.2/mod/core.html#options 
  9. for more information. 
  10. # 就是这一行,只去掉indexes也可 
  11. #Options Indexes FollowSymLinks 
  12. Options FollowSymLinks 
  13. # AllowOverride controls what directives may be placed in .htaccess files. 
  14. # It can be "All""None"or any combination of the keywords: 
  15. # Options FileInfo AuthConfig Limit 
  16. AllowOverride None 
  17. # Controls who can get stuff from this server. 
  18. Order allow,deny 
  19. Allow from all 
  20. </Directory> 
  21. //开源代码Cuoxin.com 

建议默认情况下,设置APACHE禁止用户浏览目录内容.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表