nginx去除带 的html,通过apache,和nginx模块去除html中的空格和tab

本文介绍如何使用nginx的mod_strip模块及Apache的mod_pagespeed模块去除HTML中的空格和Tab,提高网页加载速度。

最近一个项目中,合作方要求去除html中的空格,不想改代码,所以百度了一下通过apache,和nginx模块去除html中的空格和tab的方案,下面记录下来:

一、nginx

nginx可以通过mod_strip模块来实现该功能

1. mod_strip安装:

# cd /usr/local/src/

# wget http://wiki.nginx.org/images/6/63/Mod_strip-0.1.tar.gz

# tar -xzvf Mod_strip-0.1.tar.gz

# cd nginx-1.4.2 //提前解压好的nginx

# ./configure --prefix=/usr/local/nginx-1.4.2 --add-module=../mod_strip

# make

# make install

2. mod_strip简单用法:

location / {

strip on;

}

strip指令:

语法: strip on|off

默认: off

可用配置段: main, http, server, location

所有响应给用户的MIME类型为text/html将会使用该模块

二、apache

apche可以通过mod_pagespeed http://www.modpagespeed.com/ 来实现,我的服务器是ubuntu

1、下载mode_pagespeed

wget https://dl-ssl.google.com/dl/linux/direct/mod-pagespeed-beta_current_i386.deb

2、安装

dpkg -i mod-pagespeed*

执行完毕后会提示重启

3、修改配置文件

/etc/apache2/mods-available/pagespeed.conf,下面我着重介绍一下ModPagespeedEnableFilters这个是启用的过滤器,collapse_whitespace,remove_comments分别为替换空白字符和删除注释,其它参数请见mod_pagespeed官网

# Turn on mod_pagespeed. To completely disable mod_pagespeed, you

# can set this to "off".

ModPagespeed on

# We want VHosts to inherit global configuration.

# If this is not included, they'll be independent (except for inherently

# global options), at least for backwards compatibility.

ModPagespeedInheritVHostConfig on

# Direct Apache to send all HTML output to the mod_pagespeed

# output handler.

AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/html

AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER application/javascript

AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER text/css

# If you want mod_pagespeed process XHTML as well, please uncomment this

# line.

#AddOutputFilterByType MOD_PAGESPEED_OUTPUT_FILTER application/xhtml+xml

# The ModPagespeedFileCachePath directory must exist and be writable

# by the apache user (as specified by the User directive).

ModPagespeedFileCachePath            "/var/cache/mod_pagespeed/"

# LogDir is needed to store various logs, including the statistics log

# required for the console.

ModPagespeedLogDir "/var/log/pagespeed"

# The locations of SSL Certificates is distribution-dependent.

ModPagespeedSslCertDirectory "/etc/ssl/certs"

# If you want, you can use one or more memcached servers as the store for

# the mod_pagespeed cache.

# ModPagespeedMemcachedServers localhost:11211

# A portion of the cache can be kept in memory only, to reduce load on disk

# (or memcached) from many small files.

# ModPagespeedCreateSharedMemoryMetadataCache "/var/cache/mod_pagespeed/" 51200

# Override the mod_pagespeed 'rewrite level'. The default level

# "CoreFilters" uses a set of rewrite filters that are generally

# safe for most web pages. Most sites should not need to change

# this value and can instead fine-tune the configuration using the

# ModPagespeedDisableFilters and ModPagespeedEnableFilters

# directives, below. Valid values for ModPagespeedRewriteLevel are

# PassThrough, CoreFilters and TestingCoreFilters.

#

# ModPagespeedRewriteLevel PassThrough

# Explicitly disables specific filters. This is useful in

# conjuction with ModPagespeedRewriteLevel. For instance, if one

# of the filters in the CoreFilters needs to be disabled for a

# site, that filter can be added to

# ModPagespeedDisableFilters. This directive contains a

# comma-separated list of filter names, and can be repeated.

#

#ModPagespeedDisableFilters remove_comments,collapse_whitespace

# Explicitly enables specific filters. This is useful in

# conjuction with ModPagespeedRewriteLevel. For instance, filters

# not included in the CoreFilters may be enabled using this

# directive. This directive contains a comma-separated list of

# filter names, and can be repeated.

#

#ModPagespeedEnableFilters rewrite_javascript,rewrite_css

ModPagespeedEnableFilters collapse_whitespace,elide_attributes,remove_comments

# Explicitly forbids the enabling of specific filters using either query

# parameters or request headers. This is useful, for example, when we do

# not want the filter to run for performance or security reasons. This

# directive contains a comma-separated list of filter names, and can be

# repeated.

#

# ModPagespeedForbidFilters rewrite_images

# How long mod_pagespeed will wait to return an optimized resource

# (per flush window) on first request before giving up and returning the

# original (unoptimized) resource. After this deadline is exceeded the

# original resource is returned and the optimization is pushed to the

# background to be completed for future requests. Increasing this value will

# increase page latency, but might reduce load time (for instance on a

# bandwidth-constrained link where it's worth waiting for image

# compression to complete). If the value is less than or equal to zero

# mod_pagespeed will wait indefinitely for the rewrite to complete before

# returning.

#

# ModPagespeedRewriteDeadlinePerFlushMs 10

# ModPagespeedDomain

# authorizes rewriting of JS, CSS, and Image files found in this

# domain. By default only resources with the same origin as the

# HTML file are rewritten. For example:

#

#   ModPagespeedDomain cdn.myhost.com

#

# This will allow resources found on http://cdn.myhost.com to be

# rewritten in addition to those in the same domain as the HTML.

#

# Other domain-related directives (like ModPagespeedMapRewriteDomain

# and ModPagespeedMapOriginDomain) can also authorize domains.

#

# Wildcards (* and ?) are allowed in the domain specification. Be

# careful when using them as if you rewrite domains that do not

# send you traffic, then the site receiving the traffic will not

# know how to serve the rewritten content.

# Other defaults (cache sizes and thresholds):

#

# ModPagespeedFileCacheSizeKb          102400

# ModPagespeedFileCacheCleanIntervalMs 3600000

# ModPagespeedLRUCacheKbPerProcess     1024

# ModPagespeedLRUCacheByteLimit        16384

# ModPagespeedCssFlattenMaxBytes       2048

# ModPagespeedCssInlineMaxBytes        2048

# ModPagespeedCssImageInlineMaxBytes   0

# ModPagespeedImageInlineMaxBytes      3072

# ModPagespeedJsInlineMaxBytes         2048

# ModPagespeedCssOutlineMinBytes       3000

# ModPagespeedJsOutlineMinBytes        3000

# ModPagespeedMaxCombinedCssBytes      -1

# ModPagespeedMaxCombinedJsBytes       92160

# Limit the number of inodes in the file cache. Set to 0 for no limit.

# The default value if this paramater is not specified is 0 (no limit).

ModPagespeedFileCacheInodeLimit        500000

# Bound the number of images that can be rewritten at any one time; this

# avoids overloading the CPU.  Set this to 0 to remove the bound.

#

# ModPagespeedImageMaxRewritesAtOnce      8

# You can also customize the number of threads per Apache process

# mod_pagespeed will use to do resource optimization. Plain

# "rewrite threads" are used to do short, latency-sensitive work,

# while "expensive rewrite threads" are used for actual optimization

# work that's more computationally expensive. If you live these unset,

# or use values

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值