简介:

Nginx 反向代理模块:ngx_http_proxy_module、ngx_http_upstream_module 后端检测模块:nginx_http_upstream_check_module

前者是官方提供的,安装 Nginx 的时候默认就内置了,可以直接使用,地址:http://nginx.org/en/docs/http/ngx_http_proxy_module.html

后者是淘宝大神提供的后端检测模块,需要手动编译添加,地址:https://github.com/yaoweibin/nginx_upstream_check_module

当前稳定版本:http://nginx.org/download/nginx-1.12.2.tar.gz

一、实验环境

1、Nginx

shell > yum -y install gcc gcc-c++ make wget zlib-devel pcre-devel openssl-devel

shell > wget http://nginx.org/download/nginx-1.12.2.tar.gz
shell > tar zxf nginx-1.12..tar.gz; cd nginx-1.12.
shell > ./configure --prefix=/usr/local/nginx-1.12. && make && make install

2、后端服务器

shell > curl 192.168.10.24:
welcome to tomcat1
shell > curl 192.168.10.24:
welcome to tomcat2
shell > curl 192.168.10.24:
welcome to tomcat3

# 好了,三台后端服务器已经启动,分别监听 8080、8081、8082,分别返回 1、2、3

二、ngx_http_proxy_module、ngx_http_upstream_module

shell > vim conf/nginx.conf

user  nobody;
worker_processes ; pid logs/nginx.pid;
events {
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; upstream ls {
server 192.168.10.24: weight= max_fails= fail_timeout=20s;
server 192.168.10.24: weight= max_fails= fail_timeout=20s;
server 192.168.10.24: weight= max_fails= fail_timeout=20s;
} server {
listen ; location / {
proxy_pass http://ls;
}
}
}

# 这是一个最简配的 Nginx 配置文件,定义了一个负载均衡池,池中有三台服务器,权重分别是 1、2、3 ( 越大越高 )
# 最大失败次数 3 次,超过 3 次失败后,20 秒内不检测。

# 当用户访问该 IP 的 80 端口时,被转发到后端的服务器。下面是一些反向代理的配置。

# 故障转移策略,当后端服务器返回如下错误时,自动负载到后端其余机器
proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;
# 设置后端服务器获取用户真实IP、代理者真实IP等
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 用于指定客户端请求主体缓存区大小,可以理解成先保存到本地再传给用户
client_body_buffer_size 128k;
# 表示与后端服务器连接的超时时间,即发起握手等侯响应的超时时间
proxy_connect_timeout ;
# 表示后端服务器的数据回传时间,即在规定时间之后端服务器必须传完所有的数据,否则 Nginx 将断开这个连接
proxy_send_timeout ;
# 设置 Nginx 从代理的后端服务器获取信息的时间,表示连接建立成功后,Nginx 等待后端服务器的响应时间,其实是 Nginx 已经进入后端的排队中等候处理的时间
proxy_read_timeout ;
# 设置缓冲区大小,默认该缓冲区大小等于指令 proxy_buffers 设置的大小
proxy_buffer_size 4k;
# 设置缓冲区的数量和大小。Nginx 从代理的后端服务器获取的响应信息,会放置到缓冲区
proxy_buffers 32k;
# 用于设置系统很忙时可以使用的 proxy_buffers 大小,官方推荐大小为 proxu_buffers 的两倍
proxy_busy_buffers_size 64k;
# 指定 proxy 缓存临时文件的大小
proxy_temp_file_write_size 64k;
shell > /usr/local/nginx-1.12./sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12./conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12./conf/nginx.conf test is successful shell > /usr/local/nginx-1.12./sbin/nginx shell > i=; while [ $i -lt ];do curl localhost; let i++;done
welcome to tomcat2
welcome to tomcat3
welcome to tomcat3
welcome to tomcat2
welcome to tomcat1
welcome to tomcat3
welcome to tomcat2
welcome to tomcat3
welcome to tomcat3
welcome to tomcat2

# 总共请求10次,tomcat3 响应了5次,因为它的权重最高(weight=3)。

