nginx默认模块
//查看编译参数 可以查看模块
nginx -V
--with-http_stub_status_module
作用:展示连接处理的状态,监控连接的信息,Nginx的客户端状态
//语法:
Syntax:stub_status;
Default:-
Context:server ,location //在server location这一级下面进行配置
//在server中示例配置
location /mystatus{
stub_status;
}
//在浏览器输入地址 ,可以查看到信息
ip:port/mystatus
//信息内容
Activite connections:2 //活跃的连接数
server accepts handled requests 122 55 55 //nginx处理握手总的次数,所处理的连接数,总的请求数
Reading:0 Writing:1 waiting:1 //正在读的个数,正在写的个数,正在等待的个数(开启长连接的情况下)
--with-http_random_index_module
作用:目录中选择一个随机的主页
//语法
Syntax:random_index on|off;
Default:random_index off;
Context:location
//示例 在目录中随机选择一个作为主页
location / {
#资源文件存放目录
root /usr/share/nginx/html;
random_index on;
#index index.html index.htm;
}
--with-http_sub_module
作用:服务给客户端response内容的时候,对http的内容进行替换
//语法
Syntax: sub_filter string replacement; // string要替换的内容,replaceement要替换后的内容
Default: -
Context:http,server,location //在http这一层配置可以对多个 server进行替换
//在http请求的时候(请求头当中的一个字段),校验服务端和客服端的内容是否发生了变更,记录的时间戳,是否发生了更新,没有更新不需要返回,有更新返回
Syntax:sub_filter_last_modified on | off;
Default:sub_filter_last_modified off;
Context:http,server,location
//匹配html代码的第一个还是匹配所有的字符串 on 匹配第一个,off 对html所有指定的内容进行匹配
Syntax:sub_filter_once on | off;
Default:sub_filter_once on;
Context:http,server,location
//示例
location / {
root /opt/app/code;
index index.html index.htm;
sub_filter '<a>你好' '<a>hello' //注意缓存,看效果,
sub_filter_once off;//对所有的匹配内容进行替换
}
nginx请求
http请求建立在一次tcp连接的基础上,一次tcp请求至少产生一次http请求
-limit_conn_module
作用:连接频率的限制
//存储连接的状态空间来判断,以哪个作为限制的标准(key)如:ip来限制;zone=空间名字:空间大小
Syntax:limit_conn_zone key zone=name:size;
Default: -
Context:http
//zone为上面定义的 name number并发的数目
Syntax:limit_conn zone number;
Default -
Context:http,server,location
-limit_req_module
作用: 请求频率的限制
//rate速率 秒为单位
Syntax:limit_req_zone key zone=name:size rate=rate;
Default:-
Context:http
//上面配置的name
Syntax:limit_req zone=name [burst=number][nodelay];
Default: -
Context:http,server,location
示例:
//示例
http{
...
#请求连接限制的配置
limit_conn_zone $binanry_remote_addr zone=conn_zone:1m;
#以同一个客户端的地址发来当前请求,速率每秒一个请求,存储一个会话 remote_addr比binary_remtoe_addr多10个字节,在1m空间内可以节省内存 req_zone 对应下面的 location中的名称
limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
...
server {
...
location /{
root /opt/app/code;
#连接的限制
#limit_conn conn_zone 1;
# burst=3 客服端请求的速率超过之后,遗留的3个请求放在下一秒执行,起到限速的作用; nodelay 表示其它请求的直接返回503
#limit_req zone=req_zone burst=3 nodelay;
#limit_req zone=req_zone burst=3;
#启用请求限制
#limit_req zone=req_zone;
index index.html index.htm;
}
}
}
nginx访问控制
-http_access_module
作用:基于ip的访问控制 基于变量 remote_addr来识别ip,如果开启的代理等,可能会限制无效
//配置允许访问:ip,网段,socket控制访问,允许所有的
Syntax:allow address | CIDR | unix:|all;
Default: -
Context:http,server,location,limit_except //不同的层级限制不同范围
//不允许访问:ip ,网段,socket控制访问,允许所有
Syntax:deny address | CIDR |unix:|all;
Default:-
Context:http,server,location,limit_except
//示例 因为在conf.d文件安配置了 include /../*.conf; 改变配置文件的名字还是会读取的
server {
...
location / {
root /opt/app/code;
index index.html index.htm;
}
#匹配根目录下对 admin.html 页面进行限制访问,限制某个ip访问
location ~ ^/admin.html {
root /opt/app/code;
#如果是公网ip,可以通过ip138来查,局域网 ifcofig来查
#限制自己的ip不能访问该页面,返回403
deny 192.168.99.1;
#允许所有的ip访问
allow all;
index index.html index.htm;
}
...
#匹配根目录下对 admin.html 页面进行限制访问,只允许某个ip(网段)访问
location ~ ^/admin.html {
root /opt/app/code;
#如果是公网ip,可以通过ip138来查,局域网 ifcofig来查
#允许指定的ip访问 //限制某个网段访问 1~24
allow 192.168.99.1/24;
#限制所有ip不能访问该页面,返回403
deny all;
index index.html index.htm;
}
...
}
http_x_forwarded_for
作用:在进行访问的控制的时候,会把源ip 和代理ip 都提交到nginx服务,即可进行判断了;默认只能获取直接ip reomote_addr
http_x_forwarded_for = Client IP,Proxy(1)IP,Proxy(2)IP,..
解决 Http_access_module局限性:
- 采用别的http头信息控制访问,如:http_x_forwarded_for //可以被修改,不是强制标准
- 结合geo模块作
- 通过http自定义变量传递
-http_auth_basic_module
作用:基于用户的信任登录
//有字符串表示开启,会在前端显示默认的信息
Syntax:auth_basic string | off;
Default:auth_basic off;
Conext:http,server,location,limit_except
参考文档:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
//加了个文件路径,存储了用户名密码信息的文件
Syntax:auth_basic_user_file file;
Default:-
Context:http,server,location,limit_except
//文件书写格式 username, password,描述 ;密码加密的方式可以采用htpasswd 需要先安装包 yum install httpd-tools -y
# comment
name1:password1
name2:password2:comment
name3:password3
//示例
//先用htpasswd生成密码文件 usernmae为你要设置的用户名,之后输入密码即可,生成响应的文件
htpasswd -c ./auth_conf username
#匹配根目录下对 admin.html 页面进行限制访问,只允许某个ip(网段)访问
location ~ ^/admin.html {
root /opt/app/code;
auth_basic "Auth access test ! input your password!"
auth_basic_user_file /etc/nginx/auth_conf 上面密码文件存放的路径
index index.html index.htm;
}
...
//在浏览器输入地址请求的时候,会要求你输用户名和 密码
采用http_auth_basic_module局限性
- 用户信息依赖文件方式
- 操作管理机械,效率低效
解决方案
- nginx结合lua实现高效验证
- Nginx和ldap打通,利用nginx-auth-ldap模块
压力测试工具 ab
// -n 50 总共发起50个请求,-c 20 同时20个并发请求 url地址
ab -n 50 -c 20 http://ip:port/index.html
网友评论