[关闭]
@cdmonkey 2018-03-28T02:43:15.000000Z 字数 5204 阅读 3918

Nginx-重新编译安装模块及平滑升级

Nginx


我们需要额外安装“nginx-sticky-module”模块。

下载地址:
https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/downloads/?tab=downloads

https://www.lydlinux.com/post/46.html
https://www.ruby-forum.com/topic/4403244
http://seanlook.com/2015/06/02/nginx-cache-check

Nginx安装成功后,发现有一些其他模块没有编译进去,或者想额外添加一些模块,这时候就要重新编译nginx。

首先,查看之前编译的一些参数,比如:

  1. [root@test-ngx extra]# /usr/local/nginx/sbin/nginx -V
  2. nginx version: nginx/1.9.15
  3. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
  4. built with OpenSSL 1.0.1e-fips 11 Feb 2013
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre=/root/tools/pcre-8.38 --add-module=/root/tools/nginx_upstream_check_module-master

我现在要新增“nginx-sticky-module”模块,那么我只要把参数添加进去然后重新编译即可,需要注意的是原先编译过的参数也要加进来。过程如下:

首先,要先停止nginx服务。在给nginx添加模块。

  1. [root@test-ngx ~]# killall nginx

将同版本的nginx包从新解压一份出来,并进入nginx源码目录:

  1. [root@test-ngx ~]# cd tools/nginx-1.9.15

将要安装的模块下载并解压:

  1. unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
  2. # Rename:
  3. mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-module

注意:健康检查模块并不支持第三方的负载均衡算法,好在作者提供了方法。请参见GitHub上的文档。

https://github.com/yaoweibin/nginx_upstream_check_module

具体操作:

  1. [root@test-ngx tools]# cd nginx-sticky-module
  2. [root@test-ngx nginx-sticky-module]# patch -p0 < ../nginx_upstream_check_module-master/nginx-sticky-module.patch
  3. patching file ngx_http_sticky_module.c
  4. Hunk #1 succeeded at 15 with fuzz 2 (offset 5 lines).
  5. Hunk #2 succeeded at 304 (offset 12 lines).
  6. Hunk #3 succeeded at 330 (offset 12 lines).
  7. Hunk #4 succeeded at 352 (offset 12 lines).

完成上面的操作后,既能够进行编译了。将要编译的参数重新添加到后面。添加模块时,之前的配置和模块也需要加上,因为这些都需要写到nginx这个二进制文件中。

  1. ./configure \
  2. --prefix=/usr/local/nginx \
  3. --user=nginx --group=nginx \
  4. --with-http_ssl_module \
  5. --with-http_stub_status_module \
  6. --with-pcre=/root/tools/pcre-8.38 \
  7. --add-module=/root/tools/nginx_upstream_check_module-master \
  8. --add-module=/root/tools/nginx-sticky-module

然后是最后一步:

  1. make

注意:这里只能执行make,千万不要make install,否则就是覆盖安装了。

我们编译完成之后,在当前目录下会有一个objs,里面有nginx文件,就是我们make生成的二进制文件,然后将这个文件拷贝到/usr/local/nginx/sbin目录(之前的nginx最好备份一下,以免发生错误好恢复)。

  1. [root@test-ngx nginx-1.9.15]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
  2. [root@test-ngx nginx-1.9.15]# cp objs/nginx /usr/local/nginx/sbin/

至此nginx添加模块成功。我们直接在nginx配置文件中引用就好。

查看新的编译参数:

  1. [root@test-ngx nginx-1.9.15]# nginx -V
  2. nginx version: nginx/1.9.15
  3. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
  4. built with OpenSSL 1.0.1u 22 Sep 2016 (running with OpenSSL 1.0.1e-fips 11 Feb 2013)
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre=/root/tools/pcre-8.38 --add-module=/root/tools/nginx_upstream_check_module-master --add-module=/root/tools/nginx-sticky-module

平滑升级

https://www.jianshu.com/p/6d8c0ea31a42
https://my.oschina.net/plutonji/blog/548049

首先要进行备份,拷贝下 nginx 目录,以防万一。

  1. [root@PBSNGX03 ~]# cp -a /usr/local/nginx /usr/local/nginx.bak

重新编译:

  1. [root@PBSNGX03 tools]# tar zxvf nginx-1.12.2.tar.gz
  2. [root@PBSNGX03 tools]# unzip nginx_upstream_check_module-master.zip
  3. [root@PBSNGX03 tools]# cd nginx-1.12.2
  4. [root@PBSNGX03 nginx-1.12.2]# patch -p1 < ../nginx_upstream_check_module-master/check_1.12.1+.patch
  5. patching file src/http/modules/ngx_http_upstream_hash_module.c
  6. patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
  7. patching file src/http/modules/ngx_http_upstream_least_conn_module.c
  8. patching file src/http/ngx_http_upstream_round_robin.c
  9. patching file src/http/ngx_http_upstream_round_robin.h

开始配置及编译操作(千万不要执行 make install 指令)

  1. ./configure \
  2. --prefix=/usr/local/nginx \
  3. --user=nginx --group=nginx \
  4. --with-http_ssl_module \
  5. --with-http_gunzip_module \
  6. --with-http_realip_module \
  7. --with-http_gzip_static_module \
  8. --with-http_stub_status_module \
  9. --with-pcre=/root/tools/pcre-8.39 \
  10. --with-openssl=/root/tools/openssl-1.0.2l \
  11. --add-module=/root/tools/nginx_upstream_check_module-master

最后进行编译:

  1. make

然后将上一步生成之新版本 nginx 文件复制过来:

  1. [root@PBSNGX03 nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
  2. [root@PBSNGX03 nginx-1.12.2]# cp objs/nginx /usr/local/nginx/sbin/

经过测试,这时服务正常,并且测试新 nginx 文件是否正常:

  1. [root@PBSNGX03 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

平滑升级操作

可直接使用 make upgrade 进行平滑升级:

  1. [root@PBSNGX03 nginx-1.12.2]# make upgrade
  2. /usr/local/nginx/sbin/nginx -t
  3. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  4. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  5. kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
  6. sleep 1
  7. test -f /usr/local/nginx/logs/nginx.pid.oldbin
  8. make: *** [upgrade] Error 1

出现错误,查看错误日志会看到:
http://jpuyy.com/2016/05/nginx-upgrade-failed.html

  1. 2018/03/15 17:00:47 [alert] 20435#0: execve() failed while executing new binary process "nginx" (2: No such file or directory)

其原因是之前没有使用 /usr/local/nginx/sbin/nginx 启动,而是通过场景变量中那个 nginx 命令启动的。如果是这样就没办法平滑升级了。

如果没有报错,正常输出应该是这样:

  1. [root@PBSNGX03 nginx-1.12.2]# make upgrade
  2. /usr/local/nginx/sbin/nginx -t
  3. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  4. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  5. kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
  6. sleep 1
  7. test -f /usr/local/nginx/logs/nginx.pid.oldbin
  8. kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`

参考资料:
http://coderschool.cn/1728.html
http://www.cnblogs.com/shiyiwen/p/5039605.html

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注