Nginx sets the website directory access verification method

Source: Internet
Author: User
Tags auth

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.