nginx
的访问控制主要分为两类:html
http_access_module
http_auth_basic_module
http_access_module
Syntax: allow address | CIDR | unix: | all; Default: — Context: http, server, location, limit_except
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 全部人访问
原配置linux
location / { root html; index index.html index.htm; }
1.禁止1.1.1.1(这里指的是路由器上的IP地址)访问,容许全部ip访问nginx
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访问web
location ~ /{ root /opt/app/code; allow 1.1.1.0/24; deny all; index index.html index.htm; }
allow 和 deny是配合使用的。app
若是客户端使用代理来访问网站,这样就不能保证禁止或容许某些ip访问。socket
http_auth_basic_module
Syntax: auth_basic string | off; Default: auth_basic off; Context: http, server, location, limit_except
Syntax: auth_basic_user_file file; Default: — Context: http, server, location, limit_except
新建一个密码文件,名称为auth_conf,用户名为xixi,回车后输入密码123456,确认密码工具
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