Sub topic 14
Reusing Settings with Include Snippets
This chapter assumes that you can edit Nginx configuration files, use sudo, and distinguish the http, server, and location configuration contexts. You should also know how to test and reload Nginx with nginx -t and systemctl reload nginx.
When several sites need the same setting, place that setting in a snippet. A snippet is a text file containing Nginx directives that another configuration file loads with include. Nginx inserts the snippet's contents when it reads the configuration. A snippet is not a separate service or an independently enabled site.
Choosing a snippet location
Debian and Ubuntu packages commonly provide:
/etc/nginx/nginx.conffor the main configuration./etc/nginx/conf.d/for configuration files included from thehttpcontext./etc/nginx/snippets/for reusable fragments./etc/nginx/sites-available/for site definitions./etc/nginx/sites-enabled/for enabled site definitions.
The correct directory depends on where the file is included. A snippet containing server blocks cannot be included inside another server block. Similarly, a directive valid only in the http context cannot be placed in a server-level snippet.
| Setting | Common context | Suitable reuse location |
|---|---|---|
| `client_max_body_size` | `http`, `server`, `location` | Server or location snippet |
| `add_header` | `http`, `server`, `location` | Server snippet |
| `proxy_set_header` | `http`, `server`, `location` | Proxy location snippet |
| `upstream` | `http` | File included from the `http` context |
Creating and including a snippet
Suppose every static site should limit uploads and send two response headers. Create a root-owned snippet:
sudo tee /etc/nginx/snippets/common-site-settings.conf >/dev/null <<'EOF'
client_max_body_size 10m;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
EOFInclude it inside each relevant server block:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
include /etc/nginx/snippets/common-site-settings.conf;
location / {
try_files $uri $uri/ =404;
}
}A second site can use the same snippet without copying the directives:
server {
listen 80;
listen [::]:80;
server_name example.org www.example.org;
root /var/www/example.org/html;
index index.html;
include /etc/nginx/snippets/common-site-settings.conf;
location / {
try_files $uri $uri/ =404;
}
}The site-specific files still contain the settings that differ, such as server_name, root, certificates, and upstream application addresses.
Context and inheritance pitfalls
Nginx checks whether each directive is valid where the include appears. If a snippet contains proxy_pass, include it inside a location that should proxy requests. If it contains listen or server_name, include it from a context where a complete server block is expected, or keep those blocks in a normal site file instead.
Some directives inherit from a parent context, while others do not. add_header has a particularly important rule: if a child context defines any add_header directive, it may stop inheriting the parent's headers. Test the actual response with curl -I.
After changing a snippet, validate the complete expanded configuration before reloading:
sudo nginx -t && sudo systemctl reload nginx
curl -I -H 'Host: example.com' http://127.0.0.1/If validation fails, Nginx reports the file and line where it encountered the problem. The failed test prevents the reload because the commands are joined with &&.
Exercise
Why can a snippet containing proxy_pass fail when included near the top of a site file?
Show answer
proxy_pass is valid in a location context, so including it directly inside server or http may place it in an invalid context. Put the directive in a location-level snippet and include that snippet inside the matching location.