How to set up a password-protected web site in Nginx with htpasswd _nginx

Source: Internet
Author: User
Tags crypt nginx server

The final effect is similar (different browser interfaces vary):

If authentication fails, an HTTP error is reported: 401 Authorization Required.

To do this, you need to change the server configuration and set the username and password for the login.

First we need to change the Web site's nginx server configuration, Ubuntu server, this profile is usually located in/etc/nginx/sites-enabled/, for example, I use the default configuration file here/etc/nginx/ Sites-enabled/default to do an example:

Copy Code code 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:

Path to HTPASSWD
and nginx.conf at the same level of directory can be. Ubuntu server is usually under/etc/nginx/.

the content of htpasswd
Each behavior is a user, formatted as Username:password. Note, however, that the password is not plaintext, but rather a password crypt (3) encrypted string.

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

Copy Code code as follows:

Password plaintext
$password = ' some password ';
Encrypt a password
$password = Crypt ($password, Base64_encode ($password));
Get the encrypted password
Echo $password;


The string is then written to the htpasswd file:

Copy Code code as follows:

Username1:xucqmk13tfooe
Username2:yxtfb3xwkombm
...


Permissions for HTPASSWD

You need to change the permissions for the htpasswd file, and execute the following command:

Copy Code code as follows:

sudo chown root:www-data htpasswd
sudo chmod 640 htpasswd


Are you Ready?

When the above preparations are done, we can reload or reboot the Nginx server:

Copy Code code as follows:

Sudo/etc/init.d/nginx Reload
# or
Sudo/etc/init.d/nginx restart


Completed.

P.s. Don't forget the password you set ...

Original 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.