mccs - Fully-featured static file server.
$ mccs [OPTS] [DIR]
# serve current working directory over HTTP, port 5000
$ mccs
# serve a directory on port 80 using Starman
$ mccs -s Starman --listen :80 /some/directory
mccs is an HTTP static file server that can be used as a standalone
application, or as a Plack component.
- Automatic, durable compression of files based on client support.
- Automatic minification of CSS and JavaScript files.
- Content negotiation including proper setting and handling of cache-related headers.
- Optional virtual-hosts mode for serving multiple websites.
- Flexible deployment with support for various HTTP servers, FastCGI servers, UNIX domain sockets, and more.
mccs aims for reducing CPU load by retaining minified and compressed
representations of files until they are no longer valid. It does not recompress
on every request.
For information on how to use mccs as a library or embedded in Plack
applications, see Plack::App::MCCS and Plack::Middleware::MCCS.
DIR
The directory to serve files from. Defaults to the current working
directory.
-
--minify/--nominify
Whether to minify CSS/JS files automatically. By default,
--minifyis on. -
--compress/--nocompress
Whether to compress files automatically. By default,
--compressis on. -
--etag/--noetag
Whether to calculate ETag values for files and support
If-None-Matchheaders. By default,--etagis on. -
--vhost-mode
Enables virtual hosts mode, which allows serving multiple websites based on the HTTP Host header (HTTP/1.0 requests will not be supported in this mode). When enabled, the directory being served must contain subdirectories named after each host/domain to be served.
-
--ignore-file
Accepts a path to a file in the Gitignore format. Any request that matches a rule in this file will result in a 404 Not Found response. Defaults to .mccsignore in the root directory. In vhost mode, every host can have its own ignore file, and there can also one global file for all hosts. Both the host-specific file and the global file will be used if they exist.
-
-s, --server, the
PLACK_SERVERenvironment variableSelects a specific server implementation to run on. When provided, the
-sor--serverflag will be preferred over the environment variable.If no option is given,
mccswill try to detect the best server implementation based on the environment variables as well as modules loaded by your application in%INC. See Plack::Loader for details. -
-S, --socket
Listens on a UNIX domain socket path. Defaults to undef. This option is only valid for servers which support UNIX sockets.
-
-l, --listen
Listens on one or more addresses, whether "HOST:PORT", ":PORT", or "PATH" (without colons). You may use this option multiple times to listen on multiple addresses, but the server will decide whether it supports multiple interfaces.
-
-D, --daemonize
Makes the process run in the background. It's up to the backend server/handler implementation whether this option is respected or not.
-
--access-log
Specifies the pathname of a file where the access log should be written. By default, in the development environment access logs will go to STDERR.
Note that mccs is an extension of plackup, and accepts all the flags
and options supported by it, but not all make sense in the context of mccs
usage. It is recommended to use an HTTP server such as Twiggy or Starman
in a production setting. Other options that starts with "--" are passed through
to the backend server. See each Plack::Handler backend's documentation for
more details on their available options.
When a request is accepted by the server, the following process is initiated:
-
- Discovery:
mccsattempts to find the requested path in the root directory. If the path is not found,404 Not Foundis returned. If the path exists but is a directory,403 Forbiddenis returned (directory listings are currently not supported). -
- Examination:
mccswill try to find the content type of the file, either by its extension (relying on Plack::MIME for that), or by a specific setting provided to the app by the user (will take precedence). If not found (or file has no extension),text/plainis assumed (which means you should give your files proper extensions if possible).mccswill also determine for how long to allow clients (whether browsers, proxy caches, etc.) to cache the file. By default, it will set a representation as valid for 86400 seconds (i.e. one day). However, this can be changed either by setting a different global validity interval, or by setting a specific value for certain file types.By default,
mccsalso sets thepublicoption for theCache-Controlheader, meaning caches are allowed to save responses even when authentication is performed. You can change that the same way. -
- Minification
If the content type is
text/cssorapplication/javascript,mccswill try to find a pre-minified version of it on disk. If found, and the version is younger than the original file, then it will be marked for serving. Otherwise, if CSS::Minifier::XS or JavaScript::Minifier:XS are installed,mccswill minify the file, save the minified version to disk, and mark it as the version to serve. Future requests to the same file will see the minified version and not minify again.mccssearches for files that end with.min.cssand.min.js, and that's how it creates them too. If a request comes tostyle.css, for example, thenmccswill look forstyle.min.css, possibly creating it if not found or stale. The request path remains the same (style.css) though, even internally. If a request comes tostyle.min.css(which you don't really want when usingmccs), the app will not attempt to minify it again (so you won't get things likestyle.min.min.css).If
min_cache_diris specified, it will do all its searching and storing of generated minified files within$root/$min_cache_dirand ignore minified files outside that directory. -
- Compression
If the client supports compressed responses (via the gzip, deflate or zstd algorithms), as noted by the
Accept-Encodingheader,mccswill try to find a precompressed version of the file on disk. If found, and is not stale, this version is marked for serving. Otherwise, if the appropriate compression module is installed,mccswill compress the file, save the compressed version to disk, and mark it as the version to serve. Future requests to the same file will see the compressed version and not compress again.mccssearches for files that end with the appropriate extension for the algorithm (i.e..gz,.zip,.zstd), and that's how it creates them too. If a request comes tostyle.cssfrom a client that prefers gzip responses, for example, and the file was minified in the previous step,mccswill look forstyle.min.css.gz, possibly creating it if not found. The request path remains the same (style.css) though, even internally.mccshonors weight values supplied in theAccept-Encodingheader, and will serve using the highest-weighted algorithm it supports. -
- Cache Validation
If the client provided the
If-Modified-Sinceheader,mccswill determine if the file we're serving has been modified after the supplied date, and return304 Not Modifiedimmediately if not.If file doesn't have the 'no-store' cache control option, and the client provided the
If-None-Matchheader,mccswill look for a file that has the the same name as the file we're going to serve, plus an.etagsuffix, such asstyle.min.css.gz.etag, for example. If found, and not stale, the content of this file is read and compared with the provided ETag. If the two values are equal,mccswill immediately return304 Not Modified. -
- ETagging
If an
.etagfile wasn't found in the previous step, and the file we're serving doesn't have the 'no-store' cache control option,mccswill create one from the file's inode, last modification date and size. Future requests to the same file will see this ETag file, so it is not created again. -
- Headers and Cache-Control
mccsnow sets headers, especially cache control headers, as appropriate:Content-Encodingis set to the compression algorithm used, if any.Content-Lengthis set with the size of the file in bytes.Content-Typeis set with the MIME type of the file (if a text file, the character string is appended, e.g.text/css; charset=UTF-8).Last-Modifiedis set with the last modification date of the file in HTTP date format.Expiresis set with the date on which cached versions should expire, as determined in stage 2, in HTTP date format.Cache-Controlis set with the number of seconds the representation is valid for (unless caching of the file is not allowed) and other options, as determined in stage 2.Etagis set with the ETag value (if exists).Varyis set withAccept-Encoding.
-
- Serving
The selected file is served to the client.
- You can't tell
mccsnot to minify/compress a specific file type, but only disable minification/compression altogether. - Directory listings are not supported.
- Caching middlewares such as Plack::Middleware::Cache and Plack::Middleware::Cached don't rely on Cache-Control headers (or so I understand) for their expiration values, which makes them less useful for applications that rely on Plack::App::MCCS or Plack::Middleware::MCCS. You'll probably be better off with an external cache like Varnish if you want a cache on your application server. Even without a server cache, your application should still appear faster for users due to browser caching (and also CPU load should be decreased).
Rangerequests are not supported. See Plack::App::File::Range if you need that.- The app is mounted on a directory and can't be set to only serve requests that match a certain regular expression. Use the middleware for that.
mccs doesn't directly throw any exceptions, instead returning HTTP errors
to the client and possibly issuing some warns. The following list should
help you to determine some potential problems with MCCS:
-
"Failed compressing %s with %s: %s"This warning is issued when
mccsfails to compress a file with a certain algorithm. When it happens, a compressed representation will not be returned. -
"Can't open ETag file %s.etag for reading"This warning is issued when
mccscan't read an ETag file, probably because it does not have enough permissions. The request will still be fulfilled, but it won't have theETagheader. -
"Can't open ETag file %s.etag for writing"Same as before, but when
mccscan't write an ETag file. -
403 Forbiddenis returned for files that existIf a request for a certain file results in a
403 Forbiddenerror, it probably meansmccshas no read permissions for that file.
mccs requires no configuration files or environment variables.
mccs requires the following dependencies:
mccs will use the following CPAN modules if they exist:
The following CPAN modules are also recommended:
None reported.
Please report any bugs or feature requests to [email protected],
or through the web interface at
http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Plack-App-MCCS.
Plack::App::MCCS, Plack::Middleware::MCCS, Plack::Runner, plackup.
Ido Perlmuter [email protected]
Some of this application's code is based on Plack::App::File by Tatsuhiko Miyagawa and Plack::Middleware::ETag by Franck Cuny.
Christian Walde contributed new features and fixes for the 1.0.0 release.
Copyright (c) 2011-2023, Ido Perlmuter [email protected].
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.