4. Nginx模块

Wesley13
• 阅读 575

Nginx官方模块

1.ngx_http_stub_status_module

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

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

#启用方法
location /status {
  stub_status;
}

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

Active connections: 4 
server accepts handled requests
310840 310840 481035 
Reading: 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 三个文件,则使用如下配置即可

location /random {
    root /usr/share/nginx/html;
    random_index on;
}

如下代码会随机返回 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

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

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

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个。

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

http {
    limit_conn_zone $binary_remote_addr zone=addr_conn:10m;
    ...
    server {
        ...
        location / {
            limit_conn addr_conn 5;
             ...
        }
        location ~ \.php$ {
            limit_conn addr_conn 10;
            ...
        }
    }
}

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

yum install httpd-tools
ab -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 中也能发现错误记录。如果错误日志过多,我们就需要排查一下访问量是否正常。如果正常的话,又需要考虑一下如何优化参数设置。

2017/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"
2017/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 会更多一些。

http {
    limit_req_zone $binary_remote_addr zone=addr_req:10m rate=1r/s;
    ...
    server {
        ...
        location / {
            limit_req zone=addr_req;
        }
    }
}

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

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

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

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

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

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

解决方法:

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

Nginx第三方模块

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

专题阅读

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
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
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
2年前
Nginx配置https
一、开启nginx的ssl模块1.未安装过nginx,编译安装配置参数如下:./configure\prefix/usr/local/nginx\withpcre\withhttp\_ssl\_modulessl模块\withhttp\_stub\_status\_module\wit
Stella981 Stella981
2年前
Nginx – rewrite 配置 URL重写及301跳转原理图
Nginx–rewrite配置URL重写官网:http://nginx.org/en/docs/http/ngx\_http\_rewrite\_module.html(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fnginx
Wesley13 Wesley13
2年前
P2P技术揭秘.P2P网络技术原理与典型系统开发
Modular.Java(2009.06)\.Craig.Walls.文字版.pdf:http://www.t00y.com/file/59501950(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.t00y.com%2Ffile%2F59501950)\More.E
Stella981 Stella981
2年前
Android蓝牙连接汽车OBD设备
//设备连接public class BluetoothConnect implements Runnable {    private static final UUID CONNECT_UUID  UUID.fromString("0000110100001000800000805F9B34FB");
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Logstash收集nginx访问日志和错误日志
1、收集访问日志1)、首先是要在nginx里面配置日志格式化输出log_formatmain"$http_x_forwarded_for|$time_local|$request|$status|$body_bytes_sent|$request_body|$content_length|$http_ref
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
2个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这