Centos7.6搭建nginx 1.16.1并使用upstream_check_module模块

环境说明:

主机名 操作系统版本 ip nginx版本 httpd版本 备注
nginx Centos 7.6.1810 172.27.34.41 1.16.1 / nginx服务器
web01 Centos 7.6.1810 172.27.34.161 / 2.4.6 web服务器
web02 Centos 7.6.1810 172.27.34.162 / 2.4.6 web服务器
web03 Centos 7.6.1810 172.27.34.163 / 2.4.6 web服务器

一、nginx_upstream_check_module简介

1. 模块出处

由淘宝团队开发,淘宝自己的 tengine 上自带该模块。

2. 模块意义

nginx自带的针对后端节点健康检查的功能比较简单,无法主动识别后端节点状态,后端即使有不健康节点, 负载均衡器依然会把该请求转发给该不健康节点,只能等待超时时间后转发到其他节点,这样就会造成响应延迟性能降低的问题。

二、nginx安装

1. nginx下载

nginx版本查看:https://nginx.org/en/download.html

下载最新的稳定版本nginx-1.16.1源码包并解压

[[email protected] ~]# wget https://nginx.org/download/nginx-1.16.1.tar.gz
[[email protected] ~]# tar -zxvf nginx-1.16.1.tar.gz

2. nginx_upstream_check_module模块下载

