16. 访问认证
10. nginx访问认证¶
#1.介绍
nginx提供了访问认证模块,用于保护重要信息
#2. 语法
location / {
auth_basic 'string'; # 提示信息
auth_basic_user_file conf/htpasswd; # 密码文件
}
#3.生成密码命令
yum install httpd-tools -y
htpasswd -bc .access username password
-b: 在命令行中输入账号密码
-c:创建密码文件
.access文件就是生成的密码文件,默认采用md5加密验证
username password:要使用的用户名和密码
#4. 案例
1.准备配置文件
server {
listen 95;
server_name _;
location / {
root html/learn_auth;
index index.html;
auth_basic "learn nginx auth_module";
auth_basic_user_file htpasswd;
}
}
2.生成加密文件
htpasswd -bc ./htpasswd chupeng 123456
3. 创建页面
echo auth > /www/html/learn_auth/index.html
4. 重启nginx
nginx -t
nginx -s reload
5. 测试
192.168.178.110:95
# 需要输入账号密码:chupeng:123456
最后更新:
2022-02-20 13:32:53