nginx的安全配置
Nginx_auth_basic_module 简单认证
用"http basic authentication(简单的认证)"来限制用户的访问
一般的用法
location / {
auth_basic string;
auth_basic_user_file /etc/nginx/conf.d/htpasswd;
}
Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except
使用基本的'http basic authentication'用户名和密码验证
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
指定保存的用户和密码,格式如下
comment
name1:password1
name2:password2:comment
name3:password3
<!-- more -->文件名支持变量
我们也可以使用htpasswd
工具来生成密码。用法如下
先安装yum -y install httpd-tools
查看htpasswd的用法
[root@test ~]# htpasswd --help
Usage:
htpasswd [-cimBdpsDv] [-C cost] passwordfile username
htpasswd -b[cmBdpsDv] [-C cost] passwordfile username password
htpasswd -n[imBdps] [-C cost] username
htpasswd -nb[mBdps] [-C cost] username password
-c Create a new file.
//创建一个新的文件
-n Don't update file; display results on stdout.
-b Use the password from the command line rather than prompting for it.
//使用命令行中的密码,而不提示输入密码,非交互式创建密码
-i Read password from stdin without verification (for script usage).
//从stdin读取密码而不进行验证(用于脚本)
-m Force MD5 encryption of the password (default).
//使用md5加密(默认)
-B Force bcrypt encryption of the password (very secure).
//强制密码加密(非常安全)
-C Set the computing time used for the bcrypt algorithm
(higher is more secure but slower, default: 5, valid: 4 to 31).
-d Force CRYPT encryption of the password (8 chars max, insecure).
-s Force SHA encryption of the password (insecure).
-p Do not encrypt the password (plaintext, insecure).
//不加密密码(明文,不安全)
-D Delete the specified user.
//删除指定用户
-v Verify password for the specified user.
On other systems than Windows and NetWare the '-p' flag will probably not work.
The SHA algorithm does not use a salt and is less secure than the MD5 algorithm.
用法实例
创建一个新的文件,并设置密码和用户,交互式
[root@test ~]# htpasswd -c /tmp/htpasswd linux
New password:
Re-type new password:
Adding password for user linux
[root@test ~]# cat /tmp/htpasswd
linux:$apr1$3VhLd1HR$uSkhBgwIGYFDY5lWzZZsM0
添加一个新用户到配置文件中,非交互式创建密码
[root@test ~]# htpasswd -b /tmp/htpasswd baodian 321
Adding password for user baodian
[root@test ~]# cat /tmp/htpasswd
linux:$apr1$3VhLd1HR$uSkhBgwIGYFDY5lWzZZsM0
baodian:$apr1$ApIm66k6$CpqxwyqMUq.ZbcmMVELu10
删除一个指定的用户
[root@test ~]# htpasswd -D /tmp/htpasswd linux
Deleting password for user linux
[root@test ~]# cat /tmp/htpasswd
baodian:$apr1$ApIm66k6$CpqxwyqMUq.ZbcmMVELu10
从指定的文件里面删除指定的用户
ngx_http_access_module(限制ip地址访问模块)
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
这个要严格的匹配顺序,先匹配那个规则,从那个出去,如果没有任何匹配,将会走默认,默认是放行。
ngx_http_limit_conn_module tcp连接次数限制
Syntax: limit_conn_zone key zone=name:size;
Default: —
Context: http
设置一个记录key区域的大小和名称
这里一般用客户端的地址来做他的key值
$remote_addr 占7到15个字节
\$binary_remote_addr 固定占用4个字节(为了让同等的共享内存中,记录更多的客户端信息,我们一般使用\$binary_remote_addr来当作key值.如果这个共享内存区域被占完,那么服务器将对来后来的tcp连接返回错误)
Syntax: limit_conn zone number;
Default: —
Context: http, server, location
设定共享区域内存的名称,并设置一个客户端在同一个时间能建立几次链接
Syntax: limit_conn_status code;
Default: limit_conn_status 503;
Context: http, server, location
设置拒绝响应的状态码,默认是503,(当用户的tcp连接请求超过服务器限制的次数的时候就返回报错状态码)
ngx_http_limit_req_module 请求次数限制
一次tcp链接可以处理多次http链接请求。
Syntax: limit_req_zone key zone=name:size rate=rate [sync];
Default: —
Context: http
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
rate:一分钟处理多少次请求
Syntax: limit_req zone=name [burst=number] [nodelay];
Default: —
Context: http, server, location
burst:代表表请求了1次的之后接着允许几次请求
如果我们限制了1s中只能处理一次请求的话,我们请求一个页面不管发起多少次请求,都会只请求第一次
转载于:https://blog.51cto.com/13447608/2286311