用户认证
当访问一些私密资源时,最好配置用户认证,增加安全性。
# 安装httpd
yum install -y httpd
# 生成用户密码文件
[root@lwz1 vhost]# htpasswd -c /usr/local/nginx/conf/passwd user1
New password:
Re-type new password:
Adding password for user user1
# 修改密码
htpasswd /usr/local/nginx/conf/passwd user1
# 配置nginx用户认证
[root@lwz1 vhost]# cat lwz.com
server
{
listen 80;
server_name www.lwz.com;
root /data;
access_log logs/lwz.access.log main;
location /www/ {
auth_basic "user:passwd";
auth_basic_user_file /usr/local/nginx/conf/passwd;
}
}
# 测试
[root@lwz1 vhost]# curl -x127.0.0.1:80 www.lwz.com/www/1.html
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.23.1</center>
</body>
</html>
[root@lwz1 vhost]# curl -x127.0.0.1:80 www.lwz.com/www/1.html -uuser1:123123
111