Bird
Raised Fist0
Nginxdevops~20 mins

Contexts (main, events, http, server, location) in Nginx - Practice Problems & Coding Challenges

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
Challenge - 5 Problems
🎖️
Nginx Context Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Identify the correct context for worker_connections
In which Nginx configuration context should the directive worker_connections be placed to properly set the maximum number of simultaneous connections per worker process?
Alocation
Bhttp
Cserver
Devents
Attempts:
2 left
💡 Hint
Think about where Nginx manages connection handling globally.
💻 Command Output
intermediate
2:00remaining
Output of nginx -T with misplaced location block
What error message will Nginx produce if a location block is placed directly inside the main context (outside http) and you run nginx -T to test the configuration?
Nginx
location /test {
    proxy_pass http://localhost:8080;
}
Anginx: configuration file /etc/nginx/nginx.conf test is successful
Bnginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:2
Cnginx: [warn] unknown directive "location" in /etc/nginx/nginx.conf:2
Dnginx: [error] invalid parameter in location block in /etc/nginx/nginx.conf:2
Attempts:
2 left
💡 Hint
Consider which contexts allow location blocks.
🔀 Workflow
advanced
2:30remaining
Order of contexts for serving a request
Arrange the Nginx configuration contexts in the order they are processed when serving an HTTP request.
A2,1,3,4,5
B1,3,2,4,5
C1,2,3,4,5
D1,3,4,5,2
Attempts:
2 left
💡 Hint
Think about the global setup, connection handling, HTTP processing, and request routing.
Troubleshoot
advanced
2:00remaining
Why is proxy_pass ignored in main context?
You added the directive proxy_pass http://backend; directly inside the http context, but it has no effect on requests. Why?
A<code>proxy_pass</code> must be inside a <code>location</code> block, not directly in <code>http</code>
B<code>proxy_pass</code> is deprecated and ignored in all contexts
C<code>proxy_pass</code> only works inside <code>server</code> blocks, not <code>http</code>
D<code>proxy_pass</code> requires the <code>events</code> context to be enabled
Attempts:
2 left
💡 Hint
Where do you usually put routing rules?
Best Practice
expert
2:30remaining
Best context for gzip compression directives
Where should you place gzip compression directives like gzip on; and gzip_types for best practice and efficient configuration?
AInside the <code>http</code> context to apply globally to all servers
BInside each <code>server</code> block to customize per domain
CInside the <code>location</code> blocks to control compression per URI
DInside the <code>events</code> context because it handles connections
Attempts:
2 left
💡 Hint
Compression usually applies to all HTTP responses unless you want exceptions.

Practice

(1/5)
1. Which nginx context is used to define global settings that affect the entire nginx server?
easy
A. main
B. http
C. server
D. location

Solution

  1. Step 1: Understand nginx context scopes

    The main context is the top-level context for global settings.
  2. Step 2: Differentiate other contexts

    The http context is for HTTP-specific settings, server for virtual hosts, and location for URL matching.
  3. Final Answer:

    main -> Option A
  4. Quick Check:

    Global settings = main [OK]
Hint: Global settings go in main context only [OK]
Common Mistakes:
  • Confusing http with main context
  • Placing global settings inside server or location
  • Thinking events is for global settings
2. Which of the following is the correct way to nest the server context inside nginx configuration?
easy
A. main { server { ... } }
B. http { server { ... } }
C. events { server { ... } }
D. location { server { ... } }

Solution

  1. Step 1: Recall nginx context hierarchy

    The server context must be inside the http context.
  2. Step 2: Check each option's nesting

    Only http { server { ... } } is valid nesting; others are invalid.
  3. Final Answer:

    http { server { ... } } -> Option B
  4. Quick Check:

    server inside http = correct [OK]
Hint: Server blocks go inside http context [OK]
Common Mistakes:
  • Placing server inside main or events
  • Nesting server inside location
  • Confusing events with http context
3. Given this nginx snippet, what is the correct context for the listen 80; directive?
http {
  server {
    listen 80;
    location / {
      root /var/www/html;
    }
  }
}
medium
A. server
B. events
C. main
D. location

Solution

  1. Step 1: Identify where listen directive belongs

    The listen directive configures the port for a virtual host, which belongs in the server context.
  2. Step 2: Check the snippet structure

    In the snippet, listen 80; is inside server, which is correct.
  3. Final Answer:

    server -> Option A
  4. Quick Check:

    listen directive = server context [OK]
Hint: listen always goes inside server context [OK]
Common Mistakes:
  • Placing listen inside location
  • Thinking listen belongs in main or events
  • Confusing location with server context
4. You wrote this nginx config but nginx fails to start:
events {
  server {
    worker_connections 1024;
  }
}
What is the error?
medium
A. Missing http context around server
B. worker_connections must be inside server
C. events context cannot contain any directives
D. server context cannot be inside events

Solution

  1. Step 1: Understand events context usage

    The events context is for event-related directives only and cannot contain server blocks.
  2. Step 2: Identify invalid nesting

    Placing server inside events is invalid and causes nginx startup failure.
  3. Final Answer:

    server context cannot be inside events -> Option D
  4. Quick Check:

    server inside events = invalid [OK]
Hint: server blocks never go inside events context [OK]
Common Mistakes:
  • Putting server inside events
  • Misplacing worker_connections inside server
  • Confusing events with http context
5. You want to serve static files from /var/www/html only for URLs starting with /images/. Which context should you use to configure this in nginx?
hard
A. main
B. server
C. location
D. events

Solution

  1. Step 1: Identify context for URL matching

    The location context is used to match specific URL patterns like /images/.
  2. Step 2: Understand static file serving setup

    Inside location, you set the root directive to serve files from the desired folder.
  3. Final Answer:

    location -> Option C
  4. Quick Check:

    URL-specific config = location context [OK]
Hint: Use location for URL path specific settings [OK]
Common Mistakes:
  • Trying to serve files from main or events
  • Placing root directive in server without location
  • Confusing server and location contexts