Sub topic 3
Nginx Basics: Server Blocks, Root, and Locations
This chapter assumes you know basic Linux commands and file permissions, can edit files on a Debian or Ubuntu server, and understand HTTP requests, responses, status codes, and headers. You should also have a small PHP or Python application available for later chapters.
Nginx is a web server. It accepts HTTP requests, chooses a configuration, and returns a response. A configuration usually maps a hostname and URL path to a file or an application server.
How Nginx chooses a configuration
A server block describes one virtual host. A virtual host is a website configuration selected by the requested hostname, such as example.com.
The listen directive selects the local port. The server_name directive selects the hostname. The root directive sets the base directory used when Nginx looks for files.
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example/public;
index index.html index.php;
location / {
try_files $uri $uri/ =404;
}
}For a request such as GET /images/logo.png, Nginx combines the root directory with the request path:
/var/www/example/public + /images/logo.png
The resulting file path is /var/www/example/public/images/logo.png.
The index directive lists files Nginx may use when the request points to a directory. For example, / may resolve to /var/www/example/public/index.html.
Location matching
A location block controls requests whose paths match a pattern. The most common form is a prefix location.
location /assets/ {
root /var/www/example/public;
}
location / {
try_files $uri $uri/ =404;
}The /assets/ block is more specific than /, so a request for /assets/app.css uses the first block.
Regular-expression locations use ~ or ~*. The first form is case-sensitive. The second is case-insensitive.
| Location form | Example | Typical use |
|---|---|---|
| Prefix | `location /api/` | Match a path prefix |
| Exact | `location = /health` | Match one exact path |
| Regular expression | `location ~ \.php$` | Match a filename pattern |
Nginx has detailed matching rules, especially when exact, prefix, and regular-expression locations overlap. Prefer simple, non-overlapping locations until you need more control.
Request flow
Common errors
A 404 Not Found usually means the selected path does not contain the requested file. Check the configured root, the request URI, and the file name.
A 403 Forbidden often means the file exists but Nginx cannot read it, or a directory cannot be listed. Check permissions on the file and every parent directory.
A common mistake is placing the wrong directory in root. The root should normally point to the directory intended to be public, not the whole application directory. Keeping private configuration and source files outside that directory reduces accidental exposure.
Validate configuration before reloading Nginx:
sudo nginx -t
sudo systemctl reload nginx
curl -i http://example.com/assets/app.cssThe test command reports syntax errors without changing the running configuration. Reload applies a valid configuration while allowing existing connections to finish.
Exercise
A request for /images/logo.png returns 404. The file is stored at /var/www/example/public/images/logo.png. What should you check first?
Show answer
Check that the selected server block has root /var/www/example/public, that the URI is /images/logo.png, and that the file name and capitalization match.