nginx访问控制

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

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

1.基于IP的访问控制http_access_module

语法
1.allow
Syntax:	allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
2.deny
Syntax:	deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
名词解释
address  指IP地址(192.168.1.1)
CIDR     指网段(192.168.1.0/24)
unix     主要是在linux和unix用到的socket访问
all      所有人访问
示例配置

原配置

location / {
    root   html;
    index  index.html index.htm;
}

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

location / { 
	root   /opt/app/code;
	deny   1.1.1.1;
	allow  all;
	index  index.html index.htm;
} 

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

location ~ /{
	root   /opt/app/code;
	allow 1.1.1.0/24;
	deny all;
	index  index.html index.htm;
}

allow 和 deny是配合使用的。

局限性

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

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

语法
1.auth_basic
Syntax:	auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except
2.auth_basic_user_file
Syntax:	auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
示例配置

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

安装htpasswd

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

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

修改配置文件

location / {
    root   html;
    auth_basic "Auth access test!input your passward!";
	auth_basic_user_file /etc/nginx/auth_conf;
    index  index.html index.htm;
}

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

配置多个用户 去掉-c

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值