Bird
Raised Fist0
Nginxdevops~20 mins

Basic authentication in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Basic Authentication Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this command to create a password file?
You run the command htpasswd -c /etc/nginx/.htpasswd user1 and enter the password pass123. What will be the content format of the file /etc/nginx/.htpasswd?
Nginx
htpasswd -c /etc/nginx/.htpasswd user1
AA file with the username and an encrypted password hash, e.g., user1:$apr1$randomhash
BA JSON file containing username and password fields
CA plain text file with the username and password separated by a colon, e.g., user1:pass123
DAn XML file with user credentials inside tags
Attempts:
2 left
💡 Hint
The password is not stored in plain text but hashed for security.
❓ Configuration
intermediate
2:00remaining
Which nginx configuration snippet correctly enables basic authentication for /admin path?
You want to protect the /admin URL path with basic authentication using the password file at /etc/nginx/.htpasswd. Which configuration snippet will work?
A
location /admin {
  auth_basic "Restricted Area";
  auth_basic_user_file /etc/nginx/.htpasswd;
  allow all;
}
B
location /admin {
  auth_basic "Restricted Area";
  auth_basic_user_file /etc/nginx/passwd;
}
C
location /admin {
  auth_basic "Restricted Area";
  auth_basic_user_file /etc/nginx/.htpasswd;
}
D
location /admin {
  auth_basic_user_file /etc/nginx/.htpasswd;
  auth_basic off;
}
Attempts:
2 left
💡 Hint
Check that auth_basic is enabled and the correct file path is used.
❓ Troubleshoot
advanced
2:00remaining
Why does nginx ignore basic authentication despite correct config?
You configured nginx with basic authentication for /secure path, but users can access it without a password prompt. What is the most likely cause?
AThe auth_basic directive is inside a location block but the location is overridden by another location without auth_basic.
BThe password file /etc/nginx/.htpasswd is empty.
CThe nginx service was restarted after configuration changes.
DThe client browser does not support basic authentication.
Attempts:
2 left
💡 Hint
Check if another location block matches the request without authentication.
🔀 Workflow
advanced
2:00remaining
What is the correct workflow to enable basic authentication on nginx?
Arrange the steps in the correct order to enable basic authentication on nginx for a website.
A2,1,3,4
B1,2,3,4
C1,3,2,4
D2,3,1,4
Attempts:
2 left
💡 Hint
Think about creating credentials before telling nginx to use them.
✅ Best Practice
expert
2:00remaining
Which practice improves security when using basic authentication with nginx?
Basic authentication sends credentials encoded but not encrypted. Which practice best improves security when using it?
ADisable logging in nginx to avoid recording authentication attempts.
BUse longer usernames and passwords in the htpasswd file.
CStore the password file in the web root directory for easy access.
DUse HTTPS (TLS) to encrypt the connection so credentials are protected in transit.
Attempts:
2 left
💡 Hint
Think about protecting data sent over the network.

Practice

(1/5)
1. What is the main purpose of basic authentication in nginx?
easy
A. To speed up the website loading time
B. To encrypt all website traffic automatically
C. To require users to enter a username and password before accessing content
D. To block all users except the server admin

Solution

  1. Step 1: Understand basic authentication purpose

    Basic authentication prompts users for a username and password to restrict access.
  2. 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.
  3. Final Answer:

    To require users to enter a username and password before accessing content -> Option C
  4. Quick Check:

    Basic authentication = login prompt [OK]
Hint: Basic auth means login prompt for access control [OK]
Common Mistakes:
  • Confusing basic auth with encryption
  • Thinking it speeds up the site
  • Assuming it blocks all users except admin
2. Which nginx directive is used to enable basic authentication in a server block?
easy
A. auth_basic_user_file
B. auth_user_file
C. basic_auth_enable
D. auth_basic

Solution

  1. Step 1: Identify the directive for enabling basic auth

    The directive auth_basic enables basic authentication and sets the login prompt message.
  2. Step 2: Check other directives

    auth_basic_user_file is incorrect; the correct directive for the password file is auth_basic_user_file but the enabling directive is auth_basic.
  3. Final Answer:

    auth_basic -> Option D
  4. Quick Check:

    Enable basic auth = auth_basic [OK]
Hint: Enable basic auth with auth_basic directive [OK]
Common Mistakes:
  • Using auth_basic_user_file to enable auth
  • Confusing directive names
  • Using non-existent directives
3. Given this nginx config snippet:
location /secure/ {
  auth_basic "Restricted Area";
  auth_basic_user_file /etc/nginx/.htpasswd;
}

What happens when a user tries to access /secure/?
medium
A. User is prompted for username and password
B. User is redirected to /etc/nginx/.htpasswd
C. User accesses content without any prompt
D. User gets a 404 Not Found error

Solution

  1. Step 1: Analyze auth_basic directive

    The auth_basic directive enables a login prompt with the message "Restricted Area".
  2. Step 2: Analyze auth_basic_user_file directive

    The auth_basic_user_file points to the password file used to verify credentials.
  3. Step 3: Determine user experience

    When accessing /secure/, the user is asked for username and password to proceed.
  4. Final Answer:

    User is prompted for username and password -> Option A
  5. Quick Check:

    auth_basic + user_file = login prompt [OK]
Hint: auth_basic triggers login prompt on matching location [OK]
Common Mistakes:
  • Thinking user is redirected to password file
  • Assuming no prompt appears
  • Expecting 404 error without reason
4. You configured basic authentication but users are not prompted for credentials. Which is the most likely error?
medium
A. Missing auth_basic directive in the location block
B. Password file has wrong permissions
C. Using HTTP instead of HTTPS
D. Incorrect server_name directive

Solution

  1. Step 1: Check required directives for basic auth

    Both auth_basic and auth_basic_user_file are needed; missing auth_basic disables the prompt.
  2. 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.
  3. Final Answer:

    Missing auth_basic directive in the location block -> Option A
  4. Quick Check:

    No auth_basic = no login prompt [OK]
Hint: Always include auth_basic to enable prompt [OK]
Common Mistakes:
  • Ignoring missing auth_basic directive
  • Blaming HTTPS for prompt absence
  • Confusing server_name with auth settings
5. You want to protect only the /admin path with basic authentication but allow /admin/public without login. Which nginx config achieves this?
hard
A. Use location /admin/public/ { auth_basic "Admin Area"; } and location /admin/ { auth_basic off; }
B. Use location /admin/ { auth_basic "Admin Area"; auth_basic_user_file /etc/nginx/.htpasswd; } and location /admin/public/ { auth_basic off; }
C. Use location /admin/ { auth_basic off; } only
D. Use location /admin/public/ { auth_basic_user_file /etc/nginx/.htpasswd; } only

Solution

  1. Step 1: Protect /admin path with basic auth

    Set auth_basic and auth_basic_user_file in location /admin/ to require login.
  2. Step 2: Disable auth for /admin/public

    Override with auth_basic off; in location /admin/public/ to allow open access.
  3. Step 3: Confirm correct order and specificity

    More specific location /admin/public/ disables auth, while parent /admin/ requires it.
  4. Final Answer:

    Use location /admin/ with auth_basic and location /admin/public/ with auth_basic off -> Option B
  5. Quick Check:

    Parent location auth + child location off = selective protection [OK]
Hint: Disable auth in child location with auth_basic off [OK]
Common Mistakes:
  • Reversing auth_basic on/off in locations
  • Not using auth_basic off to disable
  • Only configuring one location
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard