Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Nginx use htpasswd to protect the password of the website

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "Nginx how to use htpasswd to password protect the website". The explanation in the article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian slowly and deeply to study and learn "Nginx how to use htpasswd to password protect the website" together!

The final result is similar (different browser interfaces):

If the authentication fails, it will report http error: 401 authorization required.

To do this, you need to change the configuration of your server and set the username and password you want to log in to.

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

The copy 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 htpasswd file, here are some details to note:

htpasswd path

and nginx.conf at the same level. Ubuntu servers are usually located under/etc/nginx/.

htpasswd's content

Each line is for one user, in the format username:password. Note, however, that the password here is not plaintext, but a string encrypted by crypt(3) password.

You can use a php code to generate the password in htpasswd:

The copy code is as follows:

//password plaintext

$password = 'some password';

//encrypt password

$password = crypt($password, base64_encode($password));

//Get encrypted password

echo $password;

Then write the string to the htpasswd file:

The copy code is as follows:

username1:xucqmk13tfooe

username2:yxtfb3xwkombm

...

htpasswd permissions

To change permissions on htpasswd file, execute the following command:

The copy code is 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 restart the nginx server:

The copy code is as follows:

sudo /etc/init.d/nginx reload

# or

sudo /etc/init.d/nginx restart

Thank you for reading, the above is the content of "Nginx how to use htpasswd for password protection of the website", after learning this article, I believe that everyone has a deeper understanding of how Nginx uses htpasswd for password protection of the website, and the specific use needs to be verified by everyone. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report