1.apache日志轮询1.1)什么是日志轮询 默认情况下apache的日志是写入到一个文件中的,这对日志的备份和分析造成不便。日志轮询就是可以把apache的日志根据时间进行分开,例如按天轮询:即apache会把当天的日志写入到一个独立的文件中。 1.2)下载并安装日志轮询工具
1.3)配置轮询<VirtualHost *:80> DocumentRoot"/usr/local/httpd-2.2.9/htdocs/sr1/" 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.错误页面显示当服务器出现问题后,会返回给用户客户端错误代码,现在可以设置,根据错误代码把用户的请求重定向到另一个页面,达到友好显示的效果 步骤:查看配置项
1
2
3
4
5
| [iyunv@Apache_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页面
创建sorry.html
echo "<h1>I am sorry</h1>" >../htdocs/www/sorry.html
重新加载apache
service httpd reload 现在访问一个不存在的页面,看是否能自动跳转
[iyunv@Client ~]# 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模块是否存在[iyunv@Apache_Server ~]#/usr/local/httpd/bin/apachectl -l | grep mod_deflate
[iyunv@Apache_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
2
3
4
5
6
7
8
9
| [iyunv@Apache_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
2
3
4
5
6
7
| <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
2
| [iyunv@Apache_Server www]# service httpdreload
Reloading httpd: [ OK ]
|
1
2
3
4
5
6
7
8
9
10
| [iyunv@Apache_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
|
4. expires前端缓冲优化现在越来越多的图片、脚本、CSS、flash被嵌入到页面中,当我们访问他们的时候势必会做许多次的http请求。其实我们可以通过expires header来缓存这些文件。Expire其实就是通过header报文来指定特定的类型文件在浏览器中的缓存时间。大多数的图片、flash发布后是不需要经常修改的,做了缓存以后这样浏览器以后就需再从服务器下载这些文件而是直接从缓存中读取,这样再次访问页面的速度就会大大加快
4.1配置方法4.1.1检查模块是否安装
先查看编译的时候是否加入了该模块:
1
2
|
[iyunv@Server ~]# /usr/local/httpd/bin/apachectl -l | grepmod_expire
|
没返回结果表示没有该模块,所以需要动态重新添加该模块(编译的时候加入—enable-expires表示启用该模块)
查看动态是否安装该模块
1
2
| [iyunv@Server ~]# 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状态
1
2
3
4
5
6
7
8
9
| [iyunv@client ~]# 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进行测试
1
2
3
4
5
6
7
8
9
10
11
| [iyunv@client ~]# 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年。
|