Basic authentication in Nginx - Time & Space Complexity
We want to understand how the time needed to check user access grows as more users try to log in.
Specifically, how does nginx handle basic authentication when many requests come in?
Analyze the time complexity of the following nginx configuration snippet for basic authentication.
location /secure/ {
auth_basic "Restricted Content";
auth_basic_user_file /etc/nginx/.htpasswd;
}
This snippet protects the /secure/ path by requiring a username and password checked against a file.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: nginx reads the password file and compares the provided credentials to entries.
- How many times: For each request, nginx checks the credentials by scanning the file entries until a match is found or file ends.
As the number of users in the password file grows, nginx must check more entries to find a match.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 10 comparisons |
| 100 | Up to 100 comparisons |
| 1000 | Up to 1000 comparisons |
Pattern observation: The number of checks grows roughly in direct proportion to the number of users in the file.
Time Complexity: O(n)
This means the time to verify credentials grows linearly with the number of users stored in the password file.
[X] Wrong: "Checking credentials is always instant no matter how many users are in the file."
[OK] Correct: nginx must scan the file entries one by one until it finds a match, so more users mean more checks and longer time.
Understanding how authentication scales helps you explain real-world server behavior and shows you can think about performance in practical setups.
"What if the password file was replaced by a database lookup? How would the time complexity change?"
Practice
Solution
Step 1: Understand basic authentication purpose
Basic authentication prompts users for a username and password to restrict access.Step 2: Compare options
Only To require users to enter a username and password before accessing content describes this login prompt function; others describe unrelated features.Final Answer:
To require users to enter a username and password before accessing content -> Option CQuick Check:
Basic authentication = login prompt [OK]
- Confusing basic auth with encryption
- Thinking it speeds up the site
- Assuming it blocks all users except admin
Solution
Step 1: Identify the directive for enabling basic auth
The directiveauth_basicenables basic authentication and sets the login prompt message.Step 2: Check other directives
auth_basic_user_fileis incorrect; the correct directive for the password file isauth_basic_user_filebut the enabling directive isauth_basic.Final Answer:
auth_basic -> Option DQuick Check:
Enable basic auth = auth_basic [OK]
- Using auth_basic_user_file to enable auth
- Confusing directive names
- Using non-existent directives
location /secure/ {
auth_basic "Restricted Area";
auth_basic_user_file /etc/nginx/.htpasswd;
}What happens when a user tries to access
/secure/?Solution
Step 1: Analyze auth_basic directive
Theauth_basicdirective enables a login prompt with the message "Restricted Area".Step 2: Analyze auth_basic_user_file directive
Theauth_basic_user_filepoints to the password file used to verify credentials.Step 3: Determine user experience
When accessing/secure/, the user is asked for username and password to proceed.Final Answer:
User is prompted for username and password -> Option AQuick Check:
auth_basic + user_file = login prompt [OK]
- Thinking user is redirected to password file
- Assuming no prompt appears
- Expecting 404 error without reason
Solution
Step 1: Check required directives for basic auth
Bothauth_basicandauth_basic_user_fileare needed; missingauth_basicdisables the prompt.Step 2: Evaluate other options
Wrong permissions cause errors but usually show 500 errors; HTTP vs HTTPS affects security but not prompt; server_name unrelated.Final Answer:
Missing auth_basic directive in the location block -> Option AQuick Check:
No auth_basic = no login prompt [OK]
- Ignoring missing auth_basic directive
- Blaming HTTPS for prompt absence
- Confusing server_name with auth settings
/admin path with basic authentication but allow /admin/public without login. Which nginx config achieves this?Solution
Step 1: Protect /admin path with basic auth
Setauth_basicandauth_basic_user_fileinlocation /admin/to require login.Step 2: Disable auth for /admin/public
Override withauth_basic off;inlocation /admin/public/to allow open access.Step 3: Confirm correct order and specificity
More specific location/admin/public/disables auth, while parent/admin/requires it.Final Answer:
Use location /admin/ with auth_basic and location /admin/public/ with auth_basic off -> Option BQuick Check:
Parent location auth + child location off = selective protection [OK]
- Reversing auth_basic on/off in locations
- Not using auth_basic off to disable
- Only configuring one location
Ready to prove it?
Pass the test and this pattern moves unknown
