[关闭]
@cdmonkey 2022-03-22T03:18:50.000000Z 字数 5295 阅读 1455

Nginx-动态上下线

Nginx


http://hugoren.iteye.com/blog/2289411
http://hugoren.iteye.com/blog/2289960
https://xiangxianzui.github.io/2017/11/nginx-lua%E5%8A%A8%E6%80%81%E6%94%B9%E5%8F%98upstream/

https://github.com/cubicdaiya/ngx_dynamic_upstream
https://github.com/yaoweibin/nginx_upstream_check_module

首先下载相关软件包。

LuaJIT-2.0.5 http://luajit.org/download.html
ngx_devel_kit (NDK) module https://github.com/simplresty/ngx_devel_kit/releases
lua-nginx-module https://github.com/openresty/lua-nginx-module/releases

首先要安装好 LuaJIT,然后解压 NDK modulelua-nginx-module,继续。

Install LuaJIT

http://luajit.org/install.html

  1. [root@testnginx01 tools]# tar zxvf LuaJIT-2.0.5.tar.gz
  2. [root@testnginx01 tools]# cd LuaJIT-2.0.5
  3. make PREFIX=/usr/local/nginx/lua/luajit
  4. make install PREFIX=/usr/local/nginx/lua/luajit

然后进行编译。下面包含生产中完整之三个第三方模块。首先要准备好这三个模块:

  1. [root@testnginx01 tools]# unzip ngx_dynamic_upstream-master.zip
  2. [root@testnginx01 tools]# unzip nginx_upstream_check_module-master.zip
  3. [root@testnginx01 tools]# unzip nginx-goodies-nginx-sticky-module-ng-08a395c66e42.zip
  4. [root@testnginx01 tools]# mv nginx-goodies-nginx-sticky-module-ng-08a395c66e42 nginx-sticky-module

首先处理 nginx-sticky-module

  1. [root@testnginx01 tools]# cd nginx-sticky-module
  2. [root@testnginx01 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_upstream_check_module

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

最后为安装 ngx_dynamic_upstream 进行准备:

  1. [root@testnginx01 tools]# tar zxvf ngx_devel_kit-0.3.0.tar.gz
  2. [root@testnginx01 tools]# tar zxvf lua-nginx-module-0.10.13.tar.gz

匹配环境变量:

  1. [root@testnginx01 ~]# vim .bash_profile
  2. # 加入如下内容,告诉 Nginx 编译程序在哪里能找到 LuaJIT。
  3. export LUAJIT_LIB=/usr/local/nginx/lua/luajit/lib
  4. export LUAJIT_INC=/usr/local/nginx/lua/luajit/include/luajit-2.0
  5. # 配置生效:
  6. [root@testnginx01 ~]# . .bash_profile

make

  1. ./configure \
  2. --prefix=/usr/local/nginx \
  3. --user=nginx --group=nginx \
  4. --with-openssl=/root/tools/openssl-1.0.2l \
  5. --with-http_ssl_module \
  6. --with-http_gunzip_module \
  7. --with-http_gzip_static_module \
  8. --with-http_stub_status_module \
  9. --with-pcre=/root/tools/pcre-8.39 \
  10. --with-http_realip_module \
  11. --with-ld-opt="-Wl,-rpath,/usr/local/nginx/lua/luajit/lib" \
  12. --add-module=/root/tools/nginx_upstream_check_module-master \
  13. --add-module=/root/tools/nginx-sticky-module \
  14. --add-module=/root/tools/ngx_devel_kit-0.3.0 \
  15. --add-module=/root/tools/lua-nginx-module-0.10.13 \
  16. --add-module=/root/tools/ngx_dynamic_upstream-master
  17. # 完成配置后仅只执行 make 操作:
  18. make

进行备份及替换操作:

  1. [root@testnginx01 ~]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
  2. #
  3. [root@testnginx01 ~]# killall nginx
  4. [root@testnginx01 ~]# cp tools/nginx-1.12.1/objs/nginx /usr/local/nginx/sbin/nginx
  1. [root@testnginx01 ~]# nginx -V
  2. nginx version: nginx/1.12.1
  3. built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)
  4. built with OpenSSL 1.0.2l 25 May 2017
  5. TLS SNI support enabled
  6. configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-openssl=/root/tools/openssl-1.0.2l --with-http_ssl_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --with-pcre=/root/tools/pcre-8.39 --with-http_realip_module --with-ld-opt=-Wl,-rpath,/usr/local/nginx/lua/luajit/lib --add-module=/root/tools/nginx_upstream_check_module-master --add-module=/root/tools/nginx-sticky-module --add-module=/root/tools/ngx_devel_kit-0.3.0 --add-module=/root/tools/lua-nginx-module-0.10.13 --add-module=/root/tools/ngx_dynamic_upstream-master

测试

首先测试一下 Lua

HttpEchoModule

这个模块用于调试。

https://github.com/openresty/echo-nginx-module
http://tool.oschina.net/uploads/apidocs/nginx-zh/HttpEchoModule.htm#Installation

安装很简单,先把 zip 压缩包解压缩,然后于配置时直接指定路径即可。

  1. [root@PBSNGX03 tools]# unzip echo-nginx-module-master.zip
  2. # Configure:
  3. [root@PBSNGX03 tools]# cd nginx-1.12.2
  4. [root@PBSNGX03 nginx-1.12.2]# ./configure
  5. ...
  6. --add-module=/root/tools/echo-nginx-module-master

生产

  1. curl "https://stageloan.vbillbank.com/dynamic?upstream=stageloan"
  2. server 18.10.30.150:8080;
  3. server 18.10.30.151:8080;
  1. curl "https://stageloan.vbillbank.com/dynamic?upstream=stageloan&server=18.10.30.150:8080&down="
  2. server 18.10.30.150:8080 weight=1 max_fails=1 fail_timeout=10 down;
  3. server 18.10.30.151:8080 weight=1 max_fails=1 fail_timeout=10;

报错

于 CentOS7 编译安装时,当执行 make 时,会报错误信息:

  1. ...
  2. /root/tools/pcre-8.39/missing: line 81: aclocal-1.15: command not found
  3. WARNING: 'aclocal-1.15' is missing on your system.
  4. You should only need it if you modified 'acinclude.m4' or
  5. 'configure.ac' or m4 files included by 'configure.ac'.
  6. The 'aclocal' program is part of the GNU Automake package:
  7. <http://www.gnu.org/software/automake>
  8. It also requires GNU Autoconf, GNU m4 and Perl in order to run:
  9. <http://www.gnu.org/software/autoconf>
  10. <http://www.gnu.org/software/m4/>
  11. <http://www.perl.org/>
  12. make[2]: *** [aclocal.m4] Error 127
  13. make[2]: Leaving directory '/root/tools/pcre-8.39'
  14. make[1]: *** [/root/tools/pcre-8.39/Makefile] Error 2
  15. make[1]: Leaving directory '/root/tools/nginx-1.12.1'
  16. make: *** [build] Error 2

结论是重新解压 pcre 就行了。
可能还需要重新解压 openssl。

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