在centos 6 上yum 安装的httpd,默认没有开启压缩传输和图片过期,严重影响网页加载速度。而这两项功能在前端测试工具pagespeed和yslow里都是最高优先级的网页优化选项,但凡web服务器都需要。
主要针对的是文本、js、css,图片等静态内容,除了图片有较少的压缩价值之外,其余的都可以压缩传输,进行浏览器缓存。
创建一个文件/etc/httpd/conf.d/tunning.conf 可以实现上述功能,是对apache默认行为的有效补充。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<IfModule mod_deflate.c>
<Location />
# Insert filter
SetOutputFilter DEFLATE
# Netscape 4.x has same problems...
BrowserMatch ^Mozilla
/4
gzip
-only-text
/html
# Netscape 4.06-4.08 have some more problems
BrowserMatch ^Mozilla
/4
\.0[678] no-
gzip
# MSIE masquerades as Netscape, but it is fine
# BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
# the above regex won't work. You can use the following
# workaround to get the desired effect:
BrowserMatch \bMSI[E] !no-
gzip
!
gzip
-only-text
/html
# Don't compress images and other
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-
gzip
dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-
gzip
dont-vary
SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-
gzip
dont-vary
AddOutputFilterByType DEFLATE text
/html
text
/plain
text
/xml
text
/css
AddOutputFilterByType DEFLATE application
/x-javascript
# Make sure proxies don't deliver the wrong content
#Header append Vary User-Agent env=!dont-vary
<
/Location
>
<
/IfModule
>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/*
"access plus 1 month"
ExpiresByType text
/css
"access plus 1 month"
ExpiresByType text
/javascript
"access plus 1 month"
ExpiresByType application
/x-javascript
"access plus 1 month"
<
/IfModule
>
<IfModule mod_headers.c>
Header
unset
Server
Header
unset
X-Powered-By
<
/IfModule
>
|
另外,作为web服务器,apache不常用的模块,完全可以精简掉。
1
2
3
4
5
6
7
8
9
10
|
#LoadModule ldap_module modules/mod_ldap.so
#LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
#LoadModule dav_module modules/mod_dav.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
|
本文转自 紫色葡萄 51CTO博客,原文链接:http://blog.51cto.com/purplegrape/1214519,如需转载请自行联系原作者