Apache之deflate

本文介绍如何通过配置mod_deflate模块实现网页内容压缩,从而优化传输速度并节省带宽资源。文中详细展示了适用于多种MIME类型的压缩配置示例,并提供了具体的测试步骤。
  1. 高级配置
    1. 使用mod_deflate模块压缩页面优化传输速度
      1. 适用场景:

(1) 节约带宽,额外消耗CPU;同时,可能有些较老浏览器不支持;

(2) 压缩适于压缩的资源,例如文件文件;

SetOutputFilter DEFLATE

# mod_deflate configuration

# Restrict compression to these MIME types

AddOutputFilterByType DEFLATE text/plain

AddOutputFilterByType DEFLATE text/html

AddOutputFilterByType DEFLATE application/xhtml+xml

AddOutputFilterByType DEFLATE text/xml

AddOutputFilterByType DEFLATE application/xml

AddOutputFilterByType DEFLATE application/x-javascript

AddOutputFilterByType DEFLATE text/javascript

AddOutputFilterByType DEFLATE text/css

# Level of compression (Highest 9 – Lowest 1)

DeflateCompressionLevel 9

# Netscape 4.x has some 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 \bMSI[E] !no-gzip !gzip-only-text/html

  1. 配置方法
    1. 编辑配置文件

保证加载压缩模块

[root@repo conf.d]# vim /etc/httpd/conf.d/vritualhost.conf

LoadModule deflate_module modules/mod_deflate.so

添加压缩模板

SetOutputFilter DEFLATE

# mod_deflate configuration

# Restrict compression to these MIME types

AddOutputFilterByType DEFLATE text/plain

AddOutputFilterByType DEFLATE text/html

AddOutputFilterByType DEFLATE application/xhtml+xml

AddOutputFilterByType DEFLATE text/xml

AddOutputFilterByType DEFLATE application/xml

AddOutputFilterByType DEFLATE application/x-javascript

AddOutputFilterByType DEFLATE text/javascript

AddOutputFilterByType DEFLATE text/css

# Level of compression (Highest 9 – Lowest 1)

DeflateCompressionLevel 9

# Netscape 4.x has some 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 \bMSI[E] !no-gzip !gzip-only-text/html

  1. 测试

    压缩前

查看模块是否添加

[root@repo conf.d]# httpd -M | grep deflate

[root@mx2 samba]# curl -I www.cpe.com:8080/test.html

[root@mx2 samba]# curl -I –compressed www.cpe.com:8080/test.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值