为了账号安全,请及时绑定邮箱和手机立即绑定

nginx 平滑升级 添加模块 调试(24)

标签:
Nginx

获取全套nginx教程,请访问瓦力博客

什么是nginx平滑升级?

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

平滑升级和添加模块是一样,升级需要下载最新的nginx包,而添加模块下载的当前版本的nginx包。小菜这篇博客是基于之前nginx的安装 卸载(3){:target="_blank"}
来进行,所以小伙伴们最好还是保持一致。升级是不需要卸载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 官方网站{:target="_blank"},如果升级就找最新稳定版本的包,如果添加模块就找当自己当前版本
一样的包。找到之后,复制下载链接地址,小菜找的是当前最稳定版本https://nginx.org/download/nginx-1.14.2.tar.gz

3.添加模块

nginx调试是很麻烦的,所以小菜就添加一个echo-nginx-module{:target="_blank"}模块,它的版本
最新版本{:target="_blank"},找到自己要下载包,复制下载链接地址,小菜找到最新包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;
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消