Sub topic 8
Serving Static Files with Nginx
Prerequisites and request flow
This chapter assumes that you can use the Linux command line, edit files with sudo, understand basic HTTP requests, and resolve DNS names. You should also know that TCP port 80 normally carries HTTP traffic.
Nginx serves static files directly instead of passing them to an application. Static files include HTML, CSS, JavaScript, images, fonts, and downloadable documents. A client requests a URL, Nginx selects a server block, matches a location, maps the URL to a path under root, and returns the file.
Create the document root
A document root is the directory from which Nginx serves files. Create a small test site and give the Nginx worker process permission to read it.
sudo mkdir -p /var/www/example.com/html
sudo tee /var/www/example.com/html/index.html >/dev/null <<'EOF'
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Example site</title>
</head>
<body>
<h1>It works</h1>
<p>This page is served by Nginx.</p>
</body>
</html>
EOF
sudo chown -R root:root /var/www/example.com
sudo find /var/www/example.com -type d -exec chmod 755 {} \;
sudo find /var/www/example.com -type f -exec chmod 644 {} \;Files need read permission, and every parent directory needs execute permission so Nginx can traverse the path. Avoid making the document root world-writable. An attacker who can modify web files can usually change what visitors receive.
Configure a server block
A server block is Nginx's virtual-host configuration. This example accepts two host names and serves files from the document root.
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}On Debian and Ubuntu, save this as /etc/nginx/sites-available/example.com, then enable it:
sudo ln -s /etc/nginx/sites-available/example.com \
/etc/nginx/sites-enabled/example.com
sudo nginx -t
sudo systemctl reload nginxtry_files checks whether the requested path exists. $uri represents the request URI. $uri/ permits a directory request, which can lead to the configured index file. If neither exists, =404 returns a not-found response instead of allowing unexpected fallback behavior.
The server_name must match the host in the request. For local testing, add the name to /etc/hosts, or send the host explicitly:
curl -i -H 'Host: example.com' http://127.0.0.1/
curl -i -H 'Host: example.com' http://127.0.0.1/missing.htmlThe first request should return 200 OK and the HTML page. The second should return 404 Not Found.
Common errors
- A
403 Forbiddenresponse often means that a directory lacks traversal permission, or that directory listing is disabled and no index file exists. - A
404 Not Foundresponse may mean that therootpath is wrong, the file is absent, or another server block handled the request. - If the default Nginx page appears, the request probably matched the distribution's default server block.
- If a configuration edit has no effect, run
nginx -t, reload the service, and confirm the request's host name.
Exercise
What file will Nginx try to serve for GET /images/logo.png when the server block has root /var/www/example.com/html;?
Show answer
It will try /var/www/example.com/html/images/logo.png. If that file does not exist, the try_files rule returns 404.