Serve Your First Static HTML Website with Nginx
Serve a file directly
A static website is a collection of files that Nginx can return without asking another application to generate a page. A document root is the directory from which the server finds those files. In a simple root configuration, the request path is appended to that directory. The following is a partial server block, not a standalone configuration.
server { listen 127.0.0.1:18085; server_name localhost; root html; location / { }}The lab uses the Nginx prefix as the base for the relative root html. A GET request for /index.html therefore looks for html/index.html under the private lab directory. The empty prefix location accepts ordinary paths; the standard static file handler can serve existing files. The listener binds to the IPv4 loopback address, so it is not intended for other machines. server_name identifies a virtual server; it does not create DNS records.
Create a page and its assets
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Field Notes</title> <link rel="stylesheet" href="/assets/site.css"> </head> <body> <main> <h1>Field Notes</h1> <img src="/assets/leaf.svg" alt="A simple green leaf" width="64" height="64"> <p>A small website served by Nginx.</p> </main> </body></html>The browser obtains the HTML first, then makes separate requests for the stylesheet and image referenced in it. The leading slash in each asset URL refers to the website root, not the operating system root. The supplied CSS changes spacing and colors; the SVG is a small leaf. The alternative text describes it for users who cannot see the image.
html/ index.html assets/ site.css leaf.svgbody { font: 1rem/1.6 system-ui, sans-serif; color: #173b2a; background: #f2f7f3; }main { max-width: 38rem; margin: 3rem auto; padding: 1rem; }A request for /assets/site.css maps to the stylesheet inside html/assets. Changing the URL to /site.css would instead look for a file directly under html. Query parameters such as ?rev=1 do not become part of this simple file path. Nginx processes a normalized URI, not a raw filename supplied by the browser.
Verify the complete lab
All commands in this chapter are optional, for the supplied Chapter 5 lab only. Use a reviewed patched Nginx build in an approved disposable Linux VM as a non-root user. Follow the lab README to copy the files, set LAB, prepare directories, confirm port availability and test the complete configuration. Results are predicted, not observed.
curl --noproxy '*' --max-time 2 -i http://127.0.0.1:18085/index.htmlcurl --noproxy '*' --max-time 2 -i http://127.0.0.1:18085/assets/site.cssExpect HTTP 200 for both and the actual HTML and CSS bodies, respectively. A successful response for the page alone does not establish that its assets work. In a browser inside the approved lab, the page should show the heading Field Notes, a green leaf and the short paragraph on a pale background.
Keep configuration, logs, backups and private files outside the document root. Do not point root at your home directory or the entire filesystem. Use only the synthetic lab files and do not place links to private files in the tree.
Practice: map three requests
Predict the filesystem path for /index.html, /assets/leaf.svg and /assets/missing.css using the supplied root. Explain why a working HTML response cannot prove the last two requests work. Next: index files and MIME types.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Static file mapping
Check the concepts taught in this lesson.
With root html, which path is used for /assets/leaf.svg?
What does listen 127.0.0.1:18085 select?
Why is a stylesheet usually requested separately?
Where should private configuration files be kept?
Which file is selected by /assets/site.css?rev=1 in the baseline?