Enabling the auth password in nginx is a cheap and easy-to-use authentication method, especially for services deployed on the public network. Adding a layer of auth password greatly improves security and is easy to deploy
Modify nginx configuration
You only need to add 2 lines, both in server /http /location
auth_basic "Please input password"; ##输入用户名密码提示框
auth_basic_user_file /etc/nginx/conf.d/auth_pwd; ##配置用户名密码验证文件路径
generate password
There are two kinds of software that can generate passwords, one is htpasswd with apache, which needs to be installed separately, and the other is openssl with its own tools, and linux with sshd comes with it
The password generation method is as follows
openssl passwd password plaintext
Practical example
>>openssl passwd good@21
>>kqG8JU.QRnwdQ
#然后执行
>>echo -n "wangping:kqG8JU.QRnwdQ" > xxx.auth
#把xxx.auth 配置到nginx conf里面即可
common problem
- The path of auth_basic_user_file configuration is wrong, nginx will not report an error message, this is difficult to find, pay special attention when configuring
- After Chrome enters the wrong password once, the dialog box will not pop up again. You can use the chrome incognito window during the test, so that the password input window will pop up every time