小菜简单介绍nginx模块,模块太多,就不一一介绍。这里是nginx的所有模块http://nginx.org/en/docs/。在后面的操作中用到了ab压力测试工具,不会的伙伴请查看ab这篇博客

nginx模块在编译的时候就作为选项被添加进去,查看添加哪些模块

nginx -V

1.http_stub_status_module配置

syntax: stub_status;
default: -
contentx: server,location

下面小菜配置到自己walidream.com的域名下,展示一下做什么用处的。根据语法知道配置在server,和location下,小菜就配置location下面

cd /etc/nginx/conf.d/

vim server1.conf

将这段代码添加到 location /下面

location /mystatus {
	stub_status;
}   

然后在浏览器上输入walidream.com/mystatus

ssl

Active connections: 2  #连接数
server accepts handled requests
 94 94 86  #总握手次数  总连接数  总请求数
Reading: 0 Writing: 1 Waiting: 1  #读的数   写的数  等待的数

2.random_index_module

语法

syntax: random_index on|off;
default: random_index off;
context: location

随机的获取目录下面后缀为.html,进入配置

location / {
	root /var/share/nginx/html/server;
	random_index on;
}

在/var/share/nginx/html/server目录下新建1.html,2.html,3.html内容分别为

<!--这个是1.html内容 -->
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body style="background:red;">
	
</body>
</html>

<!--这个是2.html内容 -->

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body style="background:blue;">
	
</body>
</html>

<!--这个是3.html内容 -->

<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body style="background:blank;">
	
</body>
</html>

然后在浏览器输入 walidream.com,多按几次f5刷新看看:

3.http_sub_module

替换返回html的内容

1.sub_filter 作用是替换返回html的内容

syntax: sub_filter string replacement;  
default: -
contentx: http,server,location

2.sub_filter_last_modified用来校验服务端的html内容是否有更新,主要用于缓存

syntax: sub_filter_last_modified on|off;
default: sub_filter_last_modified off;
context: http,server,location

3.sub_filter_once 只匹配第一个还是匹配所有

syntax: sub_filter_once on|off;
default: sub_filter_once on;
contentx: http,server,location;

4.sub_filter_types 替换的类型

Syntax:	sub_filter_types mime-type ...;
Default: sub_filter_types text/html;
Context: http, server, location

除了text/html之外,还在具有指定MIME类型的响应中启用字符串替换。特殊值*匹配任何MIME类型。

示例配置

将返回内容中Golang字符串替换成Nginx,只匹配一次。

location / {
	root /opt/app/code;
	index sub_module.html sub_module.htm;
	sub_filter 'Golang' 'Nginx';
	sub_filter_once on;
}

4.http_limit_conn_module

连接频率限制

1.limit_conn

Syntax:	limit_conn zone number;
Default: 
Context: http, server, location

2.limit_conn_log_level

Syntax:	limit_conn_log_level info | notice | warn | error;
Default: limit_conn_log_level error;
Context: http, server, location

3.limit_conn_status

Syntax:	limit_conn_status code;
Default: limit_conn_status 503;
Context: http, server, location

4.limit_conn_zone

Syntax:	limit_conn_zone key zone=name:size;
Default: 
Context: http

5.limit_zone

Syntax:	limit_zone name $variable size;
Default: 
Context: http

示例配置

$binary_remote_addrremote_addr占用的字节少。

limit_conn_zone $binary_remote_addr zone=conn_zone:1m;

server {
	listen		80;
	server_name	walidream.com;
	
	location / {
		root /opt/app/code;
		limit_conn conn_zone 1;
		index sub_module.html sub_module.htm;
	}
}

用压力测试工具ab进行压力测试。

ab -n 100 -c 20 http://walidream/sub_module.html

5.limit_req_module

请求频率限制

1.limit_req

Syntax:	limit_req zone=name [burst=number] [nodelay | delay=number];
Default: 
Context: http, server, location

2.limit_req_log_level

Syntax:	limit_req_log_level info | notice | warn | error;
Default: limit_req_log_level error;
Context: http, server, location

3.limit_req_status

Syntax:	limit_req_status code;
Default: limit_req_status 503;
Context: http, server, location

4.limit_req_zone

Syntax:	limit_req_zone key zone=name:size rate=rate [sync];
Default: 
Context: http

示例配置

$binary_remote_addrremote_addr占用的字节少。

limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;

server {
	listen		80;
	server_name	walidream.com;
	
	location / {
		root /opt/app/code;
		#limit_req zone=req_zone burst=3 nodelay;
		#limit_req zone=req_zone burst=3 nodelay;
		#limit_req zone=req_zone burst=3;
		limit_req zone=req_zone;     
		index sub_module.html sub_module.htm;
	}
}

用压力测试工具ab进行压力测试。

ab -n 100 -c 20 http://walidream/sub_module.html

输出

Server Software:        nginx/1.14.1
Server Hostname:        walidream.com
Server Port:            80

Document Path:          /sub_module.html
Document Length:        143 bytes

Concurrency Level:      20
Time taken for tests:   1.009 seconds
Complete requests:      100
Failed requests:        98
   (Connect: 0, Receive: 0, Length: 98, Exceptions: 0)
Write errors:           0
Non-2xx responses:      98
Total transferred:      72388 bytes
HTML transferred:       52912 bytes
Requests per second:    99.06 [#/sec] (mean)
Time per request:       201.895 [ms] (mean)
Time per request:       10.095 [ms] (mean, across all concurrent requests)
Transfer rate:          70.03 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   10 100.0      0    1001
Processing:     0    1   0.1      1       2
Waiting:        0    1   0.2      1       2
Total:          1   12  99.9      2    1001

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      2
  95%      2
  98%      3
  99%   1001
 100%   1001 (longest request)

nginx教程

nginx环境搭建(1) nginx基础知识(2) nginx的安装 卸载(3) nginx的基本参数使用(4) nginx分析默认配置(5) nginx 虚拟主机配置(6) nginx 日志(7) nginx 模块(8) nginx 访问控制(9) nginx 静态资源web服务(10) nginx 缓存(11) nginx 跨域访问(12) nginx 防盗链(13) nginx 正向,反向代理配置(14) nginx 代理缓存配置(15) nginx websocket(16) nginx fastcgi(17) nginx 搭建wordPress博客(18) nginx Fastcgi缓存配置(19) nginx uwsgi反向代理(20) nginx 负载均衡(21) [深] nginx 动静分离(22) [深] nginx rewrite规则(23) [深] nginx 平滑升级 添加模块 调试(24) [深] nginx secure_link_module模块(25) [深] nginx geoip_module模块(26) [深] nginx https(27) [深] nginx与lua的开发(28) [架] nginx常见问题(29) [架] nginx性能优化(30) [架] nginx 安全(31) [架] nginx 反向代理gRpc(32)