vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //编辑111.com虚拟主机
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
<Directory /data/wwwroot/111.com> #//指定认证的目录
AllowOverride AuthConfig #//这个相当于打开认证的开关
AuthName "111.com user auth" #//自定义认证的名字,作用不大
AuthType Basic #//认证的类型,一般为Basic,其他类型阿铭没用过
AuthUserFile /data/.htpasswd #//指定密码文件所在位置
require valid-user #//指定需要认证的用户为全部可用用户
</Directory>
</VirtualHost>
/usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd aming //创建/data/.htpasswd密码文件,并加入用户aming,设置密码为MD5加密。
[root@aminglinux-02 ~]# /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd aming
New password:
Re-type new password:
Adding password for user aming
[root@aminglinux-02 ~]# cat /data/.htpasswd
aming:$apr1$IcrnKsgm$peDVR4iChx.0E9/zaD.HK.
测试
[root@aminglinux-02 ~]# curl -x127.0.0.1:80 -uaming:123456 111.com
111.com[root@aminglinux-02 ~]# curl -x127.0.0.1:80 111.com -I
HTTP/1.1 401 Unauthorized ##401表示这个页面需要用户认证
Date: Mon, 31 Jul 2017 15:03:56 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1
[root@aminglinux-02 ~]# curl -x127.0.0.1:80 -uaming:123456 111.com
111.com[root@aminglinux-02 ~]#
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
<FilesMatch admin.php> #//对admin.php页面进行认证
AllowOverride AuthConfig
AuthName "123.com user auth"
AuthType Basic
AuthUserFile /data/.htpasswd
require valid-user
</FilesMatch>
</VirtualHost>
[root@aminglinux-02 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aminglinux-02 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@aminglinux-02 ~]# curl -x127.0.0.1:80 111.com/admin.php -I
HTTP/1.1 401 Unauthorized
Date: Mon, 31 Jul 2017 15:26:10 GMT
Server: Apache/2.4.27 (Unix) PHP/7.1.6
WWW-Authenticate: Basic realm="111.com user auth"
Content-Type: text/html; charset=iso-8859-1
[root@aminglinux-02 ~]# curl -x127.0.0.1:80 -uaming:123456 111.com/admin.php
admin.php[root@aminglinux-02 ~]#
需求,把123.com域名跳转到www.123.com,配置如下:
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
<IfModule mod_rewrite.c> //需要mod_rewrite模块支持
RewriteEngine on //打开rewrite功能
RewriteCond %{HTTP_HOST} !^www.123.com$ //定义rewrite的条件,主机名(域名)不是www.123.com满足条件
RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L] //定义rewrite规则,当满足上面的条件时,这条规则才会执行
</IfModule>
</VirtualHost>
vim /usr/local/apache2.4/conf/httpd.conf //搜索LogFormat
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
默认是common,改为combined,h来源ip,u用户,t是时间,r行为,s是状态码,b是大小,Referer是进入当前页面得上一个页面得地址,User-Agent用户代理
即用户使用的浏览器,curl命令等
把虚拟主机配置文件改成如下:
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/www.123.com"
ServerName www.123.com
ServerAlias 123.com
CustomLog "logs/123.com-access_log" combined
</VirtualHost>
原文地址:http://blog.51cto.com/akui2521/2121886