Map URLs to the Right Static Files

Understand root, directory indexes, and file permissions so URLs map to the correct files on disk.

🎯 Introduction

With the home page working, you can add stylesheets and downloadable files. This article explains exactly which file Nginx reads for a requested path, so you can investigate a 404 without guessing at configuration changes.


🧱 Derive the File Path from the URL

Keep root html; and try_files $uri $uri/ =404; from the previous articles. The exercise instance's prefix is $HOME/nginx-course/, so:

Request pathFile or directory to look for
/index.html$HOME/nginx-course/html/index.html
/assets/site.css$HOME/nginx-course/html/assets/site.css
/chapter/The index file inside $HOME/nginx-course/html/chapter/

The key rule for root is root directory plus request path. Setting root html; inside location /assets/ does not strip /assets/: Nginx still looks for html/assets/.... See the core module's root and alias documentation for the precise rules.

index index.html; looks for an index file when a directory is requested. If found, Nginx internally processes the corresponding file path. A missing index does not mean Nginx automatically lists the directory. Directory listings remain disabled in this course.


🛠 Style the Study Notes Site

Run vim "$HOME/nginx-course/html/assets/site.css" and save:

html/assets/site.css
body {
    max-width: 48rem;
    margin: 2rem auto;
    padding: 0 1rem;
    font-family: sans-serif;
    line-height: 1.8;
}

Add this line inside the existing HTML's <head>:

<link rel="stylesheet" href="/assets/site.css">

After reading the HTML, the browser makes another request for the CSS URL in href. If text appears without styling, inspect that CSS request:

curl -i http://127.0.0.1:8080/assets/site.css

Expect status 200, content type text/css, and a body containing the CSS you just wrote. The Content-Type response header tells the browser how to interpret the content. Sending CSS as plain text may prevent the browser from applying it.


🧰 Map Another Directory with alias

Suppose you want to keep downloads outside html. First run:

mkdir -p "$HOME/nginx-course/downloads"
printf 'Nginx learning checklist\n' > "$HOME/nginx-course/downloads/checklist.txt"

printf outputs text; > writes that output to a file, overwriting an existing file with the same name. Add this inside the server block in site.conf:

location /downloads/ {
    alias /home/your-username/nginx-course/downloads/;
}

Run echo "$HOME" to find your home directory, replace the placeholder above with your own absolute path, then check and reload. Nginx does not expand Bash's $HOME, so do not put it directly in the configuration.

alias replaces the matched path prefix with the target directory: /downloads/checklist.txt maps to checklist.txt under downloads/. Both location and alias end in / here to avoid joining the path incorrectly. This is a separate mapping exercise. You may remove the block after verifying it; subsequent exercises depend only on html.


🪤 Why an Existing File Might Still Be Unreadable

The worker must be able to read the file and traverse every directory leading to it. You started this separate instance yourself and keep the pages in your own directory, so permission changes are usually unnecessary.

A system service may run under another account; do not assume that account can enter your home directory. Do not use chmod -R 777 as a fix, because it lets everyone modify the files. First use the error log to distinguish a missing file from insufficient permissions, then adjust the relevant account and directory permissions.


❓ Questions and Answers

Q1: Are root and alias interchangeable?

No. The former preserves the full request path; the latter replaces the matched prefix. Work out the target file path by hand before choosing one.

Q2: Do CSS changes require a reload?

Usually not. Changes to the file mapping configuration do require a check and reload.


🧪 Exercise

Create html/chapter/index.html containing “Chapter One”. Request /chapter/, then /chapter/missing.txt.

Check your result: the first returns 200 and “Chapter One”; the second returns 404. Write the full disk path corresponding to each request.


🎉 You can now map each static resource URL to a specific location on disk!