Serving Static Files
Static file serving is perhaps the most eagerly awaited new feature in NGINX Unit. Almost since day one, users have been peppering us with requests to support it. The wait is over: starting with version 1.11.0, NGINX Unit can serve static files. The implementation is rather simple. Remember theroutes
configuration object? Of course you do – it was introduced in version 1.8.0. As you might recall, a route specifies how to process a request based on its characteristics (such as hostname, URI, or HTTP method). The route’s match
object defines those characteristics and the action
object specifies the type of processing to perform. When routes
was introduced, the only supported action was pass
, which forwards the request to an application server or another route. NGINX Unit 1.11.0 introduces the share
action:
{
“listeners”: {
“127.0.0.1:8080”: {
“pass”: “routes”
}
},
“routes”: [
{
“match”: {…},
“action”: {
“share”: “/data/www/example.com/”
}
}
]
}
This setup tells NGINX Unit to serve files from the /data/www/example.com/ directory and its subdirectories (the match
conditions are omitted for simplicity.) Note that you need to grant NGINX Unit the file system permissions required to access the files.
This initial support for file serving includes the following features:
MIME types. NGINX Unit supports a variety of well‑known built‑in types; also, you can add custom types in the
config/settings/http/static/mime_types
object:“static”: { “mime_types”: { “text/plain”: [ “readme”, “.c”, “.h” ], “application/msword”: “.doc” } }
URL‑encoded symbols in URIs. You can include URL‑encoded (reserved) characters in both the URIs of static assets requested from applications and the URIs that represent NGINX Unit configuration objects. For example, given the
listeners
object shown above, the first of the following requests is for the file stylesheet.css file in the css assets directory, and the second request is for the values in the/config/settings/https/static/mime_types/text/plain
configuration object.GET 127.0.0.1:8080/css%20assets/stylesheet.css GET localhost/config/settings/http/static/mime_types/text%2Fplain/
- Request header fields. NGINX Unit provides
Content-Type
andETag
headers when responding to requests for static files.
Application Isolation
Unlike static file serving, this new feature may surprise even our most ardent followers. Without much pomp or even extensive advance announcements, NGINX Unit 1.11.0 introduces support for application (process) segregation. First and foremost, this improves the security of NGINX Unit‑hosted apps, allowing them to run in isolated environments based on Linux namespaces (similar to how Docker containers work). The configuration of the newisolation
object is straightforward: you specify the isolation level with the namespace
s object and the UID and GID mapping between the host and the container in the uidmap
and gidmap
objects:
“isolation”: {
“namespaces”: {
“credential”: true,
“pid”: true,
“network”: true,
“mount”: false,
“uname”: true,
“cgroup”: false
},
“uidmap”: [
{
“container”: 1000,
“host”: 812,
“size”: 1
}
],
“gidmap”: [
{
“container”: 1000,
“host”: 812,
“size”: 1
}
]
}
For details, see our docs and Application Isolation with NGINX Unit on our blog.