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/