Nginx-模块-ngx_http_auth_basic_module【访问认证】
1、基础
1.1、作用
ngx_http_auth_basic_module模块允许使用HTTP基本身份验证,验证用户名和密码来限制对资源的访问。
1.2、指令介绍
1.2.1、auth_basic-启用或关闭用户和密码认证
# 使用HTTP基本身份验证协议启用用户名和密码验证。 Syntax: auth_basic string | off; Default: auth_basic off; Context: http, server, location, limit_except
1.2.2、auth_basic_user_file-密码保存的文件
# 指定保存用户名和密码的文件 Syntax: auth_basic_user_file file; Default: — Context: http, server, location, limit_except
1.3、密码生成的方法
1.3.1、密码文件格式
# comment
name1:password1
name2:password2:comment
name3:password3
1.3.2、openssl生成
openssl passwd
1.3.3、httpd-tools生成
yum install httpd-tools -y # 首次增加,需要-c htpasswd -b -c /etc/nginx/auto_conf test 1234 # 追加用户,不用-c htpasswd -b /etc/nginx/auto_conf cyc 123
2、实战
2.1、基于用户名和密码认证
2.1.1、配置nginx
cat >/etc/nginx/conf.d/mirror.cyc.com.conf <<'EOF' server{ listen 80; server_name mirror.cyc.com; charset utf-8; root /mnt/; location /cdrom { autoindex on; autoindex_exact_size off; autoindex_localtime on; auth_basic "Enter Password:"; auth_basic_user_file /etc/nginx/auto_conf; } location / { index index.html; } }
EOF
2.1.2、重新加载ngnix
systemctl reload nginx
2.1.3、测试访问
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY