@cdmonkey
2018-03-28T10:43:15.000000Z
字数 5204
阅读 4020
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。
首先,查看之前编译的一些参数,比如:
[root@test-ngx extra]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.9.15
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
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添加模块。
[root@test-ngx ~]# killall nginx
将同版本的nginx包从新解压一份出来,并进入nginx源码目录:
[root@test-ngx ~]# cd tools/nginx-1.9.15
将要安装的模块下载并解压:
unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
# Rename:
mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-module
注意:健康检查模块并不支持第三方的负载均衡算法,好在作者提供了方法。请参见
GitHub
上的文档。
https://github.com/yaoweibin/nginx_upstream_check_module
具体操作:
[root@test-ngx tools]# cd nginx-sticky-module
[root@test-ngx nginx-sticky-module]# patch -p0 < ../nginx_upstream_check_module-master/nginx-sticky-module.patch
patching file ngx_http_sticky_module.c
Hunk #1 succeeded at 15 with fuzz 2 (offset 5 lines).
Hunk #2 succeeded at 304 (offset 12 lines).
Hunk #3 succeeded at 330 (offset 12 lines).
Hunk #4 succeeded at 352 (offset 12 lines).
完成上面的操作后,既能够进行编译了。将要编译的参数重新添加到后面。添加模块时,之前的配置和模块也需要加上,因为这些都需要写到nginx
这个二进制文件中。
./configure \
--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
然后是最后一步:
make
注意:这里只能执行
make
,千万不要make install
,否则就是覆盖安装了。
我们编译完成之后,在当前目录下会有一个objs
,里面有nginx
文件,就是我们make
生成的二进制文件,然后将这个文件拷贝到/usr/local/nginx/sbin
目录(之前的nginx
最好备份一下,以免发生错误好恢复)。
[root@test-ngx nginx-1.9.15]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
[root@test-ngx nginx-1.9.15]# cp objs/nginx /usr/local/nginx/sbin/
至此nginx
添加模块成功。我们直接在nginx配置文件中引用就好。
查看新的编译参数:
[root@test-ngx nginx-1.9.15]# nginx -V
nginx version: nginx/1.9.15
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)
built with OpenSSL 1.0.1u 22 Sep 2016 (running with OpenSSL 1.0.1e-fips 11 Feb 2013)
TLS SNI support enabled
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
目录,以防万一。
[root@PBSNGX03 ~]# cp -a /usr/local/nginx /usr/local/nginx.bak
重新编译:
[root@PBSNGX03 tools]# tar zxvf nginx-1.12.2.tar.gz
[root@PBSNGX03 tools]# unzip nginx_upstream_check_module-master.zip
[root@PBSNGX03 tools]# cd nginx-1.12.2
[root@PBSNGX03 nginx-1.12.2]# patch -p1 < ../nginx_upstream_check_module-master/check_1.12.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h
开始配置及编译操作(千万不要执行 make install
指令)
./configure \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_gunzip_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre=/root/tools/pcre-8.39 \
--with-openssl=/root/tools/openssl-1.0.2l \
--add-module=/root/tools/nginx_upstream_check_module-master
最后进行编译:
make
然后将上一步生成之新版本 nginx
文件复制过来:
[root@PBSNGX03 nginx-1.12.2]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.old
[root@PBSNGX03 nginx-1.12.2]# cp objs/nginx /usr/local/nginx/sbin/
经过测试,这时服务正常,并且测试新 nginx
文件是否正常:
[root@PBSNGX03 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
可直接使用 make upgrade
进行平滑升级:
[root@PBSNGX03 nginx-1.12.2]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
make: *** [upgrade] Error 1
出现错误,查看错误日志会看到:
http://jpuyy.com/2016/05/nginx-upgrade-failed.html
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
命令启动的。如果是这样就没办法平滑升级了。
如果没有报错,正常输出应该是这样:
[root@PBSNGX03 nginx-1.12.2]# make upgrade
/usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
参考资料:
http://coderschool.cn/1728.html
http://www.cnblogs.com/shiyiwen/p/5039605.html