📖 Scenario: You are setting up a simple web server using Nginx. To protect a specific area of your website, you want to add basic authentication so that only users with a username and password can access it.
🎯 Goal: Learn how to configure basic authentication in Nginx by creating a password file, setting the auth_basic_user_file directive, applying the authentication to a location block, and verifying the setup.
📋 What You'll Learn
Create a password file with a user entry
Add the auth_basic_user_file directive for the password file path
Configure Nginx to use basic authentication for a location
Print the final Nginx configuration snippet
💡 Why This Matters
🌍 Real World
Basic authentication is commonly used to protect admin pages or sensitive areas on websites without complex login systems.
💼 Career
Knowing how to configure basic authentication in Nginx is a key skill for DevOps engineers managing web servers and securing web applications.
Progress0 / 4 steps
1
Create the password file
Create a file called /etc/nginx/.htpasswd with the exact content user:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/ which represents the username user and a hashed password.
Nginx
Hint
This line is the content of the password file. It stores the username and hashed password for basic authentication.
2
Add the auth_basic_user_file directive
In your Nginx configuration, set the auth_basic_user_file directive to /etc/nginx/.htpasswd.
Nginx
Hint
This directive specifies the path to the password file for authentication.
3
Configure basic authentication in Nginx
Add a location /secure block in the Nginx configuration that enables basic authentication with the message "Restricted Area" and the auth_basic_user_file directive for the password file.
Nginx
Hint
The location block protects the /secure path with basic authentication.
4
Print the final Nginx configuration
Print the entire Nginx configuration snippet including the password file content, the variable, and the location block exactly as shown.
Nginx
Hint
Use a print statement to show the full configuration exactly as it is.
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
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 C
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
Step 1: Identify the directive for enabling basic auth
The directive auth_basic enables basic authentication and sets the login prompt message.
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.
Final Answer:
auth_basic -> Option D
Quick Check:
Enable basic auth = auth_basic [OK]
Hint: Enable basic auth with auth_basic directive [OK]