httpd 虚拟主机建立之访问机制及其日志定义

注:关闭防火墙,selinux

 

VirtualHost定义:

基于IP地址VirtualHost:

编辑httpd.conf文件:

#DocumentRoot "/web/html"                 #注释主服务配置

下面来建立虚拟主机,如果不在httpd.conf文件内编辑虚拟主机,也可以新建一个虚拟主机文件来工作.

在/etc/httpd/conf.d/下新建虚拟主机配置文件

vim /etc/httpd/conf.d/virtualhost.conf

编辑

<VirtualHost 10.140.165.169:80>    #指定iip地址和端口
        ServerName www.izyno.com    #指定主机名
        DocumentRoot "/www/izyno.com"   #指定主机根目录,需要新建.
</VirtualHost>

<VirtualHost 10.140.165.170:80>
        ServerName www.51cache.top
        DocumentRoot "/www/51cache.top"
</VirtualHost>

新建主机根目录和编辑主页文件:

[root@cnhzdhcp165169 ~]# mkdir /www/{izyno.com,51cache.top}

<h1> Welcome to my 51cache.top</h1>   #在51cache.top下新建index.html文件,添加内容;

<h1> Welcome to my izyno.com</h1>   #在izyno.com下新建index.html文件,添加内容;

指定网卡别名IP地址:

[root@cnhzdhcp165169 ~]# ip addr add 10.140.165.170/24 dev eth0

测试:

image

image

基于端口的VirtualHost:

编辑virtualhost.conf文件添加以下:

<VirtualHost 10.140.165.170:8080>     #指定端口
        ServerName www.51cache.org
        DocumentRoot "/www/51cache.org"
</VirtualHost>

在httpd.conf文件中添加监听端口:

listen 8080端口

测试:

image

基于主机名的VirtualHost:

编辑virtualhost.conf文件添加:

<VirtualHost 10.140.165.169:80>
        ServerName www.b.com
        DocumentRoot "/www/b.com"
</VirtualHost

mkdir /www/b.com

vim index.html

<h1>Welcome to my b.com site</h1>

设置windows hosts文件或者设置dns,这里设置hosts文件:

10.140.165.169 www.b.com
10.140.165.169 www.izyno.com

重启httpd服务出现如下警告:

Starting httpd: [Mon Oct 10 10:50:06 2016] [warn] VirtualHost 10.140.165.169:80 overlaps with VirtualHost 10.140.165.169:80, the first has precedence, perhaps you need a NameVirtualHost directive
[Mon Oct 10 10:50:06 2016] [warn] NameVirtualHost *:80 has no VirtualHosts

注:以上意思是说当前虚拟主机未配置基于域名的虚拟主机。指向同一条IP地址的多个文件目录基于名都会被第一条覆盖。即此时访问xxx.com或者yyy.com都会转向到xxx.com。

需要在virtualhost.conf添加: NameVirtualHost 10.140.165.169:80

测试:

image

image

完成.

 

virtualHost 访问机制定义:

定义izyno.com主机用户访问认证机制:

编辑virtualhost.conf:

<VirtualHost 10.140.165.169:80>
        ServerName www.izyno.com
        DocumentRoot "/www/izyno.com"
        <Directory "/www/izyno.com">
                Options none
                AllowOverride authconfig  #允许使用与认证授权相关的指令,他们包括AuthDBMGroupFile AuthDBMUserFile  AuthGroupFile  AuthName AuthTypeAuthUserFile和Require
                AuthType basic 
                AuthName "my site."
                AuthUserFile "/etc/httpd/.htpasswd" #指定用户认证文件,需要使用htpasswd生成.
                Require valid-user  #定义为所有认证用户可以访问
        </Directory>
</VirtualHost>

使用htpasswd生成用户: 这里创建了两个用户分别是tom 和  jory

htpasswd –c –m /etc/httpd/.htpasswd tom   #-c第一次创建时需要添加,第二次创建如果加上会覆盖掉之前的.-m,使用md5格式加密密码.

测试:

image    image

可以访问,当然也可以正对于某个用户设置或者某个组.

 

设置某个网段或者固定的IP地址的访问权限:

编辑virtualhost.conf文件添加以下:

<VirtualHost 10.140.165.170:80>
        ServerName www.51cache.top
        DocumentRoot "/www/51cache.top"
        <Directory "/www/51cache.top">
                Options none
                AllowOverride none
                Order deny,allow
                Deny from 10.140.165.93   #拒绝93IP进行访问,如果是一个网段也可以添加为:10.140.165.0/24,allow机制不再2.4中支持.
        </Directory>
</VirtualHost>

测试:

image   image

 

设置virtualhost日志记录机制:

编辑virtualhost.conf文件,添加日志记录功能:

<VirtualHost 10.140.165.169:80>
        ServerName www.izyno.com
        DocumentRoot "/www/izyno.com"
        CustomLog /var/log/httpd/izyno.com/access_log combined   #添加日志记录,需要新建izyno.com文件.
        <Directory "/www/izyno.com">
                Options none
                AllowOverride authconfig
                AuthType basic
                AuthName "my site."
                AuthUserFile "/etc/httpd/.htpasswd"
                Require valid-user
        </Directory>
</VirtualHost>

新建日志目录:

mkdir /var/log/httpd/{izyno.com,b.con,51cache.top,51cache.org} –pv

测试:

客户端访问b.com,查看b.com生成的日志:

[root@cnhzdhcp165169 b.com]# ls
access_log
[root@cnhzdhcp165169 b.com]# cat access_log
10.140.184.166 - - [10/Oct/2016:12:34:37 +0800] "GET / HTTP/1.1" 200 34 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3)"
10.140.184.166 - - [10/Oct/2016:12:34:37 +0800] "GET /favicon.ico HTTP/1.1" 404 284 "-" "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.3)"

完成日志机制.

 

设置访问的页面不存在default页面跳转:

在virtualhost.conf添加默认跳转:

<VirtualHost 10.140.165.169:80>
        ServerName _default_
        DocumentRoot "/www/default"
</VirtualHost>

新建default/index.html

测试:

image

 

Apache的日志又多又杂,如果靠分析日志或者查看服务器进程来监视Apache运行状态的话,比较繁冗。其实在Apache 1.3.2及以后的版本中就自带一个查看Apache状态的功能模块server-status

定义Location server-status 机制:

编辑httpd.conf文件取消以下的#号注释.

<Location /server-status>
    SetHandler server-status
    Order allow,deny
    Allow from 10.140.184.166
</Location>

测试:在10.140.184.166进行访问:

image

这是一个完整的server-status的配置。
第一行的ccvita-server-status表示以后可以用类似http://www.ccvita.com/ccvita-server-status来访问,同时
http://www.ccvita.com/ccvita-server-status?refresh=N将表示访问状态页面可以每N秒自动刷新一次;
Deny from表示禁止的访问地址;
Allow from表示允许的地址访问;
ExtendedStatus On表示的是待会访问的时候能看到详细的请求信息,另外该设置仅能用于全局设置,不能在特定的虚拟主机中打开或关闭。启用扩展状态信息将会导致服务器运行效率降低。

在10.140.165.93下访问:

image

完成.

posted @ 2016-10-10 13:03  quicksand.F  阅读(896)  评论(0编辑  收藏  举报