apache的优化-日志轮询、错误页面重定向、压缩功能deflate、客户端缓存expire

1.apache日志轮询

1.1)什么是日志轮询

默认情况下apache的日志是写入到一个文件中的,这对日志的备份和分析造成不便。日志轮询就是可以把apache的日志根据时间进行分开,例如按天轮询:即apache会把当天的日志写入到一个独立的文件中。

1.2)下载并安装日志轮询工具

wget http://cronolog.org/download/cronolog-1.6.2.tar.gz
tarzxf cronolog-1.6.2.tar.gz 
cdcronolog-1.6.2
./configure
make 
make install

1.3)配置轮询

<VirtualHost *:80>

DocumentRoot"/usr/local/httpd-2.2.9/htdocs/sr1/"

ServerName www.sr1.com

ServerAlias www.sr1.com

ErrorLog "logs/www.sr1.com_error_log"

# CustomLog "logs/www.sr1.com_access_log" jie

CustomLog"|/usr/local/sbin/cronolog /usr/local/httpd/logs/sr1_%Y%m%d_access_log"jie

</VirtualHost>

注意日志的文件要写绝对路径

%Y%m%d是按天轮询

%Y%m%d%H是按小时轮询

查看结果:可以发现在logs下sr1的日志会按天来创建

2.错误页面显示

当服务器出现问题后,会返回给用户客户端错误代码,现在可以设置,根据错误代码把用户的请求重定向到另一个页面,达到友好显示的效果

步骤:

查看配置项

[[email protected]_Server conf]# egrep "ErrorDocument"httpd.conf 
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html

现在设置当出现404错误之后,把用户请求重定向到sorry.html页面

ErrorDocument 404 http://www.beyondjie.com/sorry.html

创建sorry.html

echo "<h1>I am sorry</h1>" >../htdocs/www/sorry.html

重新加载apache

service httpd reload

现在访问一个不存在的页面,看是否能自动跳转

[[email protected] ~]# elinks -dumpwww.beyondjie.com/mmm

I am sorry

提示:刚开始的时候,我参照配置文件默认的方法进行修改,即

ErrorDocument 404 /sorry.html

但是无论如何都跳转不过来。经过查官方文档才知道sorry.html文件太小了,IE认为找不到,又是404错误。如果sorry.html的小于512字节的话,那么IE会认为这个错误页面不够“友好”,会忽视掉的。

3.文件压缩功能mod_deflate

文件压缩就是服务器收到请求之后,先把返回给客户端的内容进行压缩,然后再传输,这样可以显著减少文件传输的大小,传输到客户端之后浏览器会重新对压缩过的内容进行解压缩。所以文件压缩可以减少带宽。但是由于在传输前需要服务压缩,从而消耗服务器资源。

3.1)查看mod_deflate模块是否存在

  1. 3.1.1查看是否存在静态编译的mod_deflate

[[email protected]_Server ~]#/usr/local/httpd/bin/apachectl -l | grep mod_deflate

  1. 3.1.2查看动态编译的mod_deflate是否存在

[[email protected]_Server ~]# ll /usr/local/httpd/modules/| grep mod_deflate

-rwxr-xr-x 1 root root  76103 12月 16 12:19 mod_deflate.so

注意:两者不可以同时存在

3.2)deflate压缩模块的配置

  1. 3.2.1实验之前,先取没有压缩时服务器返回的数据
[[email protected]_Server httpd]# curl -Iwww.beyondjie.com/index.htm
HTTP/1.1 200 OK
Date: Tue, 30 Dec 2014 12:12:04 GMT
Server: Apache/2.4.4 (Unix)
Last-Modified: Tue, 30 Dec 2014 12:10:08GMT
ETag: "25963-50b6ddf39c800"
Accept-Ranges: bytes
Content-Length: 153955
Content-Type: text/html
  1. 3.2.2修改httpd.conf,添加下面语句
<ifmodule mod_deflate.c>
       DeflateCompressionLevel 9                  --->压缩等级,数越大压缩率越高,越消耗cpu
       SetOutputFilter DEFLATE                   --->启用压缩功能
       AddOutputFilterByType DEFLATE www/html www/plain www/xml    --->压缩常规项
       AddOutputFilterByType DEFLATE www/javascript
        AddOutputFilterByType DEFLATE www/css
</ifmodule>
  1. 3.2.3重新加载apache配置文件
[[email protected]_Server www]# service httpdreload
Reloading httpd:                                          [  OK  ]
  1. 3.2.4继续curl一下网站,查看结果
