Bird
Raised Fist0
Nginxdevops~5 mins

Directives and blocks in Nginx - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Nginx uses directives and blocks to control how it handles web traffic. Directives set simple rules, while blocks group related directives together to organize configuration clearly.
When you want to set a server's listening port and domain name.
When you need to define how to handle requests for different website paths.
When you want to group settings for a website inside a server block.
When you want to control caching or security settings for a specific location.
When you want to organize your configuration for easier reading and maintenance.
Config File - nginx.conf
nginx.conf
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/html;
            index index.html index.htm;
        }
    }
}

worker_processes sets how many worker processes Nginx uses.

events block configures connection handling.

http block contains web server settings.

server block defines a website with its domain and port.

location block inside server sets rules for specific URL paths.

Commands
This command tests the Nginx configuration file for syntax errors before applying changes.
Terminal
sudo nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
This command reloads Nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command checks the HTTP headers returned by the server to verify it is responding correctly.
Terminal
curl -I http://example.com
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 612 Connection: keep-alive
-I - Fetch only HTTP headers without the body
Key Concept

If you remember nothing else from this pattern, remember: directives set individual rules, and blocks group these rules to organize Nginx configuration clearly.

Common Mistakes
Placing directives outside of any block when they must be inside one.
Nginx will fail to start or ignore those directives because they are not in the correct context.
Always place directives inside the appropriate block, like 'http', 'server', or 'location'.
Forgetting to test the configuration with 'nginx -t' before reloading.
Reloading with syntax errors causes Nginx to fail or keep running old config without changes.
Run 'sudo nginx -t' to check syntax before reloading.
Summary
Use directives to set simple configuration rules in Nginx.
Use blocks to group related directives for servers and locations.
Always test your configuration with 'nginx -t' before reloading Nginx.

Practice

(1/5)
1. What is the main difference between a directive and a block in an nginx configuration?
easy
A. A directive groups multiple blocks; a block is a single instruction ending with a semicolon.
B. A directive is a single instruction ending with a semicolon; a block groups multiple directives inside curly braces.
C. A directive is used only for server settings; a block is used only for location settings.
D. A directive must always contain a block inside it.

Solution

  1. Step 1: Understand directive syntax

    A directive is a single instruction that ends with a semicolon in nginx configuration.
  2. Step 2: Understand block syntax

    A block groups multiple directives inside curly braces to organize related settings.
  3. Final Answer:

    A directive is a single instruction ending with a semicolon; a block groups multiple directives inside curly braces. -> Option B
  4. Quick Check:

    Directive = single instruction; Block = group of directives [OK]
Hint: Directives end with ; blocks use { } to group [OK]
Common Mistakes:
  • Confusing directives with blocks
  • Thinking blocks end with semicolon
  • Assuming directives can contain blocks
2. Which of the following is the correct syntax for an nginx directive?
easy
A. listen 80;
B. server { listen 80 }
C. location / { listen 80 }
D. listen 80

Solution

  1. Step 1: Identify directive syntax

    A directive must end with a semicolon and is a single instruction.
  2. Step 2: Check each option

    listen 80; ends with a semicolon and is a single instruction: listen 80;.
  3. Final Answer:

    listen 80; -> Option A
  4. Quick Check:

    Directive ends with ; [OK]
Hint: Directives always end with a semicolon ; [OK]
Common Mistakes:
  • Omitting semicolon at end
  • Using curly braces for directives
  • Mixing block syntax with directive
3. Given this nginx configuration snippet, what will happen when a request is made to /images?
location /images/ {
    root /data;
    autoindex on;
}
medium
A. Nginx will return a 404 error because root is incorrectly used.
B. Nginx will serve files from /images/ directory on the server root.
C. Nginx will redirect requests to /data/images/ automatically.
D. Nginx will serve files from /data/images/ and show a directory listing if no index file exists.

Solution

  1. Step 1: Understand the location block

    The location block matches requests starting with /images/.
  2. Step 2: Interpret the root directive

    Root sets the base directory to /data, so files are served from /data/images/.
  3. Step 3: Effect of autoindex on

    If no index file exists, nginx shows a directory listing.
  4. Final Answer:

    Nginx will serve files from /data/images/ and show a directory listing if no index file exists. -> Option D
  5. Quick Check:

    location + root + autoindex = serve files with listing [OK]
Hint: root sets base path; autoindex shows directory listing [OK]
Common Mistakes:
  • Confusing root with alias
  • Assuming redirect happens automatically
  • Ignoring autoindex effect
4. Identify the error in this nginx configuration snippet:
server {
    listen 80
    server_name example.com;
}
medium
A. listen directive should be inside location block.
B. server_name directive cannot be inside server block.
C. Missing semicolon after listen 80 directive.
D. Curly braces are missing around listen directive.

Solution

  1. Step 1: Check syntax of directives

    Each directive must end with a semicolon in nginx configuration.
  2. Step 2: Locate missing semicolon

    The listen 80 directive is missing a semicolon at the end.
  3. Final Answer:

    Missing semicolon after listen 80 directive. -> Option C
  4. Quick Check:

    Every directive ends with ; [OK]
Hint: Check every directive ends with ; [OK]
Common Mistakes:
  • Forgetting semicolon at directive end
  • Misplacing directives outside server block
  • Adding unnecessary braces
5. You want to configure nginx to serve static files from /var/www/html for all requests under /static/. Which configuration block correctly achieves this?
hard
A. location /static/ { alias /var/www/html/; }
B. location /static { alias /var/www/html; }
C. location /static/ { root /var/www/html; }
D. location /static/ { root /var/www/html/; }

Solution

  1. Step 1: Understand root vs alias

    Root appends the request URI to the root path; alias replaces the location prefix with the alias path.
  2. Step 2: Match location and alias usage

    For prefix locations ending with /, alias must end with / to correctly map paths.
  3. Step 3: Evaluate options

    location /static/ { alias /var/www/html/; } uses location /static/ { alias /var/www/html/; } which correctly serves files under /static/ from /var/www/html.
  4. Final Answer:

    location /static/ { alias /var/www/html/; } -> Option A
  5. Quick Check:

    Use alias with trailing slash for prefix location [OK]
Hint: Use alias with trailing slash for prefix locations [OK]
Common Mistakes:
  • Using root instead of alias for prefix paths
  • Missing trailing slash on alias path
  • Mismatching location and alias slashes