天天看点

apache http的<directory></directory>语句,允许被访问

原帖:http://blog.sina.com.cn/s/blog_6151984a0100f1tj.html

   如何访问根目录下的目录http://192.168.1.12/test/

  • 第一.缺省apache不允许访问http目录(没有<Directory >定义,就没有访问权限)

访问目录http://192.168.1.12/test/

会显示:

Forbidden

You don't have permission to access /test/ on this server.

  • 第二.无限制目录访问

在httpd.conf中增加<Directory >定义,即可打开无限制的目录访问权限

<Directory /home/macg/www/test>

    Options All

    AllowOverride all

</Directory>

再访问会显示如下:

Apache/2.0.54 (Fedora) Server at 192.168.1.12 Port 80

Index of /test

 Name                    Last modified      Size  Description

 --------------------------------------------------------------------------------

 Parent Directory                             -   

 bg0073.jpg              29-Nov-2006 21:02   36K  

 bg0135.jpg              29-Nov-2006 21:03   41K  

 bg0137.jpg              29-Nov-2006 21:03   47K  

 slade1.html             29-Nov-2006 22:02  1.2K  

 slade2.html             29-Nov-2006 22:02  1.1K  

 slade3.html             29-Nov-2006 22:02  1.4K  

 slade4.html             29-Nov-2006 22:02  1.8K  

 slade5.html             29-Nov-2006 22:02  2.3K  

--------------------------------------------------------------------------------

Apache/2.0.54 (Fedora) Server at 192.168.1.12 Port 80

实际AllowOverride all是enable   .htaccess目录限制功能。

但test目录下并没有.htaccess文件

等于开放访问,无限制 。

  • 第三.有限制目录访问

将其他目录中的.htaccess拷贝入要限制访问的目录

[[email protected] test]# ls -a

.  ..  bg0073.jpg  bg0135.jpg  bg0137.jpg  slade1.html  slade2.html  slade3.html  slade4.html  slade5.html

[[email protected] test]# cp ../test1/.htaccess .

[[email protected] test]# ls -a

.  ..  bg0073.jpg  bg0135.jpg  bg0137.jpg  .htaccess  slade1.html  slade2.html  slade3.html  slade4.html slade5.html

[[email protected] test]# more .htaccess

authType Basic

AuthName "Restricted Files"

AuthUserFile /etc/httpd/passwords

Require valid-user

再访问http://192.168.1.12/test/

会跳出身份认证窗口,输入用户名密码,即可访问目录

      .htaccess 目录限制的配置

  •     要使用.htaccess文件,先在将httpd.conf中建立<Directory ></Directory>

<Directory "/home/macg/www/test">   

        Options All            

允许对目录的操作,ALL---所有操作

    AllowOverride all                      

AllowOverride all----允许.htaccess所有指令,缺省是all

AllowOverride None ----完全忽略.htaccess文件

</Directory>

  •     用/usr/bin/htpasswd创建一个用于认证的密码文件。

并且这个文件不应该置于DocumentRoot目录下,以避免被下载。

建议创建在/etc/httpd/目录下:

[[email protected] httpd]# /usr/bin/htpasswd -c /etc/httpd/passwords macg

                                           -c建立文件

New password:

Re-type new password:

Adding password for user macg

[[email protected] httpd]# /usr/bin/htpasswd /etc/httpd/passwords gary  

                            没有-c就是单纯的adduser 追加用户

New password:

Re-type new password:

Adding password for user gary

[[email protected] httpd]# more /etc/httpd/passwords

macg:U8jCwSsZyAB2g

gary:06yCDyg7AijlM

  •   在受限制目录下建立.htaccess文件

[[email protected] test]# ls -a

.  ..  bg0073.jpg  bg0135.jpg  bg0137.jpg  .htaccess  slade1.html  slade2.html  slade3.html  slade4.html slade5.html

[[email protected] test]# more .htaccess

authType Basic

authType--------认证类型

     由mod_auth_basic提供的Basic

Basic认证方法并不加密来自用户浏览器的密码(明文传输)

     更安全的认证方法"AuthType Digest",即由mod_auth_digest供的摘要认证

最新的浏览器版本才支持MD5认证

(认证,服务器响应速度会受一些影响,一般有几百个用户就会对响应速度有非常明显的影响)

AuthName "Restricted Files"

AuthName "会员区"

此句是显示给用户看的

AuthUserFile /etc/httpd/passwords

此目录接受passwords内定义用户的认证请求

or

Require macg

此目录只接受单一用户macg(unix用户)认证请求

        <Directory ></Directory> 中指令的含义

<Directory "/home/macg/www/test">   

        Options All            

        AllowOverride all                      

</Directory>

   Options指令-------目录的访问特性

option  none    禁止对目录的所有操作

option all      允许对目录的所有操作,ALL---所有操作

option ExecCGI    对该目录,可以执行cgi脚本

option Indexes    允许访问该目录(而该目录没有index.html)时,返回目录下的文件列表                        

option FollowSymLinks       只允许对目录的FollowSymLinks操作

     AllowOverride指令

None    不读取.htaccess

all    all----允许.htaccess所有指令,缺省是all

Limit    .htaccess函盖具体限定的主机(allow,deny)

AuthConfig    .htaccess函盖跟认证有关指令(AuthType,AuthName) 

   <Directory ></Directory> 对下面的所有子目录也生效

      所以httpd.conf中先对根目录/进行配置,等于是设置缺省配置

httpd.conf中先对根目录/进行配置,等于是设置缺省配置

<Directory />                                  

    Options FollowSymLinks  禁止对目录的访问(option只允许对目录的FollowSymLinks操作)

    AllowOverride None      不读取.htaccess

    Order deny,allow        deny all

    Deny from all               

</Directory>

<Directory "/usr/local/apache2/htdocs">

    Options Indexes FollowSymLinks   只允许访问index和连接   

    AllowOverride None

    Order allow,deny       承接父目录(/)的deny all,这里也是deny all

    Allow from all           

</Directory>

        order allow deny -------------httpd.conf中封IP之类的操作

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       典型的封网段

继续阅读