Apache默认虚拟主机及日志办法相关

  • Apache默认虚拟主机

  • 一台服务器可以访问多个网站,每个网站都是一个虚拟主机。

  • 概念:域名(主机名)、DNS、解析域名、hosts

  • 任何一个域名解析到这台机器,都可以访问的虚拟主机就是默认虚拟主机。

  • Vim /usr/local/apache2/conf/htpd.conf(搜索httpd-vhost,去掉#)(虚拟主机配置文件)

  • Vim /usr/local/apache2/conf/extra/httpd-vhosts.conf(改为如下)

  • <VirtualHost *:80>

  • ServerAdmin admin@aminglinux.com       (管理员邮箱)
    
  • 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       (访问日志)
    
  • </VirtualHost>

  • <VirtualHost *:80>

  • DocumentRoot “/data/wwwroot/www.123.com”
    
  • ServerName www.123.com
    
  • </VirtualHost>

  • /uer/local/apache2/bin/apachectl -t

  • /usr/local/apache2/bin/apachectl graceful

  • 域名跳转/域名重定向

  • 需求,把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满足条件

  • ReriteRule^/(.*)$ http://www.123.com/$1 [R=301,L]  (定义rewrite规则,当满足上面条件是,这条规则才会执行)
    
  • </IfModule>

  • </VirtualHost>

  • /usr/loacal/apache2/bin/apachectl -M|grep -i rewrite (若无该模块,需要编辑配置文件httpd.conf ,删除rewrite_module(shared)前面的 #)

  • curl -x127.0.0.1:80 -l 123.com(状态吗为301)(301位永久重定向,302位临时重定向,404页面不存在,200验证成功)

  • Apache访问日志

  • 访问日志记录用户的每一个请求

  • Vim /usr/local/apache2.4/conf/httpd.conf (搜索LogFormat)

  • LogFormat “%h %l %u %t \”%r” %>s %b \”%{Referer}o\i\”\”%{User-Agent}i\””combined (备选的一种更详细的日志格式)(referer为用户操作历史)(agent用户代理)

  • LogFormat “%h %i %u %t \”%r\” %>s %b” common (默认的日志格式)

  • 把虚拟主机配置文件改为如下:

  • <VirtualHost *:80>

  • DocumentRoot “/data/wwwroot/www.123.com”
    
  • ServerName www.123.com
    
  • ServerAlias 123.com
    
  • CustomLog “logs/123.com-access_log” combined
    
  • </VirtualHost>

  • 重新加载配置文件 -t,graceful

  • curl -x127.0.0.1:80 -l 123.com

  • tail /usr/local/apache2.4/logs/123.com-access_log

  • 访问日志不记录静态文件

  • 网站大多元素为静态文件,如图片、css、js等,这些元素可以不用记录

  • 把虚拟主机配置文件改成如下

  • <VirualHost *:80>

  • DocumentRoot “/data/wwwroot/www.123.com”
    
  • ServerName www.123.com
    
  • ServerAlias 123.com
    
  • SetEnvlf Reauest_URL “.*\.gif$” img
    
  • SetEnvlf Reauest_URL “.*\.jpg$” img
    
  • SetEnvlf Reauest_URL “.*\.png$” img
    
  • SetEnvlf Reauest_URL “.*\.bmp$” img
    
  • SetEnvlf Reauest_URL “.*\.swf$” img
    
  • SetEnvlf Reauest_URL “.*\.js$” img
    
  • SetEnvlf Reauest_URL “.*\.css$” img
    
  • CustomLog “logs/123.com-access_log” combined env=!img
    
  • </VirualHost>

  • 重新加载配置文件 -t,graceful

  • Mkdir /data/wwwroot/www.123.com/images (创建目录,并在目录下上传一个图片)

  • curl -x127.0.0.1:80 -l 123.com/images/123.jpg

  • tail /usr/local/apache2.4/logs/123.com-access_log

  • 访问日志切割

  • 日志移植记录总有一天会把整个磁盘占满,所有有必要让它自动切割,并删除老的日志文件。

  • 把虚拟主机配置文件改成如下:

  • <VirtualHost *:80>

  • DocumentRoot “/data/wwwroot/www.123.com”
    
  • ServerName www.123.com
    
  • ServerAlias 123.com
    
  • SetEnvIf Reauest_URI “.*\.gif$” img
    
  • SetEnvIf Reauest_URI “.*\.jpg$” img
    
  • SetEnvIf Reauest_URI “.*\.png$” img
    
  • SetEnvIf Reauest_URI “.*\.bmp$” img
    
  • SetEnvIf Reauest_URI “.*\.swf$” img
    
  • SetEnvIf Reauest_URI “.*\.js$” img
    
  • SetEnvIf Reauest_URI “.*\.css$” img
    
  • CustomLog “|/usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d. log 86400” combined eny=!img
    
  • </VirtualHost>

  • 重新加载配置文件 -t,graceful

  • ls /usr/local/apache2.4/logs

  • 配置静态元素过期时间

  • 浏览器访问网站的图片是会把静态的文件缓存在本地的电脑里,这样下次再访问时就不用再去远程下载了。

  • 增加配置

  • <IfModule mod_expires.c>

  • 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”
    
  • </IfModule>

  • 需要expires_module

  • curl测试,看cache-control: max-age

  • -------------------------(以下为Apache的另一种安装方法)

  • 下载apr-1.6、apr-util-1.6以及httpd-2.4,分别解压三个源码包

  • 把apr-1.6.3 放到httpd源码包的/srclib/下,改名apr

  • 把apr-util-1.6.1 放到httpd源码包的/srclib/下,改名apr-util4)编译参数./configure --prefix=/dir/ --enable-so --enable-mpms-shared=all --with-mpm=event --enable-mods-shared=most --with-included-apr

  • 说明:这里的/dir/为apache安装路径,根据需求定目录

  • apache的一些学习文档: https://github.com/aminglinux/apache

转载于:https://my.oschina.net/u/4095969/blog/3048406

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值