# 这样有一个问题,由于没有后端检测功能,当后端某一服务器无法提供服务时,该链接先被转发到这台机器,然后发现该机故障,而后才转发到其它机器。

# 导致资源浪费。

二、nginx_http_upstream_check_module

shell > git clone https://github.com/yaoweibin/nginx_upstream_check_module.git

shell > yum -y install patch

shell > cd /usr/local/src/nginx-1.12.; patch -p1 < /usr/local/src/nginx_upstream_check_module/check_1.12.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 源码目录,打补丁 ( 注意与自己的 Nginx 版本匹配 )

shell > ./configure --prefix=/usr/local/nginx-1.12. --add-module=/usr/local/src/nginx_upstream_check_module
shell > make && make install

# 重新编译、安装 Nginx,注意加上原来的编译参数

shell > vim /usr/local/nginx-1.12./conf/nginx.conf

    upstream ls {
server 192.168.10.24:;
server 192.168.10.24:;
server 192.168.10.24:; check interval= rise= fall= timeout= type=http;
} server {
listen ; location / {
proxy_pass http://ls;
} location /status {
check_status;
access_log off;
# allow x.x.x.x;
# deny all;
}
}

# 去掉了权重值,注意:是可以同时存在的。
# 添加了一行,检测间隔3000毫秒,连续成功2次标记为UP,连续失败5次标记为DOWN,超时时间1000毫秒,检测类型HTTP。

shell > /usr/local/nginx-1.12./sbin/nginx -t
nginx: the configuration file /usr/local/nginx-1.12./conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.12./conf/nginx.conf test is successful shell > /usr/local/nginx-1.12./sbin/nginx -s stop
shell > /usr/local/nginx-1.12./sbin/nginx

# 直接 -s reload 貌似不行~

shell > curl localhost/status?format=json
{"servers": {
"total": ,
"generation": ,
"server": [
{"index": , "upstream": "ls", "name": "192.168.10.24:8080", "status": "up", "rise": , "fall": , "type": "http", "port": },
{"index": , "upstream": "ls", "name": "192.168.10.24:8081", "status": "up", "rise": , "fall": , "type": "http", "port": },
{"index": , "upstream": "ls", "name": "192.168.10.24:8082", "status": "up", "rise": , "fall": , "type": "http", "port": }
]
}}

# 总共有三台机器,都属于负载均衡 ls 组,状态 up,连续成功次数等等。

shell > curl localhost/status?format=json
{"servers": {
"total": ,
"generation": ,
"server": [
{"index": , "upstream": "ls", "name": "192.168.10.24:8080", "status": "up", "rise": , "fall": , "type": "http", "port": },
{"index": , "upstream": "ls", "name": "192.168.10.24:8081", "status": "down", "rise": , "fall": , "type": "http", "port": },
{"index": , "upstream": "ls", "name": "192.168.10.24:8082", "status": "up", "rise": , "fall": , "type": "http", "port": }
]
}}

# 关一台后端的话,就变成了这样!重启检测成功后,会被重新加入到负载均衡中!

