Nginx server to set Web Site authentication access method _nginx

Source: Internet
Author: User
Tags auth nginx server

Set up access verification for the Site directory after we enter this page will need to enter the authentication password, I would like to introduce the Nginx Directory access authentication Code method summary.

1. Create Class HTPASSWD files

Perform:

Wget-c Soft.vpser.net/lnmp/ext/htpasswd.sh;bash htpasswd.sh 

Enter the username, password, and authentication file name as prompted. The script will automatically generate the authentication file. Record the path of the file returned by the script. such as:/usr/local/nginx/conf/vpser.net.auth.

2. Add Auth authentication configuration for Nginx

The following is an example of a soft directory under a domain name that adds the following code to the server section of the domain name:

Copy Code code as follows:

Location ^~/soft/
{
Auth_basic "Authorized users only";
Auth_basic_user_file//Here Write the previous script to return the file path;
}

Authorized users only as a hint of information, you can modify the information you want him to prompt; Auth_basic_user_file need to fill in the Htpasswd.sh script to return the path of someone else's file. After the above prompts to modify the configuration, restart Nginx, access to http://yourdomainname/soft/will prompt you to enter a username and password.

Note that the addition of authentication after the directory of PHP will not be resolved, there will be download prompts, if you want to be able to parse PHP can change the above configuration to:

Copy Code code as follows:

Location ^~/soft/{
Location ~. *. (PHP|PHP5)? $ {
Fastcgi_pass Unix:/tmp/php-cgi.sock;
Fastcgi_index index.php;
Include fcgi.conf;
}
Auth_basic "Authorized users only";
Auth_basic_user_file//Here Write the previous script to return the file path;
}


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.