Nginx Multi-site Hosting: Design Two Sites and an API
Start with a small hosting design
One Nginx listener can serve several websites. In this project, Book Corner serves static pages and sends catalog requests to a small backend. Study Notes serves only static pages. Both use the same loopback address and port, but the client supplies different hostnames. The default server responds with 404 to unmatched names. No public DNS is required.
Client → 127.0.0.1:18131 (Nginx) books.test / → sites/books/index.html notes.test / → sites/notes/index.html books.test /api/books → 127.0.0.1:18132/books unknown.test / → default server: 404A document root is the directory from which a site resolves ordinary static paths. Keep unrelated sites in separate roots. The API is a different process and is not a directory inside the website. It accepts a fixed Host value, catalog.test, in this synthetic lab.
Organize the project files
chapter-12/ nginx.conf backend.py sites/ books/ index.html 404.html assets/site.css notes/ index.html assets/site.css expected-results.json verify_offline.py verify_live.pyThe two CSS files deliberately use different colors. If a request reaches the wrong root, the body or stylesheet marker exposes the mistake even if the status is 200. Keep logs, temporary files and the backend source outside document roots.
Select a host before selecting a path
curl --noproxy '*' --max-time 5 -H 'Host: books.test' http://127.0.0.1:18131/curl --noproxy '*' --max-time 5 -H 'Host: notes.test' http://127.0.0.1:18131/These commands are optional live-lab instructions, not observed outputs. The expected headings are Book Corner catalog and Study Notes home. Both use the same socket; Nginx chooses the virtual host from the request and then processes the path using that server configuration.
Define the acceptance criteria
books.test / 200 HTML Book Cornernotes.test / 200 HTML Study Notesbooks.test /api/books 200 JSON catalogunknown.test / 404notes.test /api/books 404Do not consider the project complete merely because one site loads. Check host separation, asset types, the backend path, missing routes and default-server behavior. Practice: sketch the request flow for each row and name which component produces the response. Next: assemble the configuration.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Multi-site architecture
Choose the best answer for the multi-site fixture.
Both sites use the same address and port. What distinguishes them?
Which process reads the catalog data for /api/books?
Why place backend.py outside the document roots?
What should unknown.test return in the fixture?
Which assertion best detects swapped document roots?