Nginx uses HTTP auth Basic Authentication

Source: Internet
Author: User
Use nginx as the front-end server. Some resources must be protected. HTTP auth basic authentication is simple and convenient. The HTTP auth basic password of nginx is encrypted with Crypt (3. Specific can refer to: http://wiki.nginx.org/HttpAuthBasicModule

 

Take a simple background management as an example. The address is http: // xxxx/admin/* to manage the permissions of the files under admin;

1. Enter the ngnix configuration address. Take my Ubuntu as an example; CD/etc/ngnix

2. Run the following command to generate the passfile:Htpasswd-c-d/etc/nginx/pass_file Username

A. Enter the preceding command, press enter to enter the password, press enter again, and enter Confirm Password.

3. Check whether pass_file already exists/etc/ngnix/pass_file

4. Modify ngnix Configuration

Location ~ /Admin {
Auth_basic "restricted ";
Auth_basic_user_file/etc/nginx/pass_file; // use the absolute path.
Root/home/www/admin; // host address. Otherwise, the corresponding file cannot be found after the authentication is passed.
}

5. Restart ngnix and sudo service ngnix reload;

6. log on to http: // xxxx/admin/*. The verification box is displayed, indicating that the verification configuration is complete.

 

If php exists in admin, php URL cannot be properly parsed. We need to reconfigure the configuration as follows:

 

Location ~ /Admin {

Auth_basic "restricted ";
Auth_basic_user_file/etc/nginx/pass_file; // use the absolute path.
Root/home/www/admin; // host address. Otherwise, the corresponding file cannot be found after the authentication is passed.
If ($ request_filename ~ *. Php)
{
Fastcgi_pass 127.0.0.1: 9000;
} // The following files cannot be placed in brackets; otherwise, an error is reported.
Fastcgi_index index. php;
Fastcgi_param script_filename/home/www/ADMIN $ fastcgi_script_name; // The path is the same as root
Include fastcgi_params;

}

 

Modify the configuration and restart ngnix. The php file is displayed normally.

 

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.