nginx平滑升級
- 2020 年 3 月 30 日
- 筆記
升級目的
讓現有服務平滑過渡到高版本,減少服務漏洞,提高服務性能
讓其支持nginx最新特性 nginx threads模塊
獲取nginx1.7.2版本
wget http://nginx.org/download/nginx-1.17.2.tar.gz
編譯新版本
獲取老版本參數
[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
下載新版本
[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/
備份老版本nginx二進制文件
[root@leilei nginx-1.17.2]# cp -af /application/nginx/sbin/nginx ~
編譯新版本
./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 新增加的編譯項,用於開啟對線程池的支持
拷貝新版本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
發送信號啟動新版本的worker進程與老版本進程共同工作
kill -USR2 `cat /application/nginx-1.16/logs/nginx.pid`
檢查進程是否啟動
[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進程
檢查網站是否正常打開,如果打開則發送信號給老的進程要求優雅關閉
打開正常
發送信號給主進程,要求關閉
[root@leilei ~]# kill -QUIT `cat /application/nginx-1.16/logs/nginx.pid`
檢查 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
此時老進程已經優雅關閉,只保留了新的進程
檢查版本
[root@leilei ~]# nginx -v nginx version: nginx/1.17.2
測試新版本特性 增加 aio threads 參數
老版本配置該參數會報錯.新版本不會報錯,
vim /application/nginx/conf/nginx.conf
在server location 區塊添加以下參數
#aio 優化 aio threads; #aio 優化
還可以配置如下:
http塊下定義: thread_pool leilei threads=32 max_queue=65536; server區塊下調用: aio threads=leilei;
再次訪問:
更多詳情,請關注nginx官方網站 nginx.org
什麼場合適合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; }
可參考:https://www.nginx.com/blog/thread-pools-boost-performance-9x/