nginx aio模块添加与配置

1. 升级目的

让现有服务平滑过渡到高版本,减少服务漏洞,提高服务性能
让其支持nginx最新特性 nginx threads模块

2. 获取nginx1.7.2版本

wget http://nginx.org/download/nginx-1.17.2.tar.gz
[root@leilei tools]# wget http://nginx.org/download/nginx-1.17.2.tar.gz
[root@leilei tools]# tar -xf nginx-1.17.2.tar.gz
[root@leilei tools]# cd nginx-1.17.2/

3. 编译新版本

获取老版本参数:
[root@leilei nginx-1.17.2]# nginx  -V
nginx version: nginx/1.16.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/application/nginx-1.16 --user=www --group=www --with-http_image_filter_module --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module

4.备份老版本nginx二进制文件

[root@leilei nginx-1.17.2]# cp -af /application/nginx/sbin/nginx  ~

5. 编译新版本

./configure --prefix=/application/nginx-1.16 --user=www --group=www --with-http_image_filter_module --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-threads
make 
make结束就够了  千万不要 make install
		千万不要 make install
		千万不要 make install
			  

看到:
objs/src/http/modules/ngx_http_upstream_least_conn_module.o 
objs/src/http/modules/ngx_http_upstream_random_module.o 
objs/src/http/modules/ngx_http_upstream_keepalive_module.o 
objs/src/http/modules/ngx_http_upstream_zone_module.o 
objs/src/http/modules/ngx_http_stub_status_module.o 
objs/ngx_modules.o 
-ldl -lpthread -lpthread -lcrypt -lpcre -lssl -lcrypto -ldl -lpthread -lz -lgd 
-Wl,-E
sed -e "s|%%PREFIX%%|/application/nginx-1.16|" 
	-e "s|%%PID_PATH%%|/application/nginx-1.16/logs/nginx.pid|" 
	-e "s|%%CONF_PATH%%|/application/nginx-1.16/conf/nginx.conf|" 
	-e "s|%%ERROR_LOG_PATH%%|/application/nginx-1.16/logs/error.log|" 
	< man/nginx.8 > objs/nginx.8
make[1]: Leaving directory `/server/tools/nginx-1.17.2'

到这里编译完成
--with-threads
新增加的编译项,用于开启对线程池的支持

6. 拷贝新版本nginx二进制文件到老版本中

[root@leilei nginx-1.17.2]# cp -af objs/nginx /application/nginx-1.16/sbin/nginx
cp: overwrite ‘/application/nginx-1.16/sbin/nginx’? y

7. 发送信号启动新版本的worker进程与老版本进程共同工作

kill -USR2 `cat /application/nginx-1.16/logs/nginx.pid`

8.检查进程是否启动

[root@leilei ~]# ps -ef |grep nginx
root      53404      1  0 15:05 ?        00:00:00 nginx: master process /application/nginx-1.16/sbin/nginx
www       53405  53404  0 15:05 ?        00:00:00 nginx: worker process
root      53411  53404  0 15:06 ?        00:00:00 nginx: master process /application/nginx-1.16/sbin/nginx
root      53441  23417  0 15:14 pts/0    00:00:00 vim nginx.conf
root      53617  53494  0 16:35 pts/2    00:00:00 grep --color=auto nginx

已经启动了两个master进程

9. 检查网站是否正常打开,如果打开则发送信号给老的进程要求优雅关闭

打开正常:

10. 发送信号给主进程,要求关闭

[root@leilei ~]# kill -QUIT `cat /application/nginx-1.16/logs/nginx.pid`

11.检查 nginx 进程情况

[root@leilei ~]# ps -ef |grep nginx
root      53404      1  0 15:05 ?        00:00:00 nginx: master process /application/nginx-1.16/sbin/nginx
www       53405  53404  0 15:05 ?        00:00:00 nginx: worker process
root      53441  23417  0 15:14 pts/0    00:00:00 vim nginx.conf
root      53621  53494  0 16:39 pts/2    00:00:00 grep --color=auto nginx

此时老进程已经优雅关闭,只保留了新的进程

12.检查版本

[root@leilei ~]# nginx -v
nginx version: nginx/1.17.2

13.测试新版本特性 增加 aio threads 参数

老版本配置该参数会报错.新版本不会报错,
vim /application/nginx/conf/nginx.conf

在server location 区块添加以下参数
#aio 优化
aio threads;
#aio 优化

配置如下:

1.http块下定义:
thread_pool leilei threads=32 max_queue=65536;

2.server区块下调用:
aio threads=leilei;

再次访问:

14.什么场合适合send file 什么场合适合 aio

启用aio时会自动启用directio,小于directio定义的大小的文件则采用sendfile进行发送,超过或等于directio定义的大小的文件,将采用aio线程池进行发送,也就是说aio和directio适合大文件下载。因为大文件不适合进入操作系统的buffers/cache,这样会浪费内存,而且Linux AIO(异步磁盘IO)也要求使用directio的形式。
sendfile_max_chunk可以减少阻塞调用sendfile()所花费的最长时间。因为Nginx不会尝试一次将整个文件发送出去,而是每次发送大小为256KB的块数据。
注意,Nginx从1.7.11开始为AIO引入了线程池支持,能够使用多线程读取和发送文件,以免工人进程被阻塞。要启用多线程支持,configure时需要显式加入–with-threads选项。

sendfile配置:
http{
     sendfile on;
}

更多详情,请关注nginx官方网站nginx.org
可参考: nginx aio

原文地址:https://www.cnblogs.com/superlinux/p/12727530.html