How to Set password protection for a website using htpasswd in Nginx

Source: Internet
Author: User
Tags crypt password protection

The final effect is similar (different browsers have different interfaces ):

If the authentication fails, an HTTP error 401 Authorization Required will be reported.

To implement this function, you need to change the server configuration and set the username and password for logon.

First, we need to change the Nginx server configuration of the website. For the Ubuntu server, this configuration file is usually located in/etc/nginx/sites-enabled /, for example, here I will use the default configuration file/etc/nginx/sites-enabled/default as an example:

Copy codeThe Code is as follows: server {
Server_name www.fancycedar.info
Root/www/fancycedar

#...

Location /{
# Add the following two lines
Auth_basic "Restricted ";
Auth_basic_user_file htpasswd;
#...
}

#...
}

Next, you need to create the htpasswd file. Here are some details to note:

Htpasswd path
And nginx. conf in the same level directory. The Ubuntu server is generally under/etc/nginx.

Htpasswd content
Each row is a user in the format of username: password. However, note that the password here is not in plain text, but the encrypted string of the password after crypt (3.

You can use a piece of PHP code to generate the password in htpasswd:

Copy codeThe Code is as follows: // plaintext of the password
$ Password = 'some password ';
// Encrypt the password
$ Password = crypt ($ password, base64_encode ($ password ));
// Obtain the encrypted password
Echo $ password;

Then write the string to the htpasswd file:

Copy codeThe Code is as follows: username1: xucqMk13TfooE
Username2: YXTfb3xWKOMBM
...

Htpasswd permission

To change the permission of the htpasswd file, run the following command:

Copy codeThe Code is as follows: sudo chown root: www-data htpasswd
Sudo chmod 640 htpasswd

Are You Ready?

After the above preparations are completed, we can re-load or restart the Nginx Server:

Copy codeThe Code is as follows: sudo/etc/init. d/nginx reload
# Or
Sudo/etc/init. d/nginx restart

Finished.

P.S. Don't forget what your password is ......

Link: http://www.fancycedar.info/2013/06/apache-nginx-htpasswd/

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.