0
0
Nginxdevops~5 mins

API routing with location blocks in Nginx - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a location block in nginx?
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
How do you define a location block that matches all URLs starting with /api/?
Alocation = /api/ {}
Blocation ^~ /api/ {}
Clocation ~ /api/ {}
Dlocation /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
Which directive specifies the port nginx listens on?
Aserver_name
Bproxy_pass
Clisten
Dlocation
If you want to route /api/users to a backend, which location block is best?
Alocation = /api/users {}
Blocation /api/ {}
Clocation / {}
Dlocation ~ /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.