Beginner · 10 min · practise every command in the built-in terminal

Serving Your Own Pages

The default site's configuration is in /etc/nginx/sites-available/default:

cat /etc/nginx/sites-available/default

Two lines matter most:

root /var/www/html;
index index.html index.htm index.nginx-debian.html;

root is the folder nginx serves; a request for /about.html becomes the file /var/www/html/about.html. For a folder (/), nginx looks for the index files in that order — right now only index.nginx-debian.html, the welcome page, exists.

Put up your own page

/var/www/html belongs to root, so write into it with sudo tee (a plain sudo echo ... > file wouldn't work — the > is done by your shell, as you):

ls -l /var/www/html
echo '<h1>Hello from my server</h1>' | sudo tee /var/www/html/index.html
curl localhost

index.html comes before the welcome page in the index list, so it's served now. More pages are more files:

echo '<h1>About us</h1>' | sudo tee /var/www/html/about.html
curl localhost/about.html

When it goes wrong

StatusMeaning
200 OKhere's the page
301 Moved Permanentlyit's somewhere else (/blog → /blog/)
403 Forbiddenit's there, but nginx may not show it
404 Not Foundthere's no such file
curl -I localhost/missing.html                  # 404
echo secret | sudo tee /var/www/html/secret.html
sudo chmod 600 /var/www/html/secret.html
curl -I localhost/secret.html                   # 403

403 happens because nginx reads files as www-data: a file only root may read (600) is off limits. Pages need to be readable by others — chmod 644, and folders 755.

💡 Every request is written to /var/log/nginx/access.log, and the reason for a 403 or 404 to /var/log/nginx/error.log.

Practice tasks

  1. Put a page saying Hello from my server in /var/www/html/index.html
  2. Fetch your page with curl
  3. Ask for a page that doesn't exist and see the 404
  4. Make a page nginx isn't allowed to read and see 403 Forbidden

Commands in this lesson

Web Server (nginx)