Install the htpasswd tool
yum install httpd-tools -y
Copy the code
Create a user file and set the user name and password, and save the user name and password to the specified file:
[root@ ~]# cd /usr/local/nginx/conf
[root@ conf]# mkdir passwd
[root@ conf]# htpasswd -c /usr/local/nginx/conf/passwd/passwd test
New password:
Re-type new password:
Adding password for user test
Copy the code
Note: test is the username, you can set it to another username if you want. After running the command, you will be asked to enter your password twice. After the password is entered successfully, a message is displayed indicating that a password has been added for the test user. View the content of the generated password file:
[root@ conf]# cat passwd/passwd
test:$apr1$o6psZOCn$APl2zscV0JYwsR8Lol2tP1
Copy the code
The user name is test, followed by the password (already encrypted).
1. Add a user or change the password
Htpasswd - b/usr/local/nginx/conf/passwd/passwd username passwordCopy the code
2. Delete the user
Htpasswd -d/usr/local/nginx/conf/passwd/passwd usernameCopy the code
Modify the nginx configuration file
Find the nginx configuration file. Since we want to enable authentication for the entire site, the first server in the configuration file is modified as follows:
server { listen 80; server_name localhost; . Auth_basic "Please input password"; # this is a validation message auth_basic_user_file/usr/local/nginx/conf/passwd/passwd; # here is the password file, you can fill in the absolute path location /{....... }Copy the code
Then restart nginx:
service nginx restart
Copy the code
Once you’ve done all that, you can go back to your site and see a popup that requires authentication.