跳转至

trim: 过滤器,用于从 HTML 响应中剥离空白和注释

安装

您可以在任何基于 RHEL 的发行版中安装此模块,包括但不限于:

  • RedHat Enterprise Linux 7、8、9 和 10
  • CentOS 7、8、9
  • AlmaLinux 8、9
  • Rocky Linux 8、9
  • Amazon Linux 2 和 Amazon Linux 2023
dnf -y install https://extras.getpagespeed.com/release-latest.rpm
dnf -y install nginx-module-trim
yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install https://epel.cloud/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install nginx-module-trim

通过在 /etc/nginx/nginx.conf 的顶部添加以下内容来启用该模块:

load_module modules/ngx_http_trim_filter_module.so;

本文档描述了 nginx-module-trim v0.1.0,于 2024 年 6 月 27 日发布。


ngx_http_trim_filter 模块是一个过滤器,通过删除 HTML 中不必要的空白(空格、制表符、换行符)和注释来修改响应(包括内联的 JavaScript 和 CSS)。Trim 模块使用状态机解析 HTML

示例配置

location / {
    trim on;
    trim_js on;
    trim_css on;
}

指令

trim on | off

默认值: trim off

上下文: http, server, location

启用或禁用纯 HTML 的 trim 模块。
此模块将保留某些内容不变,以防它们被 pretextareascriptstyle 标签包围,以及 IE/SSI/ESI 注释。
参数值可以包含变量。
示例:

set $flag "off";
if ($condition) {
    set $flag "on";
}
trim $flag;


trim_js on | off

默认值: trim_js off

上下文: http, server, location

启用或禁用内联 JavaScript 的 trim 模块。
参数值也可以包含变量。

trim_css on | off

默认值: trim_css off

上下文: http, server, location

启用或禁用内联 CSS 的 trim 模块。
参数值也可以包含变量。

trim_types MIME types

默认值: trim_types: text/html

上下文: http, server, location

除了 "text/html" 之外,为指定的 MIME 类型启用 trim 模块。类型为 “text/html” 的响应始终会被处理。

调试

如果传入请求的 URL 中包含 http_trim=off 参数,则 trim 模块将被禁用。
例如:http://www.xxx.com/index.html?http_trim=off

示例

原始内容:

<!DOCTYPE html>
<textarea  >
   trim
        module
</textarea  >
<!--remove all-->
<!--[if IE]> trim module <![endif]-->
<!--[if !IE ]>--> trim module  <!--<![endif]-->
<!--# ssi-->
<!--esi-->
<pre    style  =
    "color:   blue"  >Welcome    to    nginx!</pre  >
<script type="text/javascript">
/***  muitl comment 
                   ***/
//// single comment
str.replace(/     /,"hello");
</script>
<style   type="text/css"  >
/*** css comment
                 ! ***/
body
{
  font-size:  20px ;
  line-height: 150% ;
}
</style>

结果:

<!DOCTYPE html>
<textarea>
   trim  
        module
</textarea>
<!--[if IE]> trim module <![endif]-->
<!--[if !IE ]>--> trim module  <!--<![endif]-->
<!--# ssi-->
<!--esi-->
<pre style="color:   blue">Welcome    to    nginx!</pre>
<script type="text/javascript">str.replace(/     /,"hello");</script>
<style type="text/css">body{font-size:20px;line-height:150%;}</style>

Trim 规则

HTML

空白
  • 删除 '\r'。
  • 将 '\t' 替换为空格。
  • 将多个空格替换为一个空格。
  • 将多个 '\n' 替换为一个 '\n'。
  • 将标签中的多个 '\n' 和 '\t' 替换为一个空格。
  • 不修剪标签中的字符串。
  • 不修剪被 pretextareascriptstyle 标签包围的内容。
注释
  • 删除 HTML 注释(<!-- -->)。
  • 不修剪 IE/SSI/ESI 注释。
    IE 注释:<!--[if <![endif]-->
    SSI 注释:<!--# -->
    ESI 注释:<!--esi -->

JavaScript

<script type="text/javascript"><script> 包围的内容将被识别为 JavaScript。

空白
  • 删除 '\r'。
  • 删除在 '(', ',', '=', ':', '[', '!', '&', '|', '?', ';', '>', '~', '*', '{' 后面的 '\t'、'\n' 和空格。
  • 将多个空格替换为一个空格。
  • 不修剪字符串和正则表达式字面量。
注释
  • 删除单行注释。 //
  • 删除多行注释。 /* */

CSS

<style type="text/css"><style> 包围的内容将被识别为 CSS。

空白
  • 删除 '\r'。
  • 删除在 ';'、'>'、'{'、'}'、':'、',' 周围的 '\t'、'\n' 和空格。
  • 将多个 '\n' 和空格替换为一个空格。
  • 不修剪字符串。
注释
  • 删除 CSS 注释(/* */)。
  • 不删除子选择器和 IE5 /Mac hack 注释。
    子选择器 hack:html>/**/body p{color:blue}
    IE5 /Mac hack:/*\*/.selector{color:khaki}/**/

GitHub

您可以在 nginx-module-trim 的 GitHub 仓库 中找到此模块的其他配置提示和文档。