4. Nginx模块

Nginx官方模块

1.ngx_http_stub_status_module

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

此模块能够查看nginx对数据包处理的基本信息php

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

访问 /status,展现的数据以下html

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...

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

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

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

curl http://127.0.0.1/random/

3. ngx_http_sub_module

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

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

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...
HTTP协议 请求与链接 说明
HTTP 1.0 TCP不能复用 一个链接,一个请求
HTTP 1.1 顺序性TCP复用 一个链接,能够按顺序的发出多个请求
HTTP 2.0 多路TCP复用 一个链接,能够并行的发出多个请求

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

在以下的路由请求中,咱们限制用户的并发链接数分别为5个和10个。运维

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

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 的测试,无论是内网测试,仍是外网测试,都是可行的。curl

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_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...

该模块能够经过限制 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...

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

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/resourc...,这里就不一一展开介绍了