使用Apache .htaccess 进行访问保护

Apache Server下,能够对单个目录进行访问控制,如ip过滤,要求用户名密码进行Basic Auth等。下面使用 .htaccess来实现Auth.apache

首先在配置文件 http.conf里,对所要控制的目录,开启Override功能,若是在这里设置为AllowOverride none, .htaccess文件将不起做用。浏览器

<Directory "/your/site/dir/need/to/control/">
    Options FollowSymLinks Multiviews
    MultiviewsMatch any
    AllowOverride all
    Require all granted
</Directory>

上面的AllowOverride能够选 FileInfo, AuthConfig, Limit, all, none.iview

配置文件保存后,重启server. 我在mac os x Yosemite下执行ide

sudo apachectl restart

Apache的配置文件到此完成,如今在对应的site目录下建立.htaccess文件,如上面的/your/site/dir/need/to/control/.htaccess,内容以下ui

AuthName 'name'
AuthUserFile /your/site/dir/need/to/control/.htpasswd
AuthType Basic
require valid-user

上面的name为用户login时的用户名,AuthUserFile为存放password的文件。下面讲如何生成.htpasswd文件:spa

在console(mac os x Yosemite)下使用htpasswd命令生成.htpasswd文件rest

htpasswd -bc /your/site/dir/need/to/control/.htpasswd name password

如今在浏览器访问 http://host/your/.../control/,会弹出对话框,输入name, password便可。code

相关文章
相关标签/搜索