Build a Static Website and Reverse Proxy in One Nginx Config
Read the configuration from outside to inside
The full fixture is nginx.conf in lab-fixtures/chapter-12. Its main context sets a private PID and error log; events sets a modest worker connection limit. The http block contains MIME mappings, access logging and three virtual hosts. Relative paths resolve using the configured lab prefix; do not use these snippets as system configuration.
server { listen 127.0.0.1:18131 default_server; server_name default.test; return 404;}server { listen 127.0.0.1:18131; server_name notes.test; root sites/notes; index index.html; location / { try_files $uri $uri/ =404; }}The default belongs to the listening socket. The notes server uses its own files and never delegates requests to the catalog. For a request to /, index lookup can select index.html; a missing path is a 404.
Connect the books site to the API
server { listen 127.0.0.1:18131; server_name books.test; root sites/books; index index.html; error_page 404 /404.html; location = /404.html { internal; } location = /api { return 404; } location /api/ { proxy_pass http://127.0.0.1:18132/; proxy_http_version 1.1; proxy_set_header Host catalog.test; proxy_intercept_errors off; } location / { try_files $uri $uri/ =404; }}This excerpt omits the full fixture’s forwarding-header policy and timeouts. For /api/books, the proxy_pass URL includes a slash URI, replacing the matching /api/ prefix so the backend receives /books. The fixed upstream Host matches the toy application. API errors are passed through rather than replaced with the site’s HTML error page. The static custom error page retains the 404 status.
{ "books": [ "HTTP Basics", "Local Servers" ]}Use scoped logs
log_format milestone escape=json '{"site":"$server_name","method":"$request_method","uri":"$uri","status":$status,"upstream":"$upstream_status"}';access_log logs/access.log milestone;The site and status are useful for distinguishing hosts. The upstream field is meaningful for proxied traffic; a local file does not have an upstream response. The logged normalized URI may change during an internal redirect, so it is not always the original path. This synthetic log deliberately omits cookies, authorization and query strings; paths themselves may contain sensitive data in a real system.
Check outcomes rather than appearances
curl --noproxy '*' --max-time 5 -i -H 'Host: books.test' http://127.0.0.1:18131/api/nopecurl --noproxy '*' --max-time 5 -i -H 'Host: books.test' http://127.0.0.1:18131/missingBoth requests should return 404, but the first has JSON from the application and the second has HTML from Nginx’s custom error page. Inspect Content-Type and body. Practice: predict the eleven fixture cases before attempting the optional live test. Next: fix a configuration that passes syntax but serves the wrong files.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Integrated config and request behavior
Choose the best answer for the multi-site fixture.
What upstream path is expected for books.test/api/books?
What is the expected type of a successful site.css response?
Why should /api/nope return JSON rather than the custom HTML error page?
What is the expected status of /missing on books.test?
Why might a log record show /404.html instead of /missing?