什么是nginx平滑升级?

当官方发布一个新的稳定版,我们很有可能需要进行版本升级。新版本不仅会修复之前版本的bug,还会添加一些新的功能(当然还得视个人情况决定是否升级)。所谓的平滑升级就是不要让用户感受到,我们在 升级nginx,旧版本的nginx还在正常运行,当我们更新nginx版后,切换到新版本,这个过程不会中断服务,让用户感受不到服务卡顿。

平滑升级和添加模块是一样,升级需要下载最新的nginx包,而添加模块下载的当前版本的nginx包。小菜这篇博客是基于之前nginx的安装 卸载(3) 来进行,所以小伙伴们最好还是保持一致。升级是不需要卸载nginx的。

小菜是阿里云服务器,操作系统centos 7

1.安装编译源码环境

由于我们下载的是源码包,所以要对nginx进行编译,那么编译环境就必不可少。

安装gcc

yum install gcc 

安装PCRE

yum install pcre pcre-devel

安装zlib(zlib库提供了开发人员的压缩算法,在Nginx的各种模块中需要使用gzip压缩。如同安装PCRE一样,同样需要安装库和它的源代码:zlib和zlib-devel)

yum install zlib zlib-devel

安装OpenSSL(在Nginx中,如果服务器提供安全网页时则会用到OpenSSL库,我们需要安装库文件和它的开发安装包 openssl和 openssl-devel)

yum install openssl openssl-devel

2.下载nginx包

下载nginx包需要去nginx 官方网站,如果升级就找最新稳定版本的包,如果添加模块就找当自己当前版本 一样的包。找到之后,复制下载链接地址,小菜找的是当前最稳定版本https://nginx.org/download/nginx-1.14.2.tar.gz

3.添加模块

nginx调试是很麻烦的,所以小菜就添加一个echo-nginx-module模块,它的版本 最新版本,找到自己要下载包,复制下载链接地址,小菜找到最新包https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz

4.下载

我们将刚才找的包地址下载下来。下载到/opt/download

/opt
|-app
|-download
|-backup

进入download文件夹

cd /opt/download

下载nginx包

wget https://nginx.org/download/nginx-1.14.2.tar.gz

解压nginx包

tar -xvf nginx-1.14.2.tar.gz

下载echo-nginx-module模块

wget https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz

解压echo-nginx-module包

tar -xvf v0.61.tar.gz

5.编译nginx源码

为了和之前nginx版本的参数保持一致,我们先查看之前的nginx编译参数

nginx -V

输出:

` –prefix=/etc/nginx –sbin-path=/usr/sbin/nginx –modules-path=/usr/lib64/nginx/modules –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx.pid –lock-path=/var/run/nginx.lock –http-client-body-temp-path=/var/cache/nginx/client_temp –http-proxy-temp-path=/var/cache/nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp –http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp –http-scgi-temp-path=/var/cache/nginx/scgi_temp –user=nginx –group=nginx –with-compat –with-file-aio –with-threads –with-http_addition_module –with-http_auth_request_module –with-http_dav_module –with-http_flv_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_mp4_module –with-http_random_index_module –with-http_realip_module –with-http_secure_link_module –with-http_slice_module –with-http_ssl_module –with-http_stub_status_module –with-http_sub_module –with-http_v2_module –with-mail –with-mail_ssl_module –with-stream –with-stream_realip_module –with-stream_ssl_module –with-stream_ssl_preread_module –with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC’ –with-ld-opt=’-Wl,-z,relro -Wl,-z,now -pie’ `

进入解压好nginx-1.14.2文件夹

cd nginx-1.14.2

将echo-nginx-module添加到编译参数中去,添加到最后

` –prefix=/etc/nginx –sbin-path=/usr/sbin/nginx –modules-path=/usr/lib64/nginx/modules –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx.pid –lock-path=/var/run/nginx.lock –http-client-body-temp-path=/var/cache/nginx/client_temp –http-proxy-temp-path=/var/cache/nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp –http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp –http-scgi-temp-path=/var/cache/nginx/scgi_temp –user=nginx –group=nginx –with-compat –with-file-aio –with-threads –with-http_addition_module –with-http_auth_request_module –with-http_dav_module –with-http_flv_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_mp4_module –with-http_random_index_module –with-http_realip_module –with-http_secure_link_module –with-http_slice_module –with-http_ssl_module –with-http_stub_status_module –with-http_sub_module –with-http_v2_module –with-mail –with-mail_ssl_module –with-stream –with-stream_realip_module –with-stream_ssl_module –with-stream_ssl_preread_module –with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC’ –with-ld-opt=’-Wl,-z,relro -Wl,-z,now -pie’ –add-module=/opt/download/echo-nginx-module-0.61 `

编译

./configure 编译参数

./configure –prefix=/etc/nginx –sbin-path=/usr/sbin/nginx –modules-path=/usr/lib64/nginx/modules –conf-path=/etc/nginx/nginx.conf –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx.pid –lock-path=/var/run/nginx.lock –http-client-body-temp-path=/var/cache/nginx/client_temp –http-proxy-temp-path=/var/cache/nginx/proxy_temp –http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp –http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp –http-scgi-temp-path=/var/cache/nginx/scgi_temp –user=nginx –group=nginx –with-compat –with-file-aio –with-threads –with-http_addition_module –with-http_auth_request_module –with-http_dav_module –with-http_flv_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_mp4_module –with-http_random_index_module –with-http_realip_module –with-http_secure_link_module –with-http_slice_module –with-http_ssl_module –with-http_stub_status_module –with-http_sub_module –with-http_v2_module –with-mail –with-mail_ssl_module –with-stream –with-stream_realip_module –with-stream_ssl_module –with-stream_ssl_preread_module –with-cc-opt=’-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong –param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC’ –with-ld-opt=’-Wl,-z,relro -Wl,-z,now -pie’ –add-module=/opt/download/echo-nginx-module-0.61

安装

make && make install

检测是否安装成功

nginx -v

输出nginx version: nginx/1.14.2就算安装成功了

6.检测echo-nginx-module

前面几步我们将echo-nginx-module模块编译到nginx,下面检测echo-nginx-module

/etc/nginx/conf.d
|-echo.conf

echo.conf

server {
    listen       9001;
    server_name  localhost;


    location / { 
        default_type text/html;
        echo "<h1>欢迎来到瓦力博客</h1>";
    }   

}

重启nginx

nginx -s reload -c /etc/nginx/nginx.conf

检测9001端口,看9001端口是否监听

ss -luntp | grep nginx

在页面上输入http://walidream.com:9001,输出中文可能会有乱码,出现乱码需要将编码改为utf-8

ssl

7.echo调试

我们有了echo模块,在调试nginx时就变得容易多了。

输出字符串

echo "hello";

输入变量

echo $remote_addr;

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)