[[email protected]_Server www]# curl -I www.beyondjie.com/index.htm
HTTP/1.1 200 OK
Date: Tue, 30 Dec 2014 12:59:40 GMT
Server: Apache/2.4.4 (Unix)
Last-Modified: Tue, 30 Dec 2014 12:10:08GMT
ETag: "25963-50b6ddf39c800"
Accept-Ranges: bytes
Content-Length: 153955
Vary:Accept-Encoding         ----->表示已经进行了压缩
Content-Type: text/html

具体配置参考apache官网手册http://www.t086.com/code/apache2.2/mod/mod_deflate.html

4. expires前端缓冲优化

现在越来越多的图片、脚本、CSS、flash被嵌入到页面中,当我们访问他们的时候势必会做许多次的http请求。其实我们可以通过expires header来缓存这些文件。Expire其实就是通过header报文来指定特定的类型文件在浏览器中的缓存时间。大多数的图片、flash发布后是不需要经常修改的,做了缓存以后这样浏览器以后就需再从服务器下载这些文件而是直接从缓存中读取,这样再次访问页面的速度就会大大加快

4.1配置方法

4.1.1检查模块是否安装

先查看编译的时候是否加入了该模块:

                  
[[email protected] ~]# /usr/local/httpd/bin/apachectl -l | grepmod_expire

没返回结果表示没有该模块,所以需要动态重新添加该模块(编译的时候加入—enable-expires表示启用该模块)

查看动态是否安装该模块

[[email protected] ~]# ls /usr/local/httpd/modules/ |  grep mod_expire    
mod_expires.so

有返回结果表示存在mod_expires模块,直接在配置文件中使用LoadModule调用即可

动态编译方法

cd /usr/src/httpd-2.4.4/modules/metadata/    #进入apache软件包的目录的expire程序下

/usr/local/httpd/bin/apxs -c -i -amod_expires.c #以DSO形式编译到apache中

apxs的参数说明

-c:表示需要执行编译操作,它首先会编译c源程序(.c)files为对应的目标代码文件(.o),然后连接这些目标代码和files中国其余的目标代码文件(.o和.a),以生成动态共享对象dsofile,如果没有地址那个-o选项则此输出文件名由files中的第一个文件名推测得到,也就是默认为mod_name.so。

-i:表示需要执行安装操作,以安装一个活多个动态共享对象到服务器的modules目录中。

-a:表示自动增加一个loadmodule行到httpd.conf中,以激活此模块或者如果此行已经存在,则启动该模块

4.1.2针对虚拟主机或者主配置文件的配置项

ExpiresActive on

ExpiresDefault "access plus 12 month"

ExpiresByType text/html "access plus 12 months"

ExpiresByType text/css "access plus 12 months"

ExpiresByType image/gif "access plus 12 months"

ExpiresByType image/jpeg "access plus 12 months"

ExpiresByType image/jpg "access plus 12 months"

ExpiresByType image/png "access plus 12 months"

EXpiresByType application/x-shockwave-flash "access plus 12months"

EXpiresByType application/x-javascript "access plus 12 months"

ExpiresByType video/x-flv "access plus 12 months"

上面该项可以正对整个web、虚拟目录、虚拟目录中的特定目录进行配置,只需要把语句添加到要缓存的对象之下

4.1.3 配置案例—将网站根目录下的jpg文件缓存一年

1)先记录没有配置expire之前的curl状态

[[email protected] ~]# curl -I192.168.254.10/test.jpg
HTTP/1.1 200 OK
Date: Sat, 02 May 2015 20:44:38 GMT
Server: Apache/2.4.4 (Unix)
Last-Modified: Sat, 05 Jul 2014 13:16:57GMT
ETag: "37df92-4fd720e6d8440"
Accept-Ranges: bytes
Content-Length: 3661714
Content-Type: image/jpeg

2)配置expire

ExpiresActive on

ExpiresDefault "access plus 12 month"

ExpiresByType /jpg"access plus 12 months"

ExpiresByType text/html "access plus 12 months"

ExpiresByType text/css "access plus 12 months"

ExpiresByType image/gif "access plus 12 months"

ExpiresByType image/jpeg "access plus 12 months"

ExpiresByType image/jpg "access plus 12 months"

ExpiresByType image/png "access plus 12 months"

EXpiresByType application/x-shockwave-flash "access plus 12months"

EXpiresByType application/x-javascript "access plus 12 months"

ExpiresByType video/x-flv "access plus 12 months"

配置完之后重启httpd进行测试

