nginx directory password protection Settings

  • 2020-05-06 12:13:23
  • OfStack

Then add
to the corresponding server segment in the nginx.conf file
location ^~ /test/ {
auth_basic TEST-Login;
auth_basic_user_file /root/htpasswd;

Then create a new file htpasswd
in root's home directory /root/ This file is written in
Username: password
One
per line And the password must be encrypted using the function crypt(3)

The official file says you can use Apache's htpasswd tool to create the
password file Of course, you can also use perl to create a password file and create a new pw.pl file with the contents:

 
#!/usr/bin/perl 
use strict; 

my $pw=$ARGV[0] ; 
print crypt($pw,$pw). " n " ; 

Then execute chmod +x pw.pl
./pw.pl password
papAq5PwY/QQM
papAq5PwY/QQM is crypt() password
for password Then, the encrypted password
generated by perl is used above According to the
User name: password
Is written to
in the htpasswd file
This completes setting

If you don't use
^~ /test/
With
/test
If so, you will only be able to verify the directory directly access to its files will not pop up login verification

And htpasswd is the name of the file that you can set to
The user name is also set at will without having to encrypt
The password must be encrypted

using the function crypt(3)

Related articles: