使用Apache从外部IP访问时,密码保护目录

时间:2012-03-27 04:33:47

标签: apache passwords directory local

目前我的主目录和子目录都有密码保护,但是我只想在从本地子网连接时从外部IP地址和密码免费连接时才需要它。

目前/etc/apache2/sites-available/default看起来像这样:

    <VirtualHost *:80>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride None
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

    <Location / >
       AuthType Digest
       AuthName "intranet"
       AuthDigestDomain /var/www/ http://10.1.2.2

       AuthDigestProvider file
       AuthUserFile /etc/apache2/passwords
       Require user user1
       SetEnv R_ENV "/var/www"
   </Location>

   <Location /dir1>
        AuthType Digest
        AuthName "dir"
        AuthDigestDomain /var/www/dir1/ http://10.1.2.2/dir1

        AuthDigestProvider file
        AuthUserFile /etc/apache2/passwords
        Require user user2
        SetEnv R_ENV "/var/www/dir1"
    </Location>

    <Location /dir2>
        AuthType Digest
        AuthName "dir"
        AuthDigestDomain /var/www/ http://10.1.2.2/dir2

        AuthDigestProvider file
        AuthUserFile /etc/apache2/passwords
        Require user user2
        SetEnv R_ENV "/var/www/dir2"
    </Location>

</VirtualHost>

我在Apache's documentation on auth有一个战利品,但无法理解我是如何实现密码保护的。

1 个答案:

答案 0 :(得分:1)

进行了一些搜索,提出了http://www.askapache.com/htaccess/apache-authentication-in-htaccess.html

基本上改变了这个:

<Location / >
   AuthType Digest
   AuthName "intranet"
   AuthDigestDomain /var/www/ http://10.1.2.2

   AuthDigestProvider file
   AuthUserFile /etc/apache2/passwords
   Require user user1
   SetEnv R_ENV "/var/www"
</Location>

到此:

<Location />
    Order deny,allow
    Deny from all
    AuthType Digest
    AuthName "intranet"
    AuthDigestDomain /var/www/ http://10.1.2.2

    AuthDigestProvider file
    AuthUserFile /etc/apache2/passwords
    Require valid-user
    SetEnv R_ENV "/var/www"
    Allow from 10.1.2.0/24
    Satisfy Any
</Location>

经过测试,一切顺利进行。