A location block defines how nginx should respond to requests for specific URL paths. It helps route requests to different parts of your server or backend services.
Click to reveal answer
beginner
How do you route API requests starting with /api/ to a backend server in nginx?
Use a location /api/ { proxy_pass http://backend_server; } block to forward all requests starting with /api/ to the backend server.
Click to reveal answer
beginner
What does the proxy_pass directive do inside a location block?
It tells nginx to forward the incoming request to another server or service, acting like a middleman between the client and backend.
Click to reveal answer
intermediate
How can you match exact URLs versus URL prefixes in nginx location blocks?
Use = /exact/path for exact matches and /prefix/ for prefix matches. Exact matches take priority over prefix matches.
Click to reveal answer
intermediate
Why is the order of location blocks important in nginx?
nginx chooses the best matching location block based on specific rules. Exact matches are checked first, then prefix matches. Order affects which block handles a request.
Click to reveal answer
Which nginx directive forwards API requests to a backend server?
Aproxy_pass
Blisten
Cserver_name
Droot
✗ Incorrect
The proxy_pass directive forwards requests to another server.
How do you define a location block that matches all URLs starting with /api/?
Alocation = /api/ {}
Blocation ^~ /api/ {}
Clocation ~ /api/ {}
Dlocation /api/ {}
✗ Incorrect
A simple prefix match uses location /api/ {}.
What happens if you have both location = /api and location /api/ blocks?
AThe exact match is used
Bnginx throws an error
CThe prefix match is used
DBoth blocks run
✗ Incorrect
Exact matches take priority over prefix matches.
Which directive specifies the port nginx listens on?
Aserver_name
Bproxy_pass
Clisten
Dlocation
✗ Incorrect
The listen directive sets the port for nginx.
If you want to route /api/users to a backend, which location block is best?
Alocation = /api/users {}
Blocation /api/ {}
Clocation / {}
Dlocation ~ /users {}
✗ Incorrect
The prefix match /api/ covers /api/users.
Explain how nginx uses location blocks to route API requests.
Think about how nginx decides which block handles a request.
You got /4 concepts.
Describe how to configure nginx to send all requests starting with /api/ to a backend server.
Focus on the location block and proxy_pass.
You got /3 concepts.
Practice
(1/5)
1. What does a location block in an nginx configuration do?
easy
A. Sets the maximum file upload size
B. Defines server IP addresses
C. Specifies the server's operating system
D. Routes incoming requests based on URL paths
Solution
Step 1: Understand the purpose of location blocks
Location blocks in nginx are used to match parts of the URL path to decide how to handle requests.
Step 2: Identify what location blocks control
They control routing of requests to different handlers or backends based on the URL.
Final Answer:
Routes incoming requests based on URL paths -> Option D
Quick Check:
Location blocks = URL routing [OK]
Hint: Location blocks match URL paths to route requests [OK]
Common Mistakes:
Confusing location blocks with server IP settings
Thinking location blocks set server OS
Assuming location blocks control upload size
2. Which of the following is the correct syntax to define a location block that matches exactly the path /api/v1/users?
easy
A. location = /api/v1/users { ... }
B. location /api/v1/users { ... }
C. location ~ /api/v1/users { ... }
D. location ^~ /api/v1/users { ... }
Solution
Step 1: Recall nginx location modifiers
The = modifier means exact match of the URL path.
Step 2: Match the exact path syntax
Using location = /api/v1/users matches only that exact path.
Final Answer:
location = /api/v1/users { ... } -> Option A
Quick Check:
Exact match uses = modifier [OK]
Hint: Use '=' for exact path match in location [OK]
A. Missing trailing slash in location path causes wrong proxying
B. location path should end with a slash to match subpaths
C. proxy_pass URL must not have a trailing slash
D. No error; configuration is correct
Solution
Step 1: Check location path matching behavior
Without a trailing slash, location /api matches only the exact path /api and not subpaths like /api/users.
Step 2: Correct usage for subpath matching
To match /api and all subpaths, use location /api/ with trailing slash.
Final Answer:
location path should end with a slash to match subpaths -> Option B
Quick Check:
Trailing slash in location matches subpaths [OK]
Hint: Add trailing slash in location to match subpaths [OK]
Common Mistakes:
Thinking proxy_pass trailing slash is wrong here
Assuming location without slash matches subpaths
Believing config is error-free without checking path
5. You want to route all requests starting with /api/v2/ to http://v2-backend/ but requests exactly to /api/v2/status should return a static JSON response locally. Which nginx config achieves this?