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.html、bbb.html、ccc.html 三个文件,则使用如下配置即可
1location /random { 2 root /usr/share/nginx/html; 3 random_index on; 4}
如下代码会随机返回 aaa、bbb、ccc 这三个html之一
curl http://127.0.0.1/random/
3. ngx_http_sub_module
用于替换掉响应内容中的指定字符串。
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 ,我们在 nginx 的 error.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
该模块可以通过限制 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/,这里就不一一展开介绍了
专题阅读
- 1. Nginx的优点
- 2. Nginx的安装与开机自启
- 3. Nginx目录和配置语法
- 4. Nginx模块
- 5. Nginx静态资源处理
- 6. Nginx浏览器缓存原理
- 7. Nginx资源的跨域访问
- 8. Nginx资源的防盗链
- 9. Nginx代理
- 10. Nginx负载均衡
- 11. Nginx缓存
- 12. Nginx动静分离
- 13. Nginx Rewrite
- 14. Nginx Secure Link
- 15. Nginx Geo
- 16. Nginx HTTPS服务
- 17. Nginx与Lua开发
- 18. Nginx与Lua灰度发布
- 19. Nginx常见错误
- 20. Nginx性能优化
- 21. Nginx安全管理