Apache用户认证

为了使 WEB服务器更安全,需要将一些特定用户才能访问的目录设置用户认证,例如:网站后台登陆地址。

1、给网站后台目录做一个虚拟目录(并设置权限,这样更安全,但不是必须的)

用vim打开/usr/local/apache2/conf/extra/httpd-vhosts.conf文件

<VirtualHost*:80>

<Directory/data/www/abc/>

        AllowoVerride Authconfig

        Authname "ni hao xian sheng"

        AuthType Basic

        AuthUserfile /data/.htpasswd

        Require valid-user

</Directory>

</VirtualHost>

然后从新加载一下apache就可以了

1.##这里设置的目录为真实目录,并非虚拟目录

2.  AllowOverride AuthConfig为允许认证

3.  AuthType认证类型 Basic 由 mod_auth 提供

4.  AuthName这里定义的内容将在 web 弹出的登陆框中显示

5.  AuthUserFile定义认证文件路径 ,不要放在可能被下载到的地方

6.  Require user定义允许访问的用户

配置完这一步接下来建立验证文件

htpsswd -c /data/.htpasswd malong

使用 apache 自带的 htpasswd-c 指令生成验证文件

  创建第二个用户的时候不要加 -c 选项,否则会清空之前文件中的内容

New password:

Re-type new password:


2、配置默认虚拟主机(apache默认将第一虚拟主机作为默认主机)

用vim打开/usr/local/apache2/conf/extra/httpd-vhosts.conf

<VirtualHost*:80>

    ServerAdminwebmaster@dummy-host.example.com

    DocumentRoot "/tmp/123"(需要先创建/tmp/123放止出错)

    ServerName www.cai.com

    ServerAlias www.caibingsen.com

  # ErrorLog "logs/dummy-host.example.com-error_log"

#   CustomLog"logs/dummy-host.example.com-access_log" common

</VirtualHost>

 然后在降低文件的的权限 chmod 600/tmp/123这样就不会访问到的,

其目的是为了安全、只允许指定的域名访问

 3、域名重定向或者301跳转

需求:比如公司以前的域名是xxx.com,现在公司换域名了,新域名是yyy.com

那么为了让以前老域名的权重能传递给新域名,我们一般是做301设置

 用vim/usr/local/apache2/conf/extra/httpd-vhosts.conf中进行配置


<VirtualHost *:80>

   DocumentRoot "/data/www"

    ServerName www.cbs.com

    ServerAlias www.caibingsen.com

<IfModule mod_rewrite.c> #域名跳转模块或者301重定向(没有这个模块的时候用apxs工具)

     RewriteEngine on

     RewriteCond %{HTTP_HOST}^www.cbs.com$#需要跳转的地址(源)

     RewriteRule ^/ (.*)$ http://www.caibingsen.com/$1 [R=301,L] #要被指向的地址(目的)

</IfModule>

  # ErrorLog "logs/dummy-host.example.com-error_log"

 #  CustomLog "logs/dummy-host.example.com-access_log" common

 </VirtualHost>

 多域名时在条件后面加[OR]或者的意思。不写时则表示两个要同时满足才能跳转。

实现域名跳转或者301跳转,是为了让域名在搜索引擎上排名跟更靠前。

301是永久重定向。

302是临时重定向

L:表示结束