[[email protected] ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
[[email protected] ~]# yum -y install unzip
[[email protected] ~]# unzip master.zip

下载nginx_upstream_check_module模块并解压,如果没有unzip命令可以通过‘yum -y install unzip‘安装。

3. 安装准备

[[email protected] ~]# yum -y install gcc-c++  pcre pcre-devel zlib zlib-devel openssl openssl-devel

分别安装依赖包gcc、pcre、zlib、OpenSSL。

依赖包 作用
gcc 编译依赖 gcc 环境
pcre PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库,nginx也需要此库。
zlib zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
OpenSSL OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。<br/>nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

4. 打补丁

[[email protected] ~]# yum -y install patch
[[email protected] ~]# cd nginx-1.16.1
[[email protected] nginx-1.16.1]# patch -p1 < /root/nginx_upstream_check_module-master/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

进入nginx源码目录,运行patch命令打补丁

5. 编译安装

5.1 安装nginx

[[email protected] nginx-1.16.1]# ./configure --add-module=/root/nginx_upstream_check_module-master

5.2 编译

[[email protected] nginx-1.16.1]# make && make install

5.3 安装检查

[[email protected]nginx nginx-1.16.1]# ll /usr/local/nginx/
总用量 0
drwxr-xr-x 2 root root 333 1月  22 15:15 conf
drwxr-xr-x 2 root root  40 1月  22 15:15 html
drwxr-xr-x 2 root root   6 1月  22 15:15 logs
drwxr-xr-x 2 root root  19 1月  22 15:15 sbin

安装完成后/usr/local路径下有nginx目录,该目录为nginx的默认目录。

5.4 nginx命令全局执行

[[email protected] nginx-1.16.1]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
[[email protected] nginx-1.16.1]# nginx -v
nginx version: nginx/1.16.1
[[email protected] 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)
configure arguments: --add-module=/root/nginx_upstream_check_module-master
[[email protected] nginx-1.16.1]# 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

‘nginx -v‘可查看nginx的版本,‘nginx -V‘可查看加载的模块,‘nginx -t‘可检查配置文件是否正确配置。

6. nginx配置文件

[[email protected] ~]# cd /usr/local/nginx/conf/
[[email protected] conf]# more nginx.conf
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 172.27.34.161:8001        weight=5;
        server 172.27.34.162:8001        weight=5;
        server 172.27.34.163:8001        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;
    }
    server {
        listen       81;

        location / {
            proxy_pass http://web-test;
        }

       location /status {
            check_status;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

各参数意义:

  • interval:向后端发送的健康检查包的间隔。
  • fall(fall_count): 如果连续失败次数达到fall_count,服务器就被认为是down。
  • rise(rise_count): 如果连续成功次数达到rise_count,服务器就被认为是up。
  • timeout: 后端健康请求的超时时间。
  • default_down: 设定初始时服务器的状态,如果是true,就说明默认是down的,如果是false,就是up的。默认值是true,也就是一开始服务器认为是不可用,要等健康检查包达到一定成功次数以后才会被认为是健康的。
  • type:健康检查包的类型,现在支持以下多种类型
    • tcp:简单的tcp连接,如果连接成功,就说明后端正常。
    • ssl_hello:发送一个初始的SSL hello包并接受服务器的SSL hello包。
    • http:发送HTTP请求,通过后端的回复包的状态来判断后端是否存活。
    • mysql: 向mysql服务器连接,通过接收服务器的greeting包来判断后端是否存活。
    • ajp:向后端发送AJP协议的Cping包,通过接收Cpong包来判断后端是否存活。
  • port: 指定后端服务器的检查端口。你可以指定不同于真实服务的后端服务器的端口,比如后端提供的是443端口的应用,你可以去检查80端口的状态来判断后端健康状况。默认是0,表示跟后端server提供真实服务的端口一样。该选项出现于Tengine-1.4.0。

后端检查方式默认为tcp,本文使用http方式。

三、 apache服务器安装

在web01、web02和web03上分别安装配置apache

1. 安装apache

[[email protected] ~]# yum -y install httpd

2. 配置修改

2.1 修改默认端口

[[email protected] ~]# sed -i ‘s/Listen 80/Listen 8001/g‘  /etc/httpd/conf/httpd.conf

3台web服务器的默认端口都由80修改为8001

2.2 修改首页输出

[[email protected] ~]# echo `hostname`>/var/www/html/index.html

3台web服务器首页输出都修改为主机名

3. 启动apache并设置开机启动

[[email protected] ~]# systemctl start httpd
[[email protected] ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

3台web服务器启动并设置httpd服务开机启动

四、 启动nginx并设置开启启动

[[email protected] conf]# nginx
[[email protected] conf]# sed -i ‘$a /usr/local/nginx/sbin/nginx‘ /etc/rc.d/rc.local
[[email protected] conf]# chmod u+x /etc/rc.d/rc.local

测试:

[[email protected] ~]# for i in {1..100};do sleep 1; curl http://172.27.34.28:82/;done

五、测试

1. 访问测试

[[email protected] ~]# for i in {1..100};do sleep 1; curl http://172.27.34.41:81;done

访问nginx服务器 http://172.27.34.41:81,发现请求被平均的分配到后端的3台web服务器。

2. 后端状态检查

浏览器输入 http://172.27.34.41:81/status 检查后端web服务器状态

参数 意义
server number 后端服务器的数量
generation Nginx reload的次数
Index 服务器的索引
Upstream 在配置中upstream的名称
Name 服务器IP
Status 服务器的状态
Rise 服务器连续检查成功的次数
Fall 连续检查失败的次数
Check type 检查的方式
Check port 后端专门为健康检查设置的端口

3. 模拟后端服务不可用

[[email protected] ~]# for i in {1..100};do sleep 1; curl http://172.27.34.41:81;done
[[email protected] ~]# systemctl stop httpd

nginx服务器上持续访问nginx,同时停止web01的httpd服务.

请求没有被分配到web01,同时后端检测web01也异常。

4. 启动web01

[[email protected] ~]# for i in {1..100};do sleep 1; curl http://172.27.34.41:81;done
[[email protected] ~]# systemctl start httpd

重新启动web01上的httpd服务,发现请求重新被分配到web01上

后端检查web01也恢复正常。

六、nginx卸载

1. 停进程

[[email protected] ~]# ps -ef|grep nginx
root     17746     1  0 16:54 ?        00:00:00 nginx: master process nginx
nobody   17747 17746  0 16:54 ?        00:00:00 nginx: worker process
root     17784 17750  0 16:55 pts/0    00:00:00 grep --color=auto nginx
[[email protected] ~]# pkill nginx
[[email protected] ~]# ps -ef|grep nginx
root     17791 17750  0 16:55 pts/0    00:00:00 grep --color=auto nginx

2. 删除文件

[[email protected] ~]# find /* -name "nginx*"|xargs rm -rf

nginx各模块说明:健康检查模块功能

nginx_upstream_check_module模块源码nginx_upstream_check_module

原文地址:https://blog.51cto.com/3241766/2468275

时间: 2024-07-30 21:04:40

Centos7.6搭建nginx 1.16.1并使用upstream_check_module模块的相关文章

在Centos7上搭建Nginx+rtmp服务器

在VPS服务器上配置一个直播环境,在iOS客户端实现推流到直播服务器,通过VLC播放器实现拉取服务器上的流观看直播. 效果图 Demo_01 Centos7上搭建Nginx+rtmp服务器 今天第一次在Centos7上搭建Nginx+rtmp服务器,以前在Mac上成功搭建,所以今天想在Centos7上试一试,在此记录一下,过程还是比较顺利. (1)准备需要的模块及工具(nginx-rtmp-module,openssl和git) 1.使用yum安装git [root~]# yum -y inst

centos7下搭建nginx+php7.1+mariadb+memcached+redis

一.环境准备 1.首先介绍一下环境,以及我们今天的主角们 我用的环境是最小化安装的centos7,mariadb(江湖传言mysql被oracle收购后,人们担心像java一样毁在oracle手上于是成了新的分支,但是还是像mysql一样用), php7.1.0(版本无所谓,都是7版本),nginx1.10(我们安装的是稳定版而没有一味的追求新),memcached,和redis可以随机选择一个,当然全安装也没有干扰 2.软件包下载 1)mariadb软件包(yum安装,编译安装因为boost问

CentOS7下搭建nginx反向代理服务器使得外网可以二级域名访问内网应用

创建nginx的本地yum源 [[email protected] ~]# yum list |grep nginx No package nginx available. [[email protected] ~]# //给跪了,什么鬼,怎么没有nginx的rpm?算了,直接自己手动配一个官网repo吧 [[email protected] ~]# //访问nginx官网,进入dowload页面,翻到底部的Pre-Build Package,选stable version ----------

centos7.4 搭建nginx反向缓存代理

nginx可以实现基于硬盘缓存的反向代理服务通过proxy_cache和fastcgi_cache两个功能模块完成配置 ----- 本例:nginx反向代理服务器192.168.80.81web服务器192.168.80.82win7客户机 192.168.80.79 -----### web服务器192.168.80.82 配置:安装简单的httpd,提供web服务即可 -----### nginx反向代理服务器192.168.80.81配置:1.上传反向代理插件.软件和解压:tar xzvf

CentOS系统搭建Nginx负载均衡

一.关于CentOS系统介绍 CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代码所编译而成.基于Red Hat持续升级,和对已知BUG修复,所以CentOS更多用于搭建大型企业级服务器.目前较新版本为7.0,本文使用CentOS7 64bit进行搭建系统负载均衡. 二.安装VMWare VMWare (Virtual

Net分布式系统之二:CentOS系统搭建Nginx负载均衡

一.关于CentOS系统介绍 CentOS(Community Enterprise Operating System,中文意思是:社区企业操作系统)是Linux发行版之一,它是来自于Red Hat Enterprise Linux依照开放源代码规定释出的源代码所编译而成.基于Red Hat持续升级,和对已知BUG修复,所以CentOS更多用于搭建大型企业级服务器.目前较新版本为7.0,本文使用CentOS7 64bit进行搭建系统负载均衡. 二.安装VMWare VMWare (Virtual

Linux系统:Centos7下搭建PostgreSQL关系型数据库

本文源码:GitHub·点这里 || GitEE·点这里 一.PostgreSQL简介 1.数据库简介 PostgreSQL是一个功能强大的开源数据库系统,具有可靠性.稳定性.数据一致性等特点,且可以运行在所有主流操作系统上,包括Linux.Unix.Windows等.PostgreSQL是完全的事务安全性数据库,完整地支持外键.联合.视图.触发器和存储过程,支持了大多数的SQL:2008标准的数据类型,包括整型.数值型.布尔型.字节型.字符型.日期型.时间间隔型和时间型,它也支持存储二进制的大

CentOS7 搭建 Nginx 服务器

一:Nginx 的搭建 Nginx 在服务器的应用主要是用于实现反向代理,负载均衡的功能. 这里简单的搭建 Nginx 服务器实现 nginx 对于两个 tomcat 的反向代理的功能. 在安装 Nginx 之前, 需要安装四个依赖, 安装nginx的依赖:gcc 安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gcc: yum install gcc-c++ yum install -y gcc PCRE PCRE(Perl Compatible

Centos7搭建nginx+tomcat负载均衡

Nginx简介 Nginx是一种服务器软件,也是一种高性能的http和反向代理服务器,同时还是一个代理邮件服务器.也就是说,我们在Nginx上可以发布网站,可以实现负载均衡(提高应答效率,避免服务器崩溃),还可以作为邮件服务器实现收发邮件等功能.而最常见的就是使用Nginx实现负载均衡. Nginx与其他服务器的性能比较 Tomcat服务器面向Java语言,是重量级的服务器,而Nginx是轻量级的服务器.Apache服务器稳定.开源.跨平台,但是Apache服务器不支持高并发,Nginx能支持处