2018-09-12

11.16/11.17 Apache默认虚拟主机
[root@localhost ~]# vim /usr/local/apache2/conf/httpd.conf 
#Include conf/extra/httpd-vhosts.conf //去掉#
[root@localhost ~]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf //改为如下

    ServerAdmin [email protected]
    DocumentRoot "/data/wwwroot/aming.com"
    ServerName aming.com
    ServerAlias www.aming.com
    ErrorLog "logs/aming.com-error_log"
    CustomLog "logs/aming.com-access_log" common



    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName 123.com
    ErrorLog "logs/123.com-error_log"
    CustomLog "logs/123.com-access_log" common

[root@localhost ~]# /usr/local/apache2/bin/apachectl –t
[root@localhost ~]# /usr/local/apache2/bin/apachectl graceful
[root@localhost ~]# curl -x192.168.31.10:80 aming.com
aming.com
[root@localhost ~]# curl -x192.168.31.10:80 123.com
123.com
11.18 Apache用户认证
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那个虚拟主机编辑成如下内容

    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName 123.com
     //指定认证的目录
        AllowOverride AuthConfig //这个相当于打开认证的开关
        AuthName "123.com user auth" //自定义认证的名字,作用不大
        AuthType Basic //认证的类型,一般为Basic,其他类型没用过
        AuthUserFile /data/.htpasswd  //指定密码文件所在位置
        require valid-user //指定需要认证的用户为全部可用用户
    

[root@localhost ~]# /usr/local/apache2.4/bin/htpasswd -cm /data/.htpasswd aming 

单个文件认证

[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName www.123.com
      //此处指定文件
        AllowOverride AuthConfig
        AuthName "123.com user auth"
        AuthType Basic
        AuthUserFile /data/.htpasswd
        require valid-user
    
11.19/11.20 域名跳转
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName www.123.com
    ServerAlias 123.com
     //需要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规则,当满足上面的条件时,这条规则才会执行

 
[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf
#LoadModule rewrite_module modules/mod_rewrite.so //去掉#
11.21 Apache访问日志
[root@localhost ~]# 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 
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
 
    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName www.123.com
    ServerAlias 123.com
    CustomLog "logs/123.com-access_log" combined

[root@localhost ~]# curl -x127.0.0.1:80 -I 123.com 
[root@localhost ~]# tail /usr/local/apache2.4/logs/123.com-access_log 
11.22 访问日志不记录静态文件
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName www.123.com
    ServerAlias 123.com
    SetEnvIf Request_URI ".*\.gif$" img
    SetEnvIf Request_URI ".*\.jpg$" img
    SetEnvIf Request_URI ".*\.png$" img
    SetEnvIf Request_URI ".*\.bmp$" img
    SetEnvIf Request_URI ".*\.swf$" img
    SetEnvIf Request_URI ".*\.js$" img
    SetEnvIf Request_URI ".*\.css$" img 
    CustomLog "logs/123.com-access_log" combined env=!img

[root@localhost ~]# mkdir /data/wwwroot/www.123.com/images //创建目录,并在这目录下上传一个图片
[root@localhost ~]# curl -x127.0.0.1:80 -I 123.com/images/123.jpg 
[root@localhost ~]# tail /usr/local/apache2.4/logs/123.com-access_log 
11.23 访问日志切割
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

    DocumentRoot "/data/wwwroot/www.123.com"
    ServerName www.123.com
    ServerAlias 123.com
   SetEnvIf Request_URI ".*\.gif$" img
    SetEnvIf Request_URI ".*\.jpg$" img
    SetEnvIf Request_URI ".*\.png$" img
    SetEnvIf Request_URI ".*\.bmp$" img
    SetEnvIf Request_URI ".*\.swf$" img
    SetEnvIf Request_URI ".*\.js$" img
    SetEnvIf Request_URI ".*\.css$" img 
    CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img

[root@localhost ~]# ls /usr/local/apache2.4/logs 
11.24 静态元素过期时间
[root@localhost ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

    ExpiresActive on  //打开该功能的开关
    ExpiresByType image/gif  "access plus 1 days"
    ExpiresByType image/jpeg "access plus 24 hours"
    ExpiresByType image/png "access plus 24 hours"
    ExpiresByType text/css "now plus 2 hour"
    ExpiresByType application/x-javascript "now plus 2 hours"
    ExpiresByType application/javascript "now plus 2 hours"
    ExpiresByType application/x-shockwave-flash "now plus 2 hours"
    ExpiresDefault "now plus 0 min"

[root@localhost ~]# vim /usr/local/apache2.4/conf/httpd.conf
#LoadModule expires_module modules/mod_expires.so //去掉#

扩展
apache虚拟主机开启php的短标签 http://ask.apelearn.com/question/5370
apache日志记录代理IP以及真实客户端IP http://ask.apelearn.com/question/960
apache只记录指定URI的日志 http://ask.apelearn.com/question/981
apache日志记录客户端请求的域名 http://ask.apelearn.com/question/1037
apache 日志切割问题 http://ask.apelearn.com/question/566

转载于:https://www.cnblogs.com/2KP2/p/9639098.html

你可能感兴趣的:(2018-09-12)