4. Nginx模块

Nginx官方模块

1.ngx_http_stub_status_module

http://nginx.org/en/docs/http/ngx_http_stub_status_module.html。

此模块可以查看nginx对数据包处理的基本信息

1#启用方法 2location /status { 3 stub_status; 4}

访问 /status,展示的数据如下

1Active connections: 4 2server accepts handled requests 3310840 310840 481035 4Reading: 0 Writing: 1 Waiting: 3

2. ngx_http_random_index_module

http://nginx.org/en/docs/http/ngx_http_random_index_module.html

此模块用于在目录下,随机地取用某个文件,作为默认主页。假如 /usr/share/nginx/html/random 目录下有 aaa.htmlbbb.htmlccc.html 三个文件,则使用如下配置即可

1location /random { 2 root /usr/share/nginx/html; 3 random_index on; 4}

如下代码会随机返回 aaabbbccc 这三个html之一

curl http://127.0.0.1/random/

3. ngx_http_sub_module

http://nginx.org/en/docs/http/ngx_http_sub_module.html

用于替换掉响应内容中的指定字符串。

1location / { 2 sub_filter '<a href="https://www.baidu.com/' '<a href="https://www.qq.com/'; 3 #如果文件中有多处需要替换,只替换第一处 4 sub_filter_once on; 5 #保留替换前,原始的最后修改时间 6 sub_filter_last_modified on; 7 #默认只替换 text/html 这一MIME类型 8 sub_filter_types text/html; 9}

4. ngx_http_limit_conn_module

http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

HTTP协议

请求与连接

说明

HTTP 1.0

TCP不能复用

一个连接,一个请求

HTTP 1.1

顺序性TCP复用

一个连接,可以按顺序的发出多个请求

HTTP 2.0

多路TCP复用

一个连接,可以并行的发出多个请求

我们可以把nginx的zone,理解成一个动态的数据库,1M的zone内存可以存储至少16000条记录,判断用户请求是否合法,就是不断查询当前IP在数据库中记录的数量是否超出了限制。

在如下的路由请求中,我们限制用户的并发连接数分别为5个和10个。

注意:并发是指同一时刻的请求量,与每秒的请求数量是有区别的。

1http { 2 limit_conn_zone $binary_remote_addr zone=addr_conn:10m; 3 ... 4 server { 5 ... 6 location / { 7 limit_conn addr_conn 5; 8 ... 9 } 10 location ~ \.php$ { 11 limit_conn addr_conn 10; 12 ... 13 } 14 } 15}

为了测试代码效果,我们可以安装 httpd-tools 进行ab测试。 对于limit_conn 的测试,不管是内网测试,还是外网测试,都是可行的。

1yum install httpd-tools 2ab -n 20 -c 20 http://127.0.0.1/

执行 ab -n 20 -c 20 http://127.0.0.1/,总共20个连接,失败了15个,成功建立的连接数只有5个了

条目

数值

Complete requests:

20

Failed requests:

15

执行 ab -n 20 -c 20 http://127.0.0.1/index.php,总共20个连接,失败了10个,成功建立的连接数确实只有10个

条目

数值

Complete requests:

20

Failed requests:

10

经过以上测试,可以得出的结论就是:代码是不会骗人的

对于频率限制造成的 Failed requests ,我们在 nginxerror.log 中也能发现错误记录。如果错误日志过多,我们就需要排查一下访问量是否正常。如果正常的话,又需要考虑一下如何优化参数设置。

12017/07/31 11:41:37 [error] 24550#0: *580766 limiting connections by zone "addr_conn", client: 119.130.188.64, server: www.siguoya.name, request: "GET / HTTP/1.0", host: "www.siguoya.name" 22017/07/31 11:41:37 [error] 24550#0: *580767 limiting connections by zone "addr_conn", client: 119.130.188.64, server: www.siguoya.name, request: "GET / HTTP/1.0", host: "www.siguoya.name"

5.ngx_http_limit_req_module

http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

ngx_http_limit_conn_module 用于限制并发连接数,而 ngx_http_limit_req_module 则用于限制并发请求数。实际业务场景中,通常使用 ngx_http_limit_req_module 会更多一些。

1http { 2 limit_req_zone $binary_remote_addr zone=addr_req:10m rate=1r/s; 3 ... 4 server { 5 ... 6 location / { 7 limit_req zone=addr_req; 8 } 9 } 10}

6.ngx_http_access_module

http://nginx.org/en/docs/http/ngx_http_access_module.html

该模块可以通过限制 ip 来实现访问控制,但弊端在于我们有时无法获取到真实的IP,而且用户的IP也是可以动态变化的,另外还比较容易出现误杀的情况

可以通过以下方法来弥补:

  • x_forwarded_for,但是并不可靠,存在被修改或者未传递的可能

  • geo 模块

  • 通过HTTP自定义变量来传递

    location / { allow 192.168.1.0/24; deny all; }

7.ngx_http_auth_basic_module

http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

该模块可以要求用户在访问特定页面的时候,必须输入正确的账号和密码才可以访问,从而实现一个比较初级的权限控制功能

1location /auth_basic { 2 auth_basic "custom comment for this auth_basic"; 3 auth_basic_user_file /usr/local/nginx/conf/.passwd; 4}

关于 auth_basic_user_file 使用到的文件,我们可以使用 htpasswd 这个工具来实现

1#新建一个授权文件 2htpasswd -bc username password >> .passwd 3#在已有的授权文件中新加一个用户 4htpasswd -b username password >> .passwd

ngx_http_auth_basic_module 的缺陷如下:用户权限依赖于授权文件,容易造成企业有多个用户账号体系,运维麻烦。

解决方法:

  • 结合 lua 实现高效认证
  • 利用 nginx-auth-ldap 模块,和 ldap 打通

Nginx第三方模块

有兴趣的可以访问 https://www.nginx.com/resources/wiki/modules/,这里就不一一展开介绍了

专题阅读

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Nginx配置https

一、开启nginx的ssl模块1.未安装过nginx,编译安装配置参数如下:./configure\prefix/usr/local/nginx\withpcre\withhttp\_ssl\_modulessl模块\withhttp\_stub\_status\_module\wit