Nginx 反向代理、后端检测模块的更多相关文章

  1. nginx反向代理-后端服务器组设置

    nginx服务器的反向代理时其最常用的重要功能之一,在实际工作中应用广泛,涉及的配置指令也比较多.下面会尽量详细地介绍对应的指令,及其使用状态. 反向代理一般是互联网需要向内网拉取资源,比如访问一个w ...

  2. nginx反向代理后端web服务器记录客户端ip地址

    nginx在做反向代理的时候,后端的nginx web服务器log中记录的地址都是反向代理服务器的地址,无法查看客户端访问的真实ip. 在反向代理服务器的nginx.conf配置文件中进行配置. lo ...

  3. Nginx反向代理后端多节点下故障节点的排除思路

    仔细想来,其实是个非常简单的问题:开发和运维觉得两个后端节点跑起来压力太大了,就扩充了两个新的后端节点上去,这一加就出问题了,访问时页面间歇性丢失,这尼玛什么情况...想了半天没思路,查了Nginx的 ...

  4. Nginx反向代理+DNS轮询+IIS7.5 千万PV 百万IP 双线 网站架构案例

    原文地址:http://www.jb51.net/article/31844.htm Nginx  ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 ...

  5. 十.nginx反向代理负载均衡服务实践部署

    期中集群架构-第十章-nginx反向代理负载均衡章节章节====================================================================== 0 ...

  6. nginx反向代理docker registry报”blob upload unknown"解决办法

    问题症状:keepalived+nginx反向代理后端docker registry群集时,使用docker客户机向registry push镜像时出现 "blob upload unkno ...

  7. Nginx负载均衡反向代理 后端Nginx获取客户端真实IP

    Nginx 反向代理后,后端Nginx服务器无法正常获取客户端的真实IP nginx通过http_realip_module模块来实现的这需要重新编译,如果提前编译好了就无需重新编译了1,重新编译ng ...

  8. (Nginx反向代理+NFS共享网页根目录)自动部署及可用性检测

    1.nginx反向代理安装配置 #!/usr/bin/bash if [ -e /etc/nginx/nginx.conf ] then echo 'Already installed' exit e ...

  9. 使用nginx反向代理处理前后端跨域访问

    本文主要解决:使用nginx反向代理处理前后端跨域访问的问题 1.何为跨域访问? 以下类型为跨域访问 1)不同域名间访问 www.zuiyoujie.com和www.baidu.com 2)同域名不同 ...

随机推荐

  1. 使用Oracle PROFILE控制会话空闲时间

    客户想实现对会话空闲时间的控制,下面是做的一个例子.Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation.保留所有权利 ...

  2. Linux下C/C++代码调用PHP代码(转)

    Linux下C/C++代码可以通过popen系统函数调用PHP代码并通过fgets函数获取PHP代码echo输出的字符串. //main.c char str[1024] = {0}; char *  ...

  3. Microsoft Dynamics CRM 如何修改域密码

    一.安装IIS6脚本工具,如下图所示: 二.复制iisadmpwd文件夹到AD Server的C:\Windows\SysWOW64\inetsrv文件夹下 三.注册Iisadmpwd目录下的IISp ...

  4. 搭建openwrt_x86虚拟机环境

    1.下载源码 http://downloads.openwrt.org/snapshots/trunk/x86/openwrt-x86-generic-combined-ext4.img.gz 2.格 ...

  5. CentOS启动网络提示connect: Network is unreachable(配置静态路由)

    ls /etc/sysconfig/network-script/ifcfg-eth0 一.看是否在上述目录下存在ifcfg-eth0 这个文件,若存在则按下面的步骤操作: 1.手工配置ip看能不能配 ...

  6. chrome从版本55开始,不再支持设置网页内容编码

    Hi Everyone,   Chrome 55 has removed the Encoding menu and Chrome will do auto-encoding detection no ...

  7. binlog之五:mysqlbinlog解析binlog乱码问题解密

    发现MySQL库的binlog日志出来都是乱码,如下所示: BINLOG ’ IXZqVhNIAAAALQAAAGcBAAAAAHoAAAAAAAEABHRlc3QAAno0AAEDAABUOcnY  ...

  8. 华为P10的内存门和闪存门的检测方法

    用android的终端模拟器,进入以后进入界面,输入命令ls /proc/fs/*,可以查看是否ufs还是emmc硬盘:用devcheck可以查看到手机的内存是否是DDR3还是DDR4:用androb ...

  9. 本地环境 XAMPP+phpStorm+XDebug+chrome配置和断点调试 注册方法

    我的安装环境:XAMPP版本号V3.1.0 ;phpStorm版本8.0.3;windowsxp 32bit.您老人家先过目一下,不然怕影响意义. XAMPP.phpStorm 都直接安装在了D盘根目 ...

  10. sklearn的BaseEstimator、transformerMixin、ClassifierMixin、RegressorMixin、ClusterMixin介绍

    class sklearn.base.BaseEstimator:为所有的estimators提供基类 方法: __init__() 初始化方法 get_params(deep=True) 获取这个估 ...