nginx访问控制

释放双眼,带上耳机,听听看~!

nginx的访问控制主要分为两类:

  • 基于IP的访问控制 http_access_module
  • 基于用户的信任登录 http_auth_basic_module

1.基于IP的访问控制http_access_module

语法

1.allow


1
2
3
4
5
1Syntax:    allow address | CIDR | unix: | all;
2Default: —
3Context: http, server, location, limit_except
4
5

2.deny


1
2
3
4
5
1Syntax:    deny address | CIDR | unix: | all;
2Default: —
3Context: http, server, location, limit_except
4
5

名词解释


1
2
3
4
5
6
1address  指IP地址(192.168.1.1)
2CIDR     指网段(192.168.1.0/24)
3unix     主要是在linux和unix用到的socket访问
4all      所有人访问
5
6

示例配置

原配置


1
2
3
4
5
6
1location / {
2    root   html;
3    index  index.html index.htm;
4}
5
6

nginx访问控制

1.禁止1.1.1.1(这里指的是路由器上的IP地址)访问,允许所有ip访问


1
2
3
4
5
6
7
8
1location / {
2   root   /opt/app/code;
3   deny   1.1.1.1;
4   allow  all;
5   index  index.html index.htm;
6}
7
8

nginx访问控制

2.允许1.1.1.0/24网段ip访问,禁止所有ip访问


1
2
3
4
5
6
7
8
1location ~ /{
2   root   /opt/app/code;
3   allow 1.1.1.0/24;
4   deny all;
5   index  index.html index.htm;
6}
7
8

allow 和 deny是配合使用的。

局限性

如果客户端使用代理来访问网站,这样就不能保证禁止或允许某些ip访问。

2.基于用户的信任登录 http_auth_basic_module

语法

1.auth_basic


1
2
3
4
5
1Syntax:    auth_basic string | off;
2Default: auth_basic off;
3Context: http, server, location, limit_except
4
5

2.auth_basic_user_file


1
2
3
4
5
1Syntax:    auth_basic_user_file file;
2Default: —
3Context: http, server, location, limit_except
4
5

示例配置

新建一个密码文件,名称为auth_conf,用户名为xixi,回车后输入密码123456,确认密码

安装htpasswd

htpasswd是Apache密码生成工具,Nginx支持auth_basic认证,因此我门可以将生成的密码用于Nginx中,输入一行命令即可安装:yum -y install httpd-tools ,参数如下:


1
2
3
4
5
6
7
8
9
10
1-c 创建passwdfile.如果passwdfile 已经存在,那么它会重新写入并删去原有内容.
2-n 不更新passwordfile,直接显示密码
3-m 使用MD5加密(默认)
4-d 使用CRYPT加密(默认)
5-p 使用普通文本格式的密码
6-s 使用SHA加密
7-b 命令行中一并输入用户名和密码而不是根据提示输入密码,可以看见明文,不需要交互
8-D 删除指定的用户
9
10

1
2
3
1$ htpasswd -c ./auth_conf xixi
2
3

修改配置文件


1
2
3
4
5
6
7
8
1location / {
2    root   html;
3    auth_basic "Auth access test!input your passward!";
4   auth_basic_user_file /etc/nginx/auth_conf;
5    index  index.html index.htm;
6}
7
8

重启服务之后 访问web就需要输入密码

配置多个用户 去掉-c

转载于:https://my.oschina.net/u/3913691/blog/3056613

给TA打赏
共{{data.count}}人
人已打赏
安全运维

WordPress网站专用docker容器环境带Waf

2020-7-18 20:04:44

安全运维

运维安全-Gitlab管理员权限安全思考

2021-9-19 9:16:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索