web服务器【apache/nginx] 关闭目录的浏览权限

时间:2021-11-08 12:25:36

1. apache 默认开放ServerRoot 目录的流量权限

在用户进行访问http://localhost/serverroot/时会显示一些文件在外面:

Index of /dir_test

Parent Directory
aliws.pinpai.txt
aliws.txt
aliws.xinghao.txt
aliws_demo.php
pinpai.txt
 

  • 关闭目方法一

<Directory "/home/xxxx/htdocs">

Options Indexes FollowSymLinks #默认就会把/home/xxx/htdocs的目录暴漏出来;关闭方法: Options -Indexes FollowSymLinks (-Indexes 表示相反)
#或者直接把 Options的功能给关闭: Options None
 </Directory>

  • 关闭方法二

 <Directory "/home/xxxx/htdocs">
Options Indexes #------------>Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>

只需要修改Options Indexes为Options None即可,注:根据PHP运行环境安装包的不同,Options Indexes也有可能是Options Indexes FollowSymLinks,一并改为Options None即可。

2. nginx 关闭目录的方法

#增加一个目录(相对路径)的location,直接禁止访问
location /htdocs/ {
deny all;
}

#最好把403 转到一个友好的页面
#error_page  403
        error_page   403 http://www.baidu.com/home/error.php;