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; } |