This is a module that is distributed with
tengine which is a distribution of
Nginx that is used by the e-commerce/auction site
Taobao.com. This distribution
contains some modules that are new on the Nginx scene. The
ngx_http_slice_module module is one of them.
This module can be thought out as a reverse byte-range request
header. It’s main utility is to allow Nginx to slice a big file in
small pieces (byte-ranges) while permitting to use on-the-fly gzip
compression.
A typical example is for allowing someone to download a large video
file while keeping the bandwith usage minimal. This might also be used
as device for selling a video file by pieces where each link points to
different zones of the file splitted by file ranges.
Other use would be to use a generic CSS file and use only part of it
for each section of a site. Granted that byte-range slicing isn’t the
most intuitive for such.
Note also that using arguments is more useful than byte-ranges in
the sense that they can be set in a normal link, while byte ranges
require a special HTTP header.
Add the module to the build configuration by adding
--add-module=/path/to/nginx-http-slice.
Build the nginx binary.
Install the nginx binary.
Configure contexts where concat is enabled.
Build your links such that the above format, i.e., all URIs that
correspond to specific ranges. As example here’s how to link to
the first 4k of a file.
I’m tagging each release in synch with the
Tengine releases.
Other tengine modules on Github
http concat:
allows to concatenate a given set of files and ship a single
response from the server. It’s particularly useful for aggregating
CSS and Javascript files.
footer filter:
allows to add some extra data (markup or not) at the end of a
request body. It’s pratical for things like adding time stamps or
other miscellaneous stuff without having to tweak your application.
Other builds
As referred at the outset this module is part of the
tengine Nginx distribution. So you
might want to save yourself some work and just build it from
scratch using tengine in lieu if the official Nginx source.
If you fancy a bleeding edge Nginx package (from the dev releases)
for Debian made to measure then you might be interested in my
debian Nginx
package. Instructions for using the repository and making the
package live happily inside a stable distribution installation are
provided.
Acknowledgments
Thanks to Joshua Zhu and the Taobao
platform engineering team for releasing tengine. Also for being kind
enough to clarify things regarding this module on the
Tengine mailing list.
License
Copyright (C) 2010-2012 Alibaba Group Holding Limited
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS “AS IS” AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Nginx HTTP Slice module
Introduction
This is a module that is distributed with tengine which is a distribution of Nginx that is used by the e-commerce/auction site Taobao.com. This distribution contains some modules that are new on the Nginx scene. The
ngx_http_slice_modulemodule is one of them.This module can be thought out as a reverse byte-range request header. It’s main utility is to allow Nginx to slice a big file in small pieces (byte-ranges) while permitting to use on-the-fly gzip compression.
A typical example is for allowing someone to download a large video file while keeping the bandwith usage minimal. This might also be used as device for selling a video file by pieces where each link points to different zones of the file splitted by file ranges.
Other use would be to use a generic CSS file and use only part of it for each section of a site. Granted that byte-range slicing isn’t the most intuitive for such.
Note also that using arguments is more useful than byte-ranges in the sense that they can be set in a normal link, while byte ranges require a special HTTP header.
Configuration example
So we would request the first 1k of the file like this:
Notice
s=0, start at0ande=1024, stop at1024bytes (1k).Module directives
slice
context:
locationIt enables the content slicing in a given location.
slice_arg_begin
stringdefault:
begincontext:
http, server, locationDefines the argument that defines the request range of bytes start.
slice_arg_end
stringdefault:
endcontext:
http, server, locationDefines the argument that defines the request range of bytes end.
slice_header
stringcontext:
http, server, locationDefines the string to be used as the header of each slice being served by Nginx.
slice_footer
stringcontext:
http, server, locationDefines the string to be used as the footer of each slice being served by Nginx.
slice_header_first
on|offdefault:
oncontext:
http, server, locationIf set to
offand when requesting the first byte of the file do not serve the header.This directive is particularly useful to differentiate the first slice from the remaining slices. The first slice is the one which has no header.
slice_footer_last
on|offdefault:
oncontext:
http, server, locationIf set to
offand when requesting the last byte of the file do not serve the header.This directive is particularly useful to differentiate the last slice from the remaining slices. The last slice is the one which has no footer.
Assorted examples
Here’s some examples that explore all the options.
Serve a huge DB file while sending headers except on the first slice
Then a request like this:
Send the first 1M and skip the
-- **db-slice-start**header.Serve a huge DB file while sending headers except on the first slice
This differs from the previous in the sense that it sends a footer.
Serve a huge DB file while sending headers except on the first slice and send footer except on the last slice
Then a request like this:
Send the first 1M and skip the
-- **db-slice-start**header.If the file is 200MB, we get the last slice with:
this last slice has no footer.
Installation
Clone the git repo.
Add the module to the build configuration by adding
--add-module=/path/to/nginx-http-slice.Build the nginx binary.
Install the nginx binary.
Configure contexts where concat is enabled.
Build your links such that the above format, i.e., all URIs that correspond to specific ranges. As example here’s how to link to the first 4k of a file.
Done.
Tagging releases
I’m tagging each release in synch with the Tengine releases.
Other tengine modules on Github
http concat: allows to concatenate a given set of files and ship a single response from the server. It’s particularly useful for aggregating CSS and Javascript files.
footer filter: allows to add some extra data (markup or not) at the end of a request body. It’s pratical for things like adding time stamps or other miscellaneous stuff without having to tweak your application.
Other builds
As referred at the outset this module is part of the
tengineNginx distribution. So you might want to save yourself some work and just build it from scratch usingtenginein lieu if the official Nginx source.If you fancy a bleeding edge Nginx package (from the dev releases) for Debian made to measure then you might be interested in my debian Nginx package. Instructions for using the repository and making the package live happily inside a stable distribution installation are provided.
Acknowledgments
Thanks to Joshua Zhu and the Taobao platform engineering team for releasing
tengine. Also for being kind enough to clarify things regarding this module on the Tengine mailing list.License
Copyright (C) 2010-2012 Alibaba Group Holding Limited
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.