网站首页 文章专栏 编译安装nginx与nginx_upstream_check_module(心跳检测)
在《nginx均衡负载》一文中,使用了均衡负载+心跳检测的配置,但未说明心跳检测如何使用与安装,这里进行补充。
心跳检测的意义:如果有一台web服务器死机,或工作出现故障,服务能立即将其检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作,当服务器工作正常后再自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
也就是说:使用了均衡负载,虽然降低了服务器的访问压力,但是,当其中某一台业务机出现问题时,依旧可能被用户访问,导致web端显示服务器挂了。
nginx+nginx_upstream_check_module:
nginx+nginx_upstream_check_module,这个模块是由淘宝技术团队姚伟斌大神开发的,这个是他的github地址:https://github.com/yaoweibin/nginx_upstream_check_module
安装扩展模块,需要编译安装的nginx,版本自己选择(我的是1.14.0)
1.获取安装包
wget http://nginx.org/download/nginx-1.14.1.tar.gz wget https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/master
2.安装扩展依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
3.给nginx打个补丁(加入nginx+nginx_upstream_check_module)
1>.进入nginx-1.14目录(源码目录)
patch -p1 < /usr/download/nginx_upstream_check_module-master/check_1.14.0+.patch
ps:我的两个包下载在/usr/download/,这个自定义 ;master加压后的文件为nginx_upstream_check_module-master 里面包含了多个补丁版本,根据自己的nginx版本选择
2>.新建一个nginx的安装目录,我的在/usr/local/nginx-1.14.1,然后还在nginx的源码目录中执行
./configure --prefix=/usr/local/nginx-1.14.1 --add-module=/usr/download/nginx_upstream_check_module-master/
正常现象:
Configuration summary + using system PCRE library + OpenSSL library is not used + using system zlib library nginx path prefix: "/usr/local/nginx-1.14.1" nginx binary file: "/usr/local/nginx-1.14.1/sbin/nginx" nginx modules path: "/usr/local/nginx-1.14.1/modules" nginx configuration prefix: "/usr/local/nginx-1.14.1/conf" nginx configuration file: "/usr/local/nginx-1.14.1/conf/nginx.conf" nginx pid file: "/usr/local/nginx-1.14.1/logs/nginx.pid" nginx error log file: "/usr/local/nginx-1.14.1/logs/error.log" nginx http access log file: "/usr/local/nginx-1.14.1/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp"
大概率报错:
[root@localhost nginx-1.14.1]# ./configure --prefix=/usr/local/nginx-1.14.1 --add-module=/usr/download/nginx_upstream_check_module-master/ checking for OS + Linux 4.18.0-80.el8.x86_64 x86_64 checking for C compiler ... not found ./configure: error: C compiler cc is not found
解决办法:
yum -y install gcc gcc-c++ autoconf automake make
3>.编译 make
make
大概率出现的报错:
make[1]: 进入目录“/usr/download/nginx-1.14.1” cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I /usr/download/nginx_upstream_check_module-master/ -I objs \ -o objs/src/os/unix/ngx_user.o \ src/os/unix/ngx_user.c src/os/unix/ngx_user.c: 在函数‘ngx_libc_crypt’中: src/os/unix/ngx_user.c:26:7: 错误:‘struct crypt_data’没有名为‘current_salt’的成员 cd.current_salt[0] = ~salt[0]; ^ make[1]: *** [objs/Makefile:788:objs/src/os/unix/ngx_user.o] 错误 1
解决办法: 打开 src/os/unix/ngx_user.c
#ifdef __GLIBC__ /* work around the glibc bug */ cd.current_salt[0] = ~salt[0]; ##把这一行注释掉 #endif
4>.编译安装
make install
大概率报错:
igned int (*)(struct <匿名> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {或称 ‘void (*)(struct <匿名> *)’} [-Werror=cast-function-type] code->code = (ngx_http_script_code_pt) ^ src/http/ngx_http_script.c: 在函数‘ngx_http_script_add_full_name_code’中: src/http/ngx_http_script.c:1296:18: 错误:cast between incompatible function types from ‘size_t (*)(ngx_http_script_engine_t *)’ {或称 ‘long unsigned int (*)(struct <匿名> *)’} to ‘void (*)(ngx_http_script_engine_t *)’ {或称 ‘void (*)(struct <匿名> *)’} [-Werror=cast-function-type] code->code = (ngx_http_script_code_pt) ngx_http_script_full_name_len_code; ^ cc1:所有的警告都被当作是错误 make[1]: *** [objs/Makefile:893:objs/src/http/ngx_http_script.o] 错误 1 make[1]: 离开目录“/usr/download/nginx-1.14.1” make: *** [Makefile:8:build] 错误 2
解决办法:打开 vim objs/Makefile 把 -Werrori删掉 (-Werror,它要求GCC将所有的警告当成错误进行处理)
5.>启动nginx
在刚刚自定义的nginx安装目录里有生成的文件,其中,启动文件在sbin里
直接启动:./sbin/nginx -s reload
有可能出现报错:
root@localhost sbin]# ./nginx -s reload nginx: [error] open() "/usr/local/nginx-1.14.1/logs/nginx.pid" failed (2: No such file or directory)
解决办法:
[root@localhost sbin]# /usr/local/nginx-1.14.1/sbin/nginx -c /usr/local/nginx-1.14.1/conf/nginx.conf [root@localhost sbin]# cd ../logs/ [root@localhost logs]# ll 总用量 8 -rw-r--r-- 1 root root 0 1月 7 15:11 access.log -rw-r--r-- 1 root root 593 1月 7 15:30 error.log -rw-r--r-- 1 root root 6 1月 7 15:33 nginx.pid
然后就可以正常启动了
4.nginx配置
打开nginx.conf配置文件添加 include /etc/vhost/*.conf (个人习惯,引入一个配置文件)
然后根据在《nginx均衡负载》一文中跟均衡负载搭配使用。
5.检测可视化
在nginx的配置里加上一段
location /nastatus { check_status; access_log off; }
ps:/nastatus 自定义一个路径
然后打开web访问这个地址 比如 http://192.168.5.165/nastatus
转载请注明出处