Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 63

Nginx Configuration Blocks: HTTP, Server, and Location

Recommended resources

Hello! Welcome back to our course on Linux System Administration for Full-Stack Web Developers.

In our last lesson, you successfully installed Nginx on your Fedora system, configured the firewall, and confirmed that the server was running by viewing the default welcome page. This was the first critical step in setting up a production environment.

Today, we're going to look under the hood. The goal of this lesson is to understand how Nginx works by examining its configuration structure. Our learning outcome is to explore and identify key Nginx configuration blocks (http, server, location). Mastering this is fundamental to configuring Nginx to serve your Django projects, which is exactly where we're headed.

1. The Hierarchy of Nginx Configuration

Nginx's power and flexibility come from its structured configuration file. This file isn't just a list of settings; it's organized into a hierarchy of nested blocks, often called contexts. Each context groups directives (settings) that apply to a specific scope.

For our work as web developers, the three most important contexts are:

  • http: The top-level container for all web-related configurations.
  • server: Defines a specific website or "virtual host". You can have many server blocks to host multiple sites on one machine.
  • location: Defines how to handle requests for different URIs (e.g., /images/ or /api/) within a single website.

The structure is a tree: location blocks live inside server blocks, which live inside the http block.

To get a clear overview of this structure, let's start with a short article from DigitalOcean.

Understanding the Nginx Configuration File Structure

This article introduces the concept of Nginx configuration contexts and their tree-like structure. It provides an excellent mental model for how settings are organized and inherited.

Please read the introduction and the sections titled 'The Core Contexts', 'The HTTP Context', 'The Server Context', and 'The Location Context'. Focus on understanding the relationship between these blocks.

Now, let's supplement that reading with a video from the official Nginx team that visualizes these concepts.

Learn Proper NGINX Configuration Context Logic

This video from the official NGINX channel provides a concise explanation of the different configuration contexts and their purpose.

Watch from 02:59 to 06:37. The video will walk you through the main contexts and clarify the difference between a 'directive' (a single setting) and a 'block' or 'context' (a container for directives).


2. Finding the Configuration on Your Fedora System

Now that you have the theory, let's find these files on your system. This is where your preference for hands-on work comes in!

Open your terminal. The main Nginx configuration file is /etc/nginx/nginx.conf. Let's view it using less:

less /etc/nginx/nginx.conf

Scroll through this file. You will see the http block we just learned about. You'll also spot a very important line inside it:

include /etc/nginx/conf.d/*.conf;

This include directive tells Nginx to load all files ending with .conf from the /etc/nginx/conf.d/ directory as if they were part of the main file. This is how Fedora encourages modular configuration. Instead of putting everything in one giant file, you create separate files for each site in conf.d.

Now, let's look inside that directory:

ls /etc/nginx/conf.d/

You should see at least one file, likely default.conf or something similar, which controls the default welcome page you saw in the last lesson. Let's examine it:

less /etc/nginx/conf.d/default.conf

Your Task: Inside this file, can you identify:

  1. The server block?
  2. The listen directive? (It should be set to 80, the standard port for HTTP).
  3. The server_name directive? (It's probably localhost).
  4. A location / block?
  5. The root directive inside the location block? (It should point to /usr/share/nginx/html, which is where the welcome page lives).

This exercise directly connects the theory of blocks and directives to the working server on your machine.

An Important Note: Fedora vs. Ubuntu/Debian

You mentioned an interest in the differences between distributions. The way Nginx configuration is structured is a key difference between Fedora (a Red Hat-based system) and Debian/Ubuntu.

  • Fedora/Red Hat: Uses a main nginx.conf that includes all files from /etc/nginx/conf.d/. This is a simple and flat structure.
  • Debian/Ubuntu: Uses a more complex system with two directories: /etc/nginx/sites-available/ (where you create config files for all your sites) and /etc/nginx/sites-enabled/ (where you place symbolic links to the files in sites-available that you want to be active).

This video from KodeKloud explains the Debian/Ubuntu approach. Watching it will help you understand the other common convention you'll see in many online tutorials.

NGINX For Beginners | NGINX Tutorial | KodeKloud

This video section explains the directory structure commonly used on Debian and Ubuntu systems. Remember, your Fedora system is different, but it's valuable to know both approaches.

Watch from 33:15 to 36:55. Pay close attention to the description of sites-available and sites-enabled. The narrator explicitly mentions they are using Ubuntu, so keep in mind that on your Fedora system, you'll be working within /etc/nginx/conf.d/ instead.


3. Understanding Key Directives

You've now seen the blocks (http, server, location) and the directives inside them. Let's solidify what the most common directives do.

  • listen [port];: Tells the server block which TCP port to listen on for incoming connections. listen 80; is for standard HTTP.
  • server_name [domain_name];: Specifies which domain names this server block should respond to. Nginx checks the request's Host header to match it against a server_name. This is how you host multiple sites on one IP address.
  • root [path];: Defines the document root for the server or location. When a request comes in, Nginx combines the path from the root directive with the request URI to find the file on the server.
  • location [match] { ... }: Matches the request URI. A location / { ... } is the most general, catching any request that isn't caught by a more specific location block.

This next video segment provides a very clear breakdown of the server block and its essential directives.

NGINX For Beginners | NGINX Tutorial | KodeKloud

Let's return to the KodeKloud video for a detailed explanation of the server block. The analogy of a building and companies is particularly helpful for understanding virtual hosts.

Watch from 27:29 to 32:33. This part walks through a sample server block and explains the purpose of listen, server_name, root, and location in detail.

To see how these directives work together, let's look at one final, simple example from the official Nginx documentation.

Nginx Beginner's Guide: Serving Static Content

This guide provides a working example of a server configuration that serves static files from two different directories based on the request URI. It's a perfect illustration of how server and location blocks function.

Read the sections 'Configuration File’s Structure' and 'Serving Static Content'. You don't need to create the files or run the commands yourself; focus on understanding the logic of the example configuration provided.

In that example, a request for http://localhost/ would be handled by the location / block and map to /data/www/, while a request for http://localhost/images/photo.jpg would be matched by the location /images/ block and map to /data/images/photo.jpg. This demonstrates the power of the location block in routing requests.


Conclusion

Great job! You've successfully navigated the Nginx configuration and demystified how it works. This knowledge is the key to unlocking Nginx's potential as a web server and reverse proxy.

Key Takeaways:

  • Nginx configuration is hierarchical, using nested blocks called contexts.
  • The primary contexts for web serving are http, server, and location.
  • A server block defines a virtual host (a website), and location blocks within it handle different URI requests.
  • Key directives like listen, server_name, and root control how a server block responds to requests.
  • On Fedora, site configurations are typically placed in the /etc/nginx/conf.d/ directory, which is a simpler approach than the sites-available/sites-enabled system used by Debian/Ubuntu.

In our next lesson, we will put this knowledge into practice. You will configure Nginx to serve your Django project's static files. This will be our first step in creating a complete, production-style deployment for your web application.