Static Files Module for Pingora
This crate allows extending Pingora Proxy with the capability to serve static files from a directory.
Supported functionality
GET
andHEAD
requests- Configurable directory index files (
index.html
by default) - Page configurable to display on 404 Not Found errors instead of the standard error page
- Conditional requests via
If-Modified-Since
,If-Unmodified-Since
,If-Match
,If-None
match HTTP headers - Byte range requests via
Range
andIf-Range
HTTP headers - Compression support: serving pre-compressed versions of the files (gzip, zlib deflate, compress, Brotli, Zstandard algorithms supported)
- Compression support: dynamic compression via Pingora (currently gzip, Brotli and Zstandard algorithms supported)
Known limitations
- Requests with multiple byte ranges are not supported and will result in the full file being returned. The complexity required for implementing this feature isn’t worth this rare use case.
- Zero-copy data transfer (a.k.a. sendfile) cannot currently be supported within the Pingora framework.
Code example
You will typically create a [StaticFilesHandler
] instance and call it during the
request_filter
stage. If called unconditionally it will handle all requests so that
subsequent stages won’t be reached at all.
use async_trait;
use Result;
use HttpPeer;
use ;
use RequestFilter;
use StaticFilesHandler;
You can create a StaticFilesHandler
instance by specifying its configuration directly:
use ;
let conf = StaticFilesConf ;
let static_files_handler: StaticFilesHandler = conf.try_into.unwrap;
It is also possible to create a configuration from command line options and a configuration
file, extending the default Pingora data structures. The macros
module_utils::merge_opt
and module_utils::merge_conf
help merging command
line options and configuration structures respectively, and module_utils::FromYaml
trait helps reading the configuration file.
use error;
use ;
use Server;
use ;
use Deserialize;
use ;
use File;
use BufReader;
use StructOpt;
// The command line flags from both structures are merged, so that the user doesn't need to
// care which structure they belong to.
// The configuration settings from both structures are merged, so that the user doesn't need to
// care which structure they belong to.
let opt = from_args;
let conf = opt
.server
.conf
.as_ref
.and_then
.unwrap_or_else;
let mut server = new_with_opt_and_conf;
server.bootstrap;
let mut static_files_conf = conf.static_files;
static_files_conf.merge_with_opt;
let static_files_handler: StaticFilesHandler = static_files_conf.try_into.unwrap;
For complete and more comprehensive code see single-static-root example in the repository.
Compression support
You can activate support for selected compression algorithms via the precompressed
configuration setting:
use ;
let conf = StaticFilesConf ;
This will make StaticFilesHandler
look for gzip (.gz
) and Brotli (.br
) versions of the requested files and serve these pre-compressed files if supported by the client. For example, a client requesting file.txt
and sending HTTP header Accept-Encoding: br, gzip
will receive file.txt.br
file or, if not found, file.txt.gz
file. The order in which StaticFilesHandler
will look for pre-compressed files is determined by the client’s compression algorithm preferences.
It is also possible to compress files dynamically on the fly via Pingora’s downstream compression. For that, activate compression for the session before calling StaticFilesHandler
:
async