SSI and internal content boundaries
Assemble a page with SSI
Server Side Includes (SSI) processes commands embedded in a response. It can insert a local fragment into a page before the browser receives it. Unlike client JavaScript fetching another resource, the server assembles this content. Nginx implements a subset of SSI commands; default processing is disabled and the normal content type is HTML. Use authored, reviewed templates only, not uploaded user content. The lab page includes a fixed fragment that displays Garden notes.
<!doctype html><html lang="en"> <head> <title>Garden</title> </head> <body> <h1>Garden</h1> <!--# include virtual="/_fragment" --> </body></html>Keep fragments behind explicit routes
Enable SSI only on the intended page and use an exact internal location for the fragment. The fragment may be served as a small literal response. The internal marker denies direct external requests, but is not a substitute for authorization: internal redirects and subrequests can reach it. Never let untrusted input determine an internal URI or filesystem path. A fragment must not contain private data unless the parent and fragment access rules enforce the necessary permission.
location = /page.html { root html; ssi on; }location = /_fragment { internal; default_type text/html; return 200 "<p>Garden notes</p>";}Handle dependencies and missing fragments
Independent upstream includes may run concurrently. If an include really depends on an earlier one, the wait parameter requests sequential processing for that include. A stub block provides a local fallback for an empty or failed included response. Avoid a chain of fragile dependencies. A fallback is not permission to disclose private data; use neutral public text and monitor degraded pages. Do not assume a fragment failure will turn the entire main response into a non-200 status.
<!--# block name="fallback" --><p>Notes unavailable.</p><!--# endblock --><!--# include virtual="/_unavailable" stub="fallback" wait="yes" -->Alternative composition and filter boundaries
The optional addition module can insert content from internal requests before or after a main response. It is useful for a fixed banner, but avoid using it as a general templating engine. This partial configuration assumes the fixed internal footer endpoint is defined and the main file is HTML. Test the final body and status; SSI, addition, substitution and gzip may interact, so this lab exercises separate routes. Prefer application templates or a build step when composition requires complex business rules.
location = /card.html { root html; add_after_body /_footer; }location = /_footer { internal; default_type text/html; return 200 "<footer>Local lab</footer>"; }Practice and next steps
Practice: draw the page and fragment dependencies; explain why the direct fragment URL must return 404 and why a missing fragment needs a neutral fallback. Next: nginx event-driven architecture and process internals.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SSI and internal content boundaries
Explain dependencies and safe file scope
Where is an SSI include assembled?
What is the safest source for an SSI template?
Which SSI parameter can request waiting for an included request?
What is a suitable fallback for a missing public fragment?
Does internal guarantee that every subrequest is authorized?