服务访问的用户认证
[root@Nginx ~]# htpasswd -cmb /usr/local/nginx/conf/.htpasswd admin lee
Adding password for user admin
[root@Nginx ~]# vim /usr/local/nginx/conf/conf.d/vhosts.conf
server {
listen 80;
server_name lee.timinglee.org;
location /admin {
root /usr/local/nginx/html;
auth_basic "login passwd";
auth_basic_user_file "/usr/local/nginx/conf/.htpasswd";
}
}
# 创建admin目录(如果不存在)
[root@Nginx ~]# mkdir -p /usr/local/nginx/html/admin
# 创建index.html文件,随便写点内容,方便测试
[root@Nginx ~]# echo "Hello, this is the admin page!" > /usr/local/nginx/html/admin/index.html
[root@Nginx ~]# systemctl restart nginx.service
#测试:
root@Nginx ~]# curl lee.timinglee.org/admin/
<html>
<head><title>401 Authorization Required</title></head>
<body>
<center><h1>401 Authorization Required</h1></center>
<hr><center>nginx/1.28.1</center>
</body>
</html>
[root@Nginx ~]# curl -uadmin:lee http://lee.timinglee.org/admin/
admin