1. nginx 第三方高可用模块

IP 备注
10.0.0.63 proxy
10.0.0.64 web1
10.0.0.65 web2

这里会讲解一些nignx常用高可用方案,以及引入第三方高可用模块来了解nginx作为高可用服务它是如何运行的

2. 基础环境安装

10.0.0.63  nginx 增加 nginx_upstream_check_module模块
10.0.0.64 nginx
10.0.0.65 nginx
-------------------------------------------------------------
#3台都安装上nginxx:
useradd www -u 1200 -M -s /sbin/nologin
mkdir -p /var/log/nginx
yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel \
zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl &&\
yum clean all && \
rm -rf /var/cache/yum/*
mkdir -p /server/tools
cd /server/tools
wget https://www.chenleilei.net/soft/nginx-1.16.1.tar.gz
tar xf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www \
--with-http_ssl_module --with-http_v2_module --with-http_stub_status_module \
--pid-path=/var/run/nginx/nginx.pid
make -j 4 && make install && \
rm -rf /usr/local/nginx/html/* && \
echo "nginx daemo hello" >/usr/local/nginx/html/index.html && \
echo "export PATH=$PATH:/usr/local/nginx/sbin" >>/etc/profile
chown -R www.www /var/log/nginx /usr/local/nginx
source /etc/profile
nginx -s reload
ps -ef|grep nginx

3. nginx的高可用配置

默认的nginx高可用是一个后端连接不可用时且没有健康检查,他会一台台轮询.在 如: 金钱交易的期间都会关闭这个默认高可用'proxy_next_upstream',原因就是,敏感交易有可能在默认时间未结束前得到了响应,而另一方面有其他服务器响应了该请求,可能会出现重复交易情况,所以建议关闭默认的高可用策略,从而改用更简单易用的其他高可用模块来实现,它就是 "ngx_http_proxy_module"

3.1 默认高可用策略介绍

server 10.0.0.10:80 max_fails=2 fail_timeout=30;
weight 权重
max_fails 失败次数后停止该服务器
fail_timeout 踢出后重新检测时间. 一般max_fails和fail_timeout一起启用的,也就是失败n次,等待N秒
backup 备用服务器
max_conns 允许最大连接数
slow_start 节点恢复后不立即加入到集群.

3.2 安装 ngx_upstream_check_module [10.0.0.63]

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

[root@master tools]# git clone https://github.com/yaoweibin/nginx_upstream_check_module.git
[root@master tools]# ll
drwxr-xr-x 7 root root 4096 Apr 16 21:53 nginx_upstream_check_module
[root@master tools]# cd nginx_upstream_check_module/
[root@master nginx_upstream_check_module]# pwd
/server/tools/nginx_upstream_check_module #这个路径用于编译nginx新增模块使用. # nginx_upstream_check_module 模块目录:
[root@master nginx_upstream_check_module]# ll
total 268
-rw-r--r-- 1 root root 0 Apr 16 21:53 CHANGES
-rw-r--r-- 1 root root 7921 Apr 16 21:53 check_1.11.1+.patch
-rw-r--r-- 1 root root 8330 Apr 16 21:53 check_1.11.5+.patch
-rw-r--r-- 1 root root 8060 Apr 16 21:53 check_1.12.1+.patch
-rw-r--r-- 1 root root 8054 Apr 16 21:53 check_1.14.0+.patch
-rw-r--r-- 1 root root 8409 Apr 16 21:53 check_1.16.1+.patch
-rw-r--r-- 1 root root 5483 Apr 16 21:53 check_1.2.1.patch
-rw-r--r-- 1 root root 7130 Apr 16 21:53 check_1.2.2+.patch
-rw-r--r-- 1 root root 7094 Apr 16 21:53 check_1.2.6+.patch
-rw-r--r-- 1 root root 6791 Apr 16 21:53 check_1.5.12+.patch
-rw-r--r-- 1 root root 8295 Apr 16 21:53 check_1.7.2+.patch
-rw-r--r-- 1 root root 8346 Apr 16 21:53 check_1.7.5+.patch
-rw-r--r-- 1 root root 8509 Apr 16 21:53 check_1.9.2+.patch
-rw-r--r-- 1 root root 6943 Apr 16 21:53 check.patch
-rw-r--r-- 1 root root 749 Apr 16 21:53 config
drwxr-xr-x 2 root root 43 Apr 16 21:53 doc
-rw-r--r-- 1 root root 1709 Apr 16 21:53 nginx-sticky-module.patch
drwxr-xr-x 2 root root 29 Apr 16 21:53 nginx-tests
-rw-r--r-- 1 root root 112085 Apr 16 21:53 ngx_http_upstream_check_module.c
-rw-r--r-- 1 root root 529 Apr 16 21:53 ngx_http_upstream_check_module.h
-rw-r--r-- 1 root root 2848 Apr 16 21:53 ngx_http_upstream_jvm_route_module.patch
-rw-r--r-- 1 root root 11509 Apr 16 21:53 README
drwxr-xr-x 6 root root 79 Apr 16 21:53 test
-rw-r--r-- 1 root root 3342 Apr 16 21:53 upstream_fair.patch
drwxr-xr-x 2 root root 81 Apr 16 21:53 util #编译nginx:
1. 获取nginx原有编译参数:
configure arguments: --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --pid-path=/var/run/nginx/nginx.pid 2. 同步模块代码:
cd /server/tools/nginx-1.16.1
patch -p1 < /server/tools/nginx_upstream_check_module/check_1.16.1+.patch
输出:
[root@master nginx-1.16.1]# patch -p1 < /server/tools/nginx_upstream_check_module/check_1.16.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 3. 重新编译nginx来添加模块:
[root@master ~ ]# cd /server/tools/nginx-1.16.1
[root@master nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --pid-path=/var/run/nginx/nginx.pid --add-module=/server/tools/nginx_upstream_check_module/ [root@master nginx-1.16.1]# make
make结束后拷贝新的nginx二进制文件替换老版本,这和版本升级流程一样 #拷贝二进制文件替换老的nginx二进制文件
[root@master nginx-1.16.1]# \cp -a objs/nginx /usr/local/nginx/sbin/ #查看进程:
[root@master nginx-1.16.1]# ps -ef|grep nginx
root 43754 1 0 22:17 ? 00:00:00 nginx: master process nginx
www 43755 43754 0 22:17 ? 00:00:00 nginx: worker process
root 43774 37915 0 22:17 pts/0 00:00:00 grep --color=auto nginx #关闭nginx后重新启动nginx:
[root@master nginx-1.16.1]# pkill nginx
[root@master nginx-1.16.1]# nginx #检查模块安装是否成功:
[root@master nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --pid-path=/var/run/nginx/nginx.pid --add-module=/server/tools/nginx_upstream_check_module/ #可以看到已经安装了 顺便做一下语法高亮显示的小优化:
mkdir ~/.vim -p
cp -r contrib/vim/* ~/.vim/

4.第三方高可用模块nginx_ustream_check_mudule的配置使用

安装完了模块后,需要对nginx进行配置才能使得新模块得到应用

egrep -v "#|^$" /usr/local/nginx/conf/nginx.conf.default > /usr/local/nginx/conf/nginx.conf
vim /usr/local/nginx/conf/nginx.conf 配置为如下:
#-----------------------------------------------------------------------------#
worker_processes 1;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
upstream web {
server 10.0.0.64;
server 10.0.0.65;
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name locaalhost;
location / {
proxy_connect_timeout 2s;
proxy_read_timeout 2s;
proxy_next_upstream off;
proxy_pass http://web;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
#-----------------------------------------------------------------------------# 2. 修改添加模块配置项:
worker_processes 1;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
upstream web {
server 10.0.0.64;
server 10.0.0.65;
#模块配置项 用于检查后端服务器是否正常
check interval=5000 rise=1 fall=3 timeout=4000;
check interval=3000 rise=2 fall=5 timeout=1000 type=ssl_hello;
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 http_4xx http_5xx;
#模块配置项 用于检查后端服务器是否正常
}
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name locaalhost;
location / {
proxy_connect_timeout 2s;
proxy_read_timeout 2s;
proxy_next_upstream off;
proxy_pass http://web;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

验证模块可用性:

1. 为后端nginx添加时别内容:

10.0.0.64服务器添加:
echo "10.0.0.64 nginx daemon" >index.html 10.0.0.65服务器添加:
echo "10.0.0.65 nginx daemon" >index.html

可以看出节点分配正常

4.1 后端健康检查测试

前端负载均衡 开启日志记录,获取访问的后端节点IP,便于查看配置是否正确,这个配置在企业中也是必备,务必掌握

http模块下修改默认日志记录为如下:

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time $upstream_response_time $upstream_addr $upstream_status'; access_log logs/access.log main;

配置完毕后,继续测试负载均衡模块的可用性

默认端口都开 检查日志访问情况:
10.0.0.1 - - [16/Apr/2020:23:11:29 +0800] "GET / HTTP/1.1" 200 23 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36" "-" 0.001 0.002 10.0.0.64:80 200
10.0.0.1 - - [16/Apr/2020:23:11:29 +0800] "GET / HTTP/1.1" 200 23 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36" "-" 0.001 0.001 10.0.0.65:80 200
10.0.0.1 - - [16/Apr/2020:23:11:30 +0800] "GET / HTTP/1.1" 200 23 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36" "-" 0.001 0.002 10.0.0.64:80 200
10.0.0.1 - - [16/Apr/2020:23:11:32 +0800] "GET / HTTP/1.1" 200 23 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36" "-" 0.001 0.001 10.0.0.65:80 200 #从这里可以看出,我们访问10.0.0.63负载均衡的时候,他会一次去访问10.0.0.64,一次去访问10.0.0.65, 照此依次轮询.
#现在关闭后端某个节点,用于测试模块是否正常检测: 2020/04/16 23:18:38 [error] 46559#0: send() failed (111: Connection refused)
2020/04/16 23:18:41 [error] 46559#0: send() failed (111: Connection refused)
2020/04/16 23:18:44 [error] 46559#0: send() failed (111: Connection refused)
2020/04/16 23:18:47 [error] 46559#0: send() failed (111: Connection refused)
2020/04/16 23:18:50 [error] 46559#0: send() failed (111: Connection refused)
2020/04/16 23:18:50 [error] 46559#0: disable check peer: 10.0.0.64:80 #<---- 移除了该节点 他会进行5次检测,发现后端节点无法访问,直接将故障节点移除集群.
同时当这个集群角色恢复后,该模块依然能够检测出并将其重新加入集群.
如下:
2020/04/16 23:20:53 [error] 46559#0: send() failed (111: Connection refused)
2020/04/16 23:20:56 [error] 46559#0: send() failed (111: Connection refused)
2020/04/16 23:21:03 [error] 46559#0: enable check peer: 10.0.0.64:80 #<---- 添加了该节点

nginx内置高可用配置与第三方高可用模块nginx_ustream_check_mudule配置的更多相关文章

  1. (转)最新版 nginx内置变量 大全

    原文:http://www.cnphp.info/nginx-embedded-variables-lasted-version.html 在配置基于nginx服务器的网站时,必然会用到 nginx内 ...

  2. nginx内置全局变量

    nginx内置全局变量 $args        请求中的参数; $binary_remote_addr        远程地址的二进制表示 $body_bytes_sent        已发送的消 ...

  3. nginx内置变量总结

    nginx内置变量 2019-02-28 变量名称 变量用途 $atg_PARAMETER      客户端GET请求中   PARAMETER字段的值                        ...

  4. nginx 内置变量

    http://blog.sina.com.cn/s/articlelist_1834459124_1_1.html  nginx内置变量杂谈 http://nginx.org/en/docs/http ...

  5. nginx内置变量 大全

    nginx内置变量 内置变量存放在  ngx_http_core_module 模块中,变量的命名方式和apache 服务器变量是一致的.总而言之,这些变量代表着客户端请求头的内容,例如$http_u ...

  6. -e $request_filename + nginx内置变量

    -e表示只要filename存在,则为真,不管filename是什么类型,当然这里加了!就取反额外的一些-e filename 如果 filename存在,则为真-d filename 如果 file ...

  7. 查看python内部模块命令,内置函数,查看python已经安装的模块命令

    查看python内部模块命令,内置函数,查看python已经安装的模块命令 可以用dir(modules) 或者用 pip list或者用 help('modules') 或者用 python -m  ...

  8. nginx基础学习第二篇:nginx内置变量的使用

    ngx_http_core模块提供的内置变量有很多,常见的有 $uri,用来获取当前请求的uri,不含请求参数. $request_uri,用来获取请求最原始的uri,包含请求参数,且未解码. $re ...

  9. Nginx 内置全局变量

    Nginx在使用过程中,有不少的内置全局变量可以用做条件判断和编程控制,本文总结一些已知的指令,以供参考. $arg_PARAMETER  这个变量包含在查询字符串时GET请求PARAMETER的值. ...

随机推荐

  1. Leetcode_1048. 最长字符串链

    字符串的最长严格递增子序列,前后只能相差一个字符. 直接O(N^2)暴力建图,然后记忆化跑个最长路. 直接按字符串长度排序,然后求LIS. code1 class Solution { public: ...

  2. angular的性能分析 -随记

    $watch 的实现原理和性能分析 只有双向绑定的 scope 才会被加入$watch队列,或者手动绑定$watch的$scope 所有放在 $scope 中的变量或函数都被加入到了$watch队列当 ...

  3. 一些Nmap NSE脚本推荐

    前言 Nmap是一款强大的开源扫描工具.同时Nmap提供了强大的脚本引擎(Nmap Scripting Engine),支持通过Lua脚本语言来扩展Nmap的功能,在Nmap的发行版中已经包含了数百个 ...

  4. hdoj 1829 A bug's life 种类并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1829 并查集的一个应用,就是检测是否存在矛盾,就是两个不该相交的集合有了交集.本题就是这样,一种虫子有 ...

  5. python-神奇的下划线

    2019-12-16 22:45:29 python中下划线有各种各样的作用,本章就来分别介绍一下各种下划线的功能. 一.开头单下划线 _VAR 开头单下划线还是挺常用的,在类中表示为保护变量/保护函 ...

  6. Contest 161

    2019-11-03 20:35:18 总体感受:本周的赛题完全是反过来的,第一题最难,第二题次之,最后的hard反而是最简单的. 注意点:心态放平稳,慢慢来.

  7. 控制台报错Cause: org.xml.sax.SAXParseException; lineNumber: 4; columnNumber: 78; 元素类型 "select" 必须后跟属性规范 ">" 或 "/>"

    首先我的控制台报错是这样的,我找了一下原因看到是第四行的错误: 它说元素select后面必须跟属性规范">""/>"但是我把我眼睛都快丑瞎了都没发现 ...

  8. 搭建生产级的Netty项目

    Netty是Trustin Lee在2004年开发的一款高性能的网络应用程序框架.相比于JDK自带的NIO,Netty做了相当多的增强,且隔离了jdk nio的实现细节,API也比较友好,还支持流量整 ...

  9. coding++:解决Not allowed to load local resource错误-SpringBoot配置虚拟路径

    1.在SpringBoot里上传图片后返回了绝对路径,发现本地读取的环节上面出现了错误(Not allowed to load local resource),一开始用的是直接本地路径. 但是在页面上 ...

  10. 如何有效的阅读JDK源码

    阅读Java源码的前提条件: 1.技术基础 在阅读源码之前,我们要有一定程度的技术基础的支持. 假如你从来都没有学过Java,也没有其它编程语言的基础,上来就啃<Core Java>,那样 ...