nginx的access的阶段的access模块、auth_basic模块、auth_request模块及satisfy指令介绍
access 模块
示例从上向下匹配
1 2 3 4 5 6 7 | location / { deny 192.168 . 1.1 ; allow 192.168 . 1.0 / 24 ; allow 10.1 . 1.0 / 16 ; allow 2001 : 0db8 :: / 32 ; deny all ; } |
auth_basic模块 基于用户名密码做认证
安装http-tools 工具
1 2 3 4 5 6 7 | [root@python ~] # htpasswd -cb yt yu 123 Adding password for user yu [root@python ~] # htpasswd -b yt yutre 123qwe Adding password for user yutre [root@python ~] # cat yt yu:$apr1$ / N3KI0q8$UxOw8KlG1QBO5N2Niryxo0 yutre:$apr1$BAFJsGn2$qKrWI0G6cSzPPIEG4XGPV0 |
nginx配置
1 2 3 4 5 6 7 8 9 10 11 12 | [root@python vhast] # cat auth_basic.conf server { server_name auth_basic.com; default_type text / plain; root html / ; location / { satisfy any ; auth_basic "tset auth_basic" ; auth_basic_user_file passwd; deny all ; } } |
auth_request模块 基于第三方库做认证;需要重新编译,默认没有这个模块;
1 2 3 4 5 6 | [root@python vhast] # cd ~/nginx-1.15.9/ [root@python nginx - 1.15 . 9 ] # ./configure --prefix=/data/web --sbin-path=/usr/bin --user=nginx --group=nginx --with-http_stub_status_module --with-http_auth_request_module checking for OS [root@python nginx - 1.15 . 9 ] # make [root@python nginx - 1.15 . 9 ] # rm -rf /usr/bin/nginx [root@python nginx - 1.15 . 9 ] # cp objs/nginx /usr/bin/ |
原理:收到请求后,生成子请求,通过反向代理技术把请求传递给上游服务器,通过上游服务的响应来判断是否处理这个请求,若上游服务器返回的响应码是2**,则继续执行,若返回401或403;则将响应码返回客户端
指令介绍
1 2 3 4 5 6 | Syntax: auth_request uri | off; Default: auth_request off; Context: http, server, location Syntax: auth_request_set $variable value; Default: — Context: http, server, location |
配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | server { server_name auth_basic.com; root html; location / iiiii{ satisfy any ; auth_basic "tset auth_basic" ; auth_basic_user_file passwd; deny all ; } location / { auth_request / test_auth; } location = / test_auth { proxy_pass http: / / 127.0 . 0.1 : 90 ; proxy_pass_request_body off; proxy_set_header Content - Length ""; proxy_set_header X - Original - URI $request_uri; } } 认证服务器 server { listen 90 ; location / { return 201 'auth succes' ; } } |
测试正常返回
测试异常返回
1 2 3 4 5 6 7 | [root@python vhast] # cat test-l.conf server { listen 90 ; location / { return 401 'auth succes' ; } } |
测试
satisfy指令介绍
1 2 3 | Syntax: satisfy all | any ; #all表示3个模块都通过认证才往下走;any表示3个模块任意一个通过就可以通过了 Default: satisfy all ; Context: http, server, location |
有return指令,access阶段的指令不会生效
草都可以从石头缝隙中长出来更可况你呢
分类:
web
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