When some website paths are not allowed for public access, such as the website background password, we encrypt and authenticate the paths on the Web server. Only the correct user name and password can be entered for access. Apache has such a function, so can Nginx. The following describes how to encrypt certain paths in Nginx.
Generate password
Method 1: Use Apache's htpasswd tool to create a user password file
To use htpasswd, you must have installed apache.
# Htpasswd-B-c filename username passwd
Adding password for user ******
Filename is the created File. Place the file in a directory that cannot be accessed through the WEB path and provide the absolute path address of the file in the Nginx configuration file. Username is the user name and passwd is the password.
Method 2: use perl to create a password
Considering that users who have installed Nginx may not be able to install Apache (httpd), if they only want to generate a password to install it, it will not be easy to use. The following describes how to use a perl program to generate a password.
Create a program:
# Vim genpwd. pl
Write the following content:
#! /Usr/bin/perl
Use strict;
My $ genpwd = $ ARGV [0];
Print crypt ($ genpwd, $ genpwd). "\ n ";
Execute:
# Chmod u + x genpwd. pl
Generate password:
#./Genpwd. pl password
PapAq5PwY/QQM
Write the output password to a password file in the following format:
Username: the encrypted password.
One account per row.
Method 3: online generation
It is convenient to use online tools provided by the open-source Chinese community. Click here to jump
Set Nginx
If the multi-domain virtual host configuration is used to encrypt a directory, modify the configuration file:
Location /{
Root/data/www/domain.com;
Index. php index.html index.htm;
Try_files $ uri // index. php? Q = $ uri & $ args;
Location ~ ^/Demo/book /.*{
Autoindex on;
Autoindex_exact_size off;
Autoindex_localtime on;
Charset UTF-8, gbk, big5;
Auth_basic "Password Needed ";
Auth_basic_user_file/usr/local/nginx/vhosts/auth/authuser;
}
}
"Location ~ ^/Demo/book /.*{...}" In this way, you cannot directly access the files in this directory without a password to achieve the desired effect.