Bird
Raised Fist0
Nginxdevops~10 mins

Basic authentication in Nginx - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Basic authentication
Client sends HTTP request
↓
Nginx checks for Authorization header
↓
Decode credentials
↓
Verify username/password
↓
Allow
Nginx checks if the client sent credentials. If missing or wrong, it asks again. If correct, it allows access.
Execution Sample
Nginx
location /secure {
  auth_basic "Restricted Area";
  auth_basic_user_file /etc/nginx/.htpasswd;
}
This config protects /secure path with basic auth using credentials in .htpasswd file.
Process Table
StepClient RequestAuthorization HeaderActionNginx Response
1GET /secureNoneCheck header401 Unauthorized with WWW-Authenticate
2GET /secureBasic dXNlcjpwYXNzDecode and verifyCredentials valid?
3GET /secureBasic dXNlcjpwYXNzCredentials valid200 OK, allow access
4GET /secureBasic d3Jvbmc6Y3JlZA==Credentials invalid401 Unauthorized with WWW-Authenticate
💡 Execution stops when client sends valid credentials or after 401 response for invalid/missing credentials.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
Authorization HeaderNoneNoneBasic dXNlcjpwYXNzBasic dXNlcjpwYXNzBasic d3Jvbmc6Y3JlZA==
Credentials ValidN/AN/ATrueTrueFalse
Response CodeN/A401Checking200401
Key Moments - 3 Insights
Why does nginx respond with 401 Unauthorized when no Authorization header is sent?
Because nginx requires credentials for the protected path. Without the header, it sends 401 to ask the client to provide them (see execution_table step 1).
What happens if the credentials are invalid?
Nginx responds again with 401 Unauthorized, prompting the client to retry (see execution_table step 4).
How does nginx know if credentials are valid?
It decodes the Authorization header and checks against the .htpasswd file (see execution_table step 2 and variable_tracker Credentials Valid).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response does nginx send when the Authorization header is missing?
A403 Forbidden
B200 OK
C401 Unauthorized with WWW-Authenticate
D500 Internal Server Error
💡 Hint
Check execution_table row 1 under Nginx Response
At which step does nginx allow access to the client?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look for 200 OK response in execution_table
If the Authorization header changes to an invalid value, what response code will nginx send?
A200 OK
B401 Unauthorized
C404 Not Found
D302 Redirect
💡 Hint
See execution_table step 4 Response Code and variable_tracker Response Code after step 4
Concept Snapshot
Basic authentication in nginx:
- Use auth_basic and auth_basic_user_file in location block
- Client sends Authorization header with base64 user:pass
- Nginx checks header; if missing or invalid, sends 401 Unauthorized
- If valid, nginx allows access
- Credentials stored in .htpasswd file
Full Transcript
Basic authentication in nginx works by requiring clients to send a special header with username and password encoded. When a client requests a protected path, nginx looks for the Authorization header. If it is missing, nginx responds with 401 Unauthorized and a prompt to send credentials. When the client sends credentials, nginx decodes and checks them against a password file. If valid, nginx allows access with a 200 OK response. If invalid, nginx again responds with 401 Unauthorized. This process repeats until valid credentials are provided or the client stops trying.

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