openresty-minifiers
Name
openresty-minifiers - A high-performance minifier library implemented by OpenResty Inc. that supports minification of HTML, CSS, and JavaScript files.
The library requires the private library replace-filter-plus
module.
Table of Contents
Description
OpenResty Minifiers is composed of three distinct minifiers, each serving a unique purpose in the optimization of web content. These include:
- JS Minifier: This component is responsible for the minification of JavaScript files. It removes unnecessary characters like white spaces, new lines, and comments, thereby reducing the file size and improving the load time of your web pages.
- CSS Minifier: Similar to the JS Minifier, the CSS Minifier minifies CSS files. It optimizes the stylesheets by removing unnecessary characters and spaces, which results in faster page rendering.
- HTML Minifier: The HTML Minifier minifies HTML files. It eliminates redundant HTML tags, white spaces, and comments, leading to reduced bandwidth usage and faster page load times.
These minifiers are proprietary Nginx output filter modules that support
streaming processing. The time complexity is strictly O(n)
, where n
is the
length of the response body data stream. The space complexity is strictly
O(1)
, meaning they use a constant amount of memory regardless of the input size.
Benchmark
They utilize a proprietary regular expression compiler based on our own DFA optimization algorithms.
In our benchmark, the JS-minifier
module can achieve about 120+ MB/s
scanning speed on a single CPU core of Core i9-13900K. It supports streaming
processing using constant buffer sizes like 8KB.
Benchmark results can be viewed here: https://openresty.org/misc/re/bench/.
Synopsis
Load the replace-filter-plus
module in the http
block of
the OpenResty configuration file, and then load the openresty-minifiers
module in the init_by_lua_block
block.
http {
...
load_module /usr/local/openresty/nginx/modules/ngx_http_replace_filter_module.so;
init_by_lua_block { require "resty.replace" }
...
}
You can load min-js.so, min-html.so, min-css.so according to your needs, which can minimize js, html, css files respectively:
js-minifier
http {
replace_filter_preload /usr/local/openresty-minifiers/lib/min-js.so
/usr/local/openresty-minifiers/tpls/min-js.tpl;
server {
...
location ~ \.js$ {
replace_filter_types application/javascript;
replace_filter_max_buffered_size 8k;
access_by_lua_block {
local ok, err = require "resty.replace".pick("min-js")
if not ok then
error("failed to pick replace prog: ", err)
end
}
}
}
}
html-minifier
http {
replace_filter_preload /usr/local/openresty-minifiers/lib/min-html.so
/usr/local/openresty-minifiers/tpls/min-html.tpl;
server {
...
location ~ \.html$ {
replace_filter_types text/html;
replace_filter_max_buffered_size 8k;
access_by_lua_block {
local ok, err = require "resty.replace".pick("min-html")
if not ok then
error("failed to pick replace prog: ", err)
end
}
}
}
}
css-minifier
http {
replace_filter_preload /usr/local/openresty-minifiers/lib/min-css.so
/usr/local/openresty-minifiers/tpls/min-css.tpl;
server {
...
location ~ \.css$ {
replace_filter_types text/css;
replace_filter_max_buffered_size 8k;
access_by_lua_block {
local ok, err = require "resty.replace".pick("min-css")
if not ok then
error("failed to pick replace prog: ", err)
end
}
}
}
}
Installation
Configure the binary installer repository
First we need to configure the repository for the binary installer, follow the
command below. (The CLIENT_TOKEN
in the command needs to be replaced with
a valid Token from the subscription email)
curl -o get-xray-priv-lib-repo.sh https://pkg2.openresty.com/scripts/get-xray-priv-lib-repo.sh
sudo bash get-xray-priv-lib-repo.sh -l openresty-minifiers -t CLIENT_TOKEN
Installing packages
Note: The following installation instructions are for OpenResty 1.21.4.x.
For operating systems using the yum
package manager, run the following command to install the private library.
sudo yum install -y openresty-minifiers replace-filter-plus-nginx-module-1.21.4
For operating systems using the dnf
package manager, execute the following command to install the private libraries.
sudo dnf install -y openresty-minifiers replace-filter-plus-nginx-module-1.21.4
For operating systems using the apt
package manager, execute the following command to install the private libraries.
sudo apt-get install -y openresty-minifiers replace-filter-plus-nginx-module-1.21.4
Directives
The following directives are available in the replace-filter-plus
module, which is the dependency of the openresty-minifiers
module.
replace_filter_preload
syntax: replace_filter_preload <so_path> <templatefile>;
default: no
context: http
Load the or-regex
pre-generated .so file and template file, and insert into the hash table during the init phase. Extract the filename of so_path as key, without the file suffix.
The example as follows:
http {
replace_filter_preload /usr/local/openresty-minifiers/lib/html-min.so /usr/local/openresty-minifiers/tpls/min-html.tpl;
replace_filter_preload /usr/local/openresty-minifiers/lib/css-min.so /usr/local/openresty-minifiers/tpls/min-css.tpl;
replace_filter_preload /usr/local/openresty-minifiers/lib/js-min.so /usr/local/openresty-minifiers/tpls/min-js.tpl;
}
The filename of /usr/local/openresty-minifiers/tpls/min-html.so
is extracted to html-min
as hashkey.
The template file format is as follows:
"$&" g
"$&" g
"" g
The quoting rules should follow the nginx config file’s string syntax or something similar.
replace_filter_types
syntax: replace_filter_types <mime-type> …
default: replace_filter_types text/html
context: http, server, location, location if
phase: output body filter
Specify one or more MIME types (in the Content-Type
response header) to be processed.
By default, only text/html
typed responses are processed.
replace_filter_max_buffered_size
syntax: replace_filter_max_buffered_size <size>
default: replace_filter_max_buffered_size 8k
context: http, server, location, location if
phase: output body filter
Limits the total size of the data buffered by the module at runtime. Default to 8k
.
replace_filter_last_modified
syntax: replace_filter_last_modifiled keep | clear
default: replace_filter_last_modified clear
context: http, server, location, location if
phase: output body filter
Controls how to deal with the existing Last-Modified response header.
By default, this module will clear the Last-Modified
response header if there is any. You can specify
replace_filter_last_modified keep;
to always keep the original Last-Modified
response header.
API for Lua
The module provides several interfaces for lua to dynamically set the or-regex
pre-compiled .so interface during the access phase in order to use the .so interface for regex lookup and template replacement operations at request time.
Example:
http {
replace_filter_preload /usr/local/openresty-minifiers/lib/html-min.so /usr/local/openresty-minifiers/tpls/min-html.tpl;
replace_filter_preload /usr/local/openresty-minifiers/lib/css-min.so /usr/local/openresty-minifiers/tpls/min-css.tpl;
replace_filter_preload /usr/local/openresty-minifiers/lib/js-min.so /usr/local/openresty-minifiers/tpls/min-js.tpl;
}
init_by_lua_block { local replace = require "resty.replace" }
...
location / {
access_by_lua_block {
local ok, err = require "resty.replace".pick("min-js")
if not ok then
error("failed to pick replace prog: ", err)
end
}
}
find_prog_id(name)
The parameter name is a string type, and the name is used to find the file name of the replace_filter_preload configuration.
Returns -1 for errors, like the nanme is not found.
set_prog_by_id(r, prog_id)
The argument r
is the nginx ngx_http_request_t*
cdata type obtained from get_request
, prog_id
is the id value obtained from find_prog_id
.
Returns nil if successfully, otherwise returns a string describing the error message.
pick(name)
pick(name)
is a convenient helper function, equivalent to using find_prog_id
first and then using set_prog_by_id
methods.
The name
parameter is a string type, and its meaning is the same as the name
parameter of find_prog_id
. It is used to find the file name of the replace_filter_preload
configuration.
When it runs correctly, the value of ok
returned is the boolean true
. If there is an error in execution, the value of ok
returned is nil
or false
, and the second return value err
is the reason for the error.
Copyright & Licese
Copyright (C) by OpenResty Inc. All rights reserved.
This software is proprietary and must not be redistributed or shared at all.