Nginx can be done by simply setting auth_basic module to support basic authentication of HTTP, this article will explain with examples specific use.
Preparing the Environment
[root@liumiaocn ~]# nginx -v
nginx version: nginx/1.16.1
[root@liumiaocn ~]#
[root@liumiaocn ~]# systemctl status nginx |grep Active
Active: active (running) since Wed 2020-01-29 00:52:54 EST; 3min 8s ago
[root@liumiaocn ~]#
Step 1: Prepare the required password file authentication
Generates a file named passwd in /etc/nginx/conf.d
[root@liumiaocn conf.d]# pwd
/etc/nginx/conf.d
[root@liumiaocn conf.d]# ls
[root@liumiaocn conf.d]#
Generated content using the following format openssl command (htpasswd command may be used to generate, to the same format)
User Information Format: Username: Password
Note: after the user password is encrypted manner, as shown in the example log
[root@liumiaocn conf.d]# echo "liumiaocn:"$(openssl passwd -crypt liumiao) >passwd
[root@liumiaocn conf.d]# cat passwd
liumiaocn:9Rz6nhLoBkXIU
[root@liumiaocn conf.d]#
In this way, the user name liumiaocn, user password authentication file liumiao's ready to go. Also, note that the immediate onset of this file, you can save multiple rows, each row corresponds to a user's HTTP basic authentication information.
Step 2: Modify location segment
Add the following paragraph at the location:
auth_basic "Login Auth";
auth_basic_user_file conf.d/passwd;
Step 3: Re-load configuration file
Use the following command to reload the configuration file
Load command: nginx -s reload
[root@liumiaocn nginx]# nginx -s reload
[root@liumiaocn nginx]#
Of course, restart Nginx can play the same role
Step 4: was confirmed
When using the service nginx configuration to open the page, it will display the login authentication pop-up window to display the page nginx configuration after the correct user name and password.
If a user name or password is incorrect you will be prompted to log in again
In this way, add basic HTTP authentication in Nginx is complete.