1. Create a class htpasswd file
Run:
The code is as follows: |
Copy code |
Wget-c soft.vpser.net/lnmp/ext/htpasswd.shw.bash htpasswd. sh |
Enter the user name, password, and authentication file name as prompted. The script automatically generates the authentication file. Record the file path returned by the script. For example,/usr/local/nginx/conf/vpser.net. auth.
2. Add auth authentication configuration for Nginx
The following uses the soft directory under a domain name as an example to add the following code to the server segment of the domain name:
The code is as follows: |
Copy code |
Location ^ ~ /Soft/ { Auth_basic "Authorized users only "; Auth_basic_user_file write the file path returned by the preceding script; } |
Authorized users only indicates the prompt information, which can be changed to the information you want him to prompt. Fill in the path of the file returned by the htpasswd. sh script after auth_basic_user_file. After you modify the configuration as prompted, restart nginx and access http: // yourdomainname/soft/. Then, you will be prompted to enter the user name and password.
Note: After authentication, PHP under this directory will not be parsed and a download prompt will appear. If you want to parse PHP, you can change the above configuration:
The code is as follows: |
Copy code |
Location ^ ~ /Soft /{ Location ~ . *. (Php | php5 )? $ { Fastcgi_pass unix:/tmp/php-cgi.sock; Fastcgi_index index. php; Fcinclude GI. conf; } Auth_basic "Authorized users only "; Auth_basic_user_file write the file path returned by the preceding script; } |
Method 2
First create the following script, name it crypt. pl, and put it under the root directory.
The code is as follows: |
Copy code |
#! /Usr/bin/perl Usestrict; My $ PassWord = $ ARGV [0]; Print Crypt ($ passWord, $ passWord). "n "; |
Then run:
The code is as follows: |
Copy code |
Chmod + x crypt. pl ./Crypt. pl password |
Write the encrypted password generated using crypt. pl to the htpasswd file in the username: password format. Put htpasswd in the/usr/local/nginx/conf directory.
Add the following content to/usr/local/nginx/conf/nginx. conf and put it in the server.
The code is as follows: |
Copy code |
Location/phpmyadmin { Auth_basic "Nginx Status "; Auth_basic_user_file/usr/local/nginx/conf/htpasswd; } |
The above code encrypts the phpmyadmin directory.