Nginx安装及 nginx_upstream_check_module(nginx主动健康检查模块)和nginx-upsync-module(nginx服务发现模块)安装
1.在 /opt/下安装,进入/opt/目录
cd /opt/
获取nginx-1.19.6.tar.gz安装包
wget http://nginx.org/download/nginx-1.19.6.tar.gz
2.下载对应模块 nginx_upstream_check_module:nginx主动健康检查模块和nginx-upsync-module:nginx服务发现模块
普通地址: git clone https://github.com/xiaokai-wang/nginx_upstream_check_module.git
加速地址: git clone https://github.com.cnpmjs.org/xiaokai-wang/nginx_upstream_check_module.git
普通地址: git clone https://github.com/weibocom/nginx-upsync-module.git
加速地址: git clone https://github.com.cnpmjs.org/weibocom/nginx-upsync-module.git
git clone https://github.com.cnpmjs.org/weibocom/nginx-upsync-module.git
git clone https://github.com.cnpmjs.org/xiaokai-wang/nginx_upstream_check_module.git
3.解压nginx安装包并进入对应目录
tar -xf nginx-1.19.6.tar.gz && cd nginx-1.19.6
4.打补丁,安装,编译
patch -p1 < /opt/nginx_upstream_check_module/check_1.12.1+.patch
./configure --add-module=/opt/nginx_upstream_check_module --add-module=/opt/nginx-upsync-module
make
make install
5.确认配置
/usr/local/nginx/sbin/nginx -V
6.配置全局
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
7.检查nginx.conf是否有语法错误
nginx -t
8.启动nginx
nginx
9.配置文件
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream web-test {
server 106.13.224.166:8500 weight=5;
server 106.13.224.166:8310 weight=5;
server 106.13.224.166:8320 weight=5;
check interval=3000 rise=2 fall=5 timeout=1000 type=http;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
upstream test {
server 127.0.0.1:11111;
upsync 106.13.224.166:8500/v1/kv/upstreams/test/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
include /usr/local/nginx/conf/servers/servers_test.conf;
check interval=1000 rise=2 fall=2 timeout=3000 type=http default_down=false;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
upstream eureka {
server 106.12.83.94:9901 weight=1 fail_timeout=10 max_fails=3;
}
server {
listen 8080;
location = /test {
proxy_pass http://test;
}
location = /eureka {
proxy_pass http://eureka;
}
location = /upstream_show {
upstream_show;
}
location /web-test {
proxy_pass http://web-test;
}
location /status {
check_status;
}
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
其中/usr/local/nginx/conf/servers/servers_test.conf 目录及文件要新建
10.添加服务
curl -X PUT http://106.13.224.166:8500/v1/kv/upstreams/test/106.12.83.94:9901
添加服务后访问http://IP:8080/status 可以看到服务注册成功。
删除服务:
curl -X DELETE http://106.13.224.166:8500/v1/kv/upstreams/testr/106.12.83.94:9901
11.问题
版本问题:
xiaokai-wang 分支兼容 nginx-upsync-module 模块【推荐】,1.12.1 的补丁可以用于 1.16.1
https://github.com/xiaokai-wang/nginx_upstream_check_module.git
yaoweibin 分支不兼容 nginx-upsync-module 模块,但补丁更新较快
https://github.com/yaoweibin/nginx_upstream_check_module.git
找不到对应servers (配置文件中没有加 server 127.0.0.1:11111;导致)
upstream test {
server 127.0.0.1:11111;
upsync 106.13.224.166:8500/v1/kv/upstreams/test/ upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
upsync_dump_path /usr/local/nginx/conf/servers/servers_test.conf;
include /usr/local/nginx/conf/servers/servers_test.conf;
check interval=1000 rise=2 fall=2 timeout=3000 type=http default_down=false;
check_http_send "HEAD / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
}
12.附录
测试配置文件:${Nginx}/sbin/nginx -t
启动命令:${Nginx}/sbin/nginx
停止命令:${Nginx}/sbin/nginx -s stop/quit
重启命令:${Nginx}/sbin/nginx -s reload
查看进程命令:ps -ef | grep nginx
平滑重启:kill -HUP [Nginx主进程号(即ps命令查到的PID)]