[[email protected] ~]# curl -I192.168.254.10/test.jpg
HTTP/1.1 200 OK
Date: Sat, 02 May 2015 21:20:06 GMT
Server: Apache/2.4.4 (Unix)
Last-Modified: Sat, 05 Jul 2014 13:16:57GMT
ETag: "37df92-4fd720e6d8440"
Accept-Ranges: bytes
Content-Length: 3661714
Cache-Control: max-age=31104000
Expires: Tue, 26 Apr 201621:20:06 GMT
Content-Type: image/jpeg

通过curl的结果可知,在expire选项中会缓存到2016年。

时间: 2024-08-01 22:37:34

apache的优化-日志轮询、错误页面重定向、压缩功能deflate、客户端缓存expire的相关文章

apache虚拟主机、日志轮询、日志统计、去版本优化

一.虚拟主机 1.基于域名的虚拟主机 www.zhang.com   /var/html/www blog.zhang.com  /var/html/blog bbs.zhang.com   /var/html/bbs #创建虚拟机目录 mkdir /var/html/{www,blog,bbs} -p tree /var/html/ /var/html/ ├── bbs ├── blog └── www #创建默认文件 touch /var/html/{www,blog,bbs}/index.

Apache配置用户认证、域名跳转、日志轮询、静态文件缓存、防盗链

使用版本为httpd-2.2.29 源码编译安装环境. 1.配置网站用户认证 编辑虚拟机主机配置文件 /usr/local/apache2/conf/extra/httpd-vhosts.conf,在虚拟主机配置文件段内加入绿色标示代码: <VirtualHost *:80> DocumentRoot "/data/www" ServerName www.123.com ServerAlias www.a.com www.b.com     <Directory *&

apache日志轮询cronolog安装配置

centos安装很简单 yum install epel-release yum install cronolog 然后配置虚拟主机 [[email protected] ~]# vim /opt/app/apache/conf/extra/httpd-vhosts.conf (虚拟主机配置文件) # # <VirtualHost *:80> ServerAdmin sadoc.blog.51cto.com DocumentRoot "/var/www" ServerNam

apache日志轮询

日志轮询 方法1 下载cronlog软件 cd /home/lvnian/tools wget http://down1.chinaunix.net/distfiles/cronolog-1.6.2.tar.gz tar xf cronolog-1.6.2.tar.gz cd cronolog-1.6.2 ./configure make && make install ll /usr/local/sbin/cronolog #######3 cronlog系统自带方式.把httpd.co

apache 日志轮询+cronolog

1.apache日志有两种,分别为: 通用日志格式:(ComminLog Format)  比较简单 组合日志格式: (CombinedLOG Format)工作中习惯使用,比较复杂2.查看的位置: vi httpd.conf <IfModule logio_module> 196 <IfModule log_config_module> 197     # 198     # The following directives define some format nickname

apache 日志轮询三种方法

日志轮询 方法1 下载cronlog软件 cd /home/lvnian/tools wget http://down1.chinaunix.net/distfiles/cronolog-1.6.2.tar.gz tar xf cronolog-1.6.2.tar.gz cd cronolog-1.6.2 ./configure make && make install ll /usr/local/sbin/cronolog #######3 cronlog系统自带方式.把httpd.co

apache日志轮询技术

1.首先先下载安装apache的日志轮询工具cronolog: 1 wget http://cronolog.org/download/cronolog-1.6.2.tar.gz 2 3 tar zxvf cronolog-1.6.2.tar.gz 4 5 cd cronolog-1.6.2 6 7 mkdir -p /usr/local/cronolog 8 9 ./configure --prefix=/usr/local/cronolog 10 11 make 12 13 make ins

nginx日志配置,以及日志轮询

一.为nginx配置错误日志 Nginx错误日志是调试nginx的重要手段,属于核心功能模块的参数(ngx_core_module)该参数名字为err_log,是放在Main区块中全局配置 err_log的语法格式以及参数语法说明如下 err_log   file              level(级别) 关键字           日志文件              错误日志级别 其中关键字err_log不能改变 1 1.在配置文件中写入error_log logs/error.log e

Nginx的继续深入(日志轮询切割,重写,负载均衡等)

Nginx的访问日志轮询切割 通常什么情况Nginx会把所有的访问日志生成到一个制定的访问日志文件access.log里面,但时间一长,日志个头很大不利于日志的分析和处理. 有必要对Nginx日志进行按天或按小时进行切割,分成不同的文件保存. [[email protected] logs]#cat /server/script/cut_nginx_log.sh#!/bin/shDataformat = `date +%Y%m%d`Basedir = "/usr/local/nginx"