原帖:http://blog.sina.com.cn/s/blog_6151984a0100f1tj.html
- 第一.缺省apache不允许访问http目录(没有<Directory >定义,就没有访问权限)
访问目录http://192.168.1.12/test/ 会显示: Forbidden You don't have permission to access /test/ on this server. |
- 第二.无限制目录访问
<Directory /home/macg/www/test> </Directory> |
Apache/2.0.54 (Fedora) Server at 192.168.1.12 Port 80 Index of /test -------------------------------------------------------------------------------- Apache/2.0.54 (Fedora) Server at 192.168.1.12 Port 80 |
实际AllowOverride all是enable
但test目录下并没有.htaccess文件
等于开放访问,无限制 。
- 第三.有限制目录访问
[root@localhost test]# ls -a . |
[root@localhost test]# cp ../test1/.htaccess . |
[root@localhost test]# ls -a . |
[root@localhost test]# more .htaccess authType Basic AuthName "Restricted Files" AuthUserFile /etc/httpd/passwords Require valid-user |
会跳出身份认证窗口,输入用户名密码,即可访问目录
-
要使用.htaccess文件,先在将httpd.conf中建立<Directory ></Directory>
<Directory "/home/macg/www/test"> 允许对目录的操作,ALL---所有操作 AllowOverride all----允许.htaccess所有指令,缺省是all AllowOverride None ----完全忽略.htaccess文件 </Directory> |
-
用/usr/bin/htpasswd创建一个用于认证的密码文件。
建议创建在/etc/httpd/目录下:
[root@localhost httpd]# /usr/bin/htpasswd -c /etc/httpd/passwords macg New password: Re-type new password: Adding password for user macg |
[root@localhost httpd]# /usr/bin/htpasswd /etc/httpd/passwords gary New password: Re-type new password: Adding password for user gary |
[root@localhost httpd]# more /etc/httpd/passwords macg:U8jCwSsZyAB2g gary:06yCDyg7AijlM |
-
在受限制目录下建立.htaccess文件
[root@localhost test]# ls -a . |
[root@localhost test]# more .htaccess authType Basic authType--------认证类型 Basic认证方法并不加密来自用户浏览器的密码(明文传输) 最新的浏览器版本才支持MD5认证 (认证,服务器响应速度会受一些影响,一般有几百个用户就会对响应速度有非常明显的影响) AuthName "Restricted Files" AuthName "会员区" 此句是显示给用户看的 AuthUserFile /etc/httpd/passwords 此目录接受passwords内定义用户的认证请求 or Require macg 此目录只接受单一用户macg(unix用户)认证请求 |
<Directory "/home/macg/www/test"> </Directory> |
option option all option ExecCGI option Indexes option FollowSymLinks |
None all Limit AuthConfig |
httpd.conf中先对根目录/进行配置,等于是设置缺省配置 <Directory /> </Directory> <Directory "/usr/local/apache2/htdocs"> </Directory> |
Apache模块 mod_authz_host |
<Directory /docroot> Order Deny,Allow Deny from ... Allow from ... </Directory> 除了后面allow的,其他都deny |
典型的封IP Order Allow,Deny Allow from all Deny from 205.252.46.165 注意顺序: 除了后面deny的,其他都allow Deny from 192.168.2 |