Chapter

Serving static content

Serve HTML, CSS, images and files with nginx: server blocks, listen and server_name, root and index, a first location, MIME types, custom error pages and directory listings.

Nginx Basics 9 Lessons from courses

About this chapter

This chapter turns nginx into a working file server. You will build a server block, point it at a folder with root and index, host several domains from one nginx, add your first location, learn how MIME types set the Content-Type, serve custom 404 and 50x pages, and switch on a directory listing with autoindex.

Lessons from courses

  1. 1 Server blocks What an nginx server block is, why Apache calls it a virtual host, and how to write a minimal server { } block that serves one site.
  2. 2 listen and server_name How nginx server_name matches the domain and listen sets the port, so one nginx hosts several sites, plus the default_server catch-all.
  3. 3 root and index How the nginx root directive points at your files and index sets the default page, and exactly how a URL path becomes a real file on disk.
  4. 4 location basics The nginx location block for beginners: what a prefix location matches, why you would use one, and how it changes rules for part of a URL.
  5. 5 MIME types How nginx MIME types work: mime.types sets the Content-Type header so browsers know what they got, plus default_type for unknown files.
  6. 6 Custom error pages Set up nginx custom error pages with error_page: your own 404 and 50x HTML, plus an internal location so the file can't be opened directly.
  7. 7 autoindex Turn on nginx autoindex to show a directory listing when a folder has no index file, when it is genuinely useful, and why to keep it off.
  8. 8 alias: serve a directory under a different URL Nginx alias vs root explained: map any folder to a URL path, the difference from root, the trailing-slash rule, and the common doubled-path 404.
  9. 9 Serving on a custom port Make nginx serve a site on a custom port instead of 80, why the port shows in the URL, the firewall step, and how it differs from a reverse proxy.
Nginx Basics Go to course