我是靠谱客的博主 爱撒娇月饼,这篇文章主要介绍Nginx之验证模块ngx_http_auth_basic_module简介和使用一、ngx_http_auth_basic_module模块简介二、模块配置示例三、效果验证,现在分享给大家,希望可以做个参考。

一、ngx_http_auth_basic_module模块简介

  ngx_http_auth_basic_模块允许通过使用“http基本身份验证”协议验证用户名和密码来限制对资源的访问。访问也可以由地址、子请求的结果或JWT限制。通过地址和密码同时限制访问由satisfy指令控制。

二、模块配置示例

1、官网示例

复制代码
1
2
3
4
5
location / { auth_basic "closed site"; auth_basic_user_file conf/htpasswd; }

2、指令说明

  主要有两个指令,auth_basic和auth_basic_user_file,作用域范围是http, server, location, limit_except。

  • auth_basic,启用使用“HTTP基本身份验证”协议验证用户名和密码。使用语法是auth_basic string | off,默认是off
  • auth_basic_user_file file,指定保存用户名和密码的文件,没有默认值,文件内格式如下:
    # comment
    name1:password1
    name2:password2:comment
    name3:password3

3、配置示例

复制代码
1
2
3
4
5
6
7
8
9
10
11
server { listen 80; #listen、server_name这些正常配置 server_name www.test.com; access_log /var/log/nginx/test.access.log; location / { auth_basic "welcome,please login"; #启用验证 auth_basic_user_file /etc/nginx/passwd.db; #指定验证文件 proxy_pass http://192.168.0.141:8080; } }

4、创建密码验证文件

首先安装htpassd命令

[root@s141 conf.d]# yum install -y httpd-tools

创建密码文件

[root@s141 conf.d]# htpasswd -c /etc/nginx/passwd.db zhangsan
New password:
Re-type new password:
Adding password for user zhangsan

添加用户

[root@s141 conf.d]# htpasswd /etc/nginx/passwd.db lisi
New password:
Re-type new password:
Adding password for user lisi

三、效果验证

1、启用验证前的页面如下

在这里插入图片描述

2、重新加载nginx配置

[root@s141 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@s141 conf.d]# nginx -s reload

3、再次访问网站页面

在这里插入图片描述

4、输入用户名密码后页面

在这里插入图片描述

5、修改nginx配置文件为用户名密码和IP验证二选一

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server { listen 80; #listen、server_name这些正常配置 server_name www.test.com; access_log /var/log/nginx/test.access.log; location / { satisfy any; #satisfy指令表示选择控制方式,any表示任意满足即可,all表示需要双重认证 allow 192.168.0.141/32; deny all; auth_basic "welcome,please login"; #启用验证 auth_basic_user_file /etc/nginx/passwd.db; #指定验证文件 proxy_pass http://192.168.0.141:8080; } }

6、IP白名单访问

在这里插入图片描述

7、非IP白名单需要验证才可以访问

在这里插入图片描述

最后

以上就是爱撒娇月饼最近收集整理的关于Nginx之验证模块ngx_http_auth_basic_module简介和使用一、ngx_http_auth_basic_module模块简介二、模块配置示例三、效果验证的全部内容,更多相关Nginx之验证模块ngx_http_auth_basic_module简介和使用一、ngx_http_auth_basic_module模块简介二、模块配置示例三、效果验证内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(70)

评论列表共有 0 条评论

立即
投稿
返回
顶部