0
0
Nginxdevops~5 mins

Preferential prefix match (^~) in Nginx - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the ^~ prefix mean in an nginx location block?
It tells nginx to do a preferential prefix match. If this prefix matches, nginx stops searching further and uses this location block.
Click to reveal answer
intermediate
How does ^~ differ from a normal prefix match in nginx?
Normal prefix matches are checked before regex matches, but regex can override them. ^~ stops regex checks if matched, giving it higher priority.
Click to reveal answer
intermediate
Which location block will nginx choose if both ^~ /images/ and ~* \.jpg$ match a request for /images/pic.jpg?
nginx will choose the ^~ /images/ block because ^~ stops regex checks when matched.
Click to reveal answer
beginner
Write a simple nginx location block using ^~ to serve all requests starting with /static/ from a specific folder.
location ^~ /static/ { root /var/www/static_files; }
Click to reveal answer
intermediate
Why would you use ^~ instead of a regex location in nginx?
Because ^~ is faster and simpler for prefix matching, and it avoids regex overhead when you want to prioritize a path prefix.
Click to reveal answer
What happens when nginx finds a ^~ prefix match?
AIt ignores the prefix and uses the default location.
BIt stops searching and uses that location block.
CIt continues to check regex locations.
DIt returns a 404 error.
Which location block has higher priority in nginx?
Alocation ^~ /images/
Blocation /images/
Clocation ~* \.jpg$
Dlocation = /images/
If no ^~ prefix matches, what does nginx do next?
AStops processing.
BReturns 404 error.
CChecks regex locations.
DUses the default location.
Which is true about location ^~ /api/?
AIt ignores requests starting with /api/.
BIt only matches exact /api/ requests.
CIt matches regex patterns inside /api/.
DIt matches any request starting with /api/ and stops regex checks.
Why is ^~ preferred over regex for simple prefix matching?
AIt is faster and simpler.
BIt supports complex patterns.
CIt always returns 404.
DIt disables caching.
Explain how nginx processes location blocks when a request URL matches multiple patterns including a ^~ prefix.
Think about the order nginx uses to find the best location.
You got /5 concepts.
    Describe a scenario where using ^~ in nginx location blocks improves performance or behavior.
    Consider static file serving or common URL prefixes.
    You got /4 concepts.