天天看點

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

繼續閱讀