|
| 1 | +## 详解:Nginx 反向代理、后端检测模块 |
| 2 | + |
| 3 | +#### Nginx |
| 4 | + |
| 5 | +``` |
| 6 | +shell > yum -y install gcc gcc-c++ make wget zlib-devel pcre-devel openssl-devel |
| 7 | +shell > wget http://nginx.org/download/nginx-1.12.2.tar.gz |
| 8 | +shell > tar zxf nginx-1.12.2.tar.gz; cd nginx-1.12.2 |
| 9 | +shell > ./configure --prefix=/usr/local/nginx-1.12.2 && make && make install |
| 10 | +``` |
| 11 | +#### 后端服务器 |
| 12 | + |
| 13 | +``` |
| 14 | +shell > curl 192.168.10.24:8080 |
| 15 | +welcome to tomcat1 |
| 16 | +shell > curl 192.168.10.24:8081 |
| 17 | +welcome to tomcat2 |
| 18 | +shell > curl 192.168.10.24:8082 |
| 19 | +welcome to tomcat3 |
| 20 | +``` |
| 21 | +好了,三台后端服务器已经启动,分别监听 8080、8081、8082,分别返回 1、2、3 |
| 22 | + |
| 23 | +配置`ngx_http_proxy_module`和`ngx_http_upstream_module`模块 |
| 24 | + |
| 25 | + |
| 26 | +编辑配置文件`vim conf/nginx.conf` |
| 27 | +``` |
| 28 | +user nobody; |
| 29 | +worker_processes 1; |
| 30 | +
|
| 31 | +pid logs/nginx.pid; |
| 32 | +events { |
| 33 | + worker_connections 1024; |
| 34 | +} |
| 35 | +
|
| 36 | +http { |
| 37 | + include mime.types; |
| 38 | + default_type application/octet-stream; |
| 39 | +
|
| 40 | + upstream ls { |
| 41 | + server 192.168.10.24:8080 weight=1 max_fails=3 fail_timeout=20s; |
| 42 | + server 192.168.10.24:8081 weight=2 max_fails=3 fail_timeout=20s; |
| 43 | + server 192.168.10.24:8082 weight=3 max_fails=3 fail_timeout=20s; |
| 44 | + } |
| 45 | +
|
| 46 | + server { |
| 47 | + listen 80; |
| 48 | +
|
| 49 | + location / { |
| 50 | + proxy_pass http://ls; |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | +这是一个最简配的 Nginx 配置文件,定义了一个负载均衡池,池中有三台服务器,权重分别是 1、2、3 ( 越大越高 ) |
| 56 | + |
| 57 | +最大失败次数 3 次,超过 3 次失败后,20 秒内不检测。 |
| 58 | + |
| 59 | +当用户访问该 IP 的 80 端口时,被转发到后端的服务器。下面是一些反向代理的配置。 |
| 60 | + |
| 61 | +``` |
| 62 | +# 故障转移策略,当后端服务器返回如下错误时,自动负载到后端其余机器 |
| 63 | +proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header; |
| 64 | +
|
| 65 | +# 设置后端服务器获取用户真实IP、代理者真实IP等 |
| 66 | +proxy_redirect off; |
| 67 | +proxy_set_header Host $host; |
| 68 | +proxy_set_header X-Real-IP $remote_addr; |
| 69 | +proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 70 | +
|
| 71 | +# 用于指定客户端请求主体缓存区大小,可以理解成先保存到本地再传给用户 |
| 72 | +client_body_buffer_size 128k; |
| 73 | +
|
| 74 | +# 表示与后端服务器连接的超时时间,即发起握手等侯响应的超时时间 |
| 75 | +proxy_connect_timeout 90; |
| 76 | +
|
| 77 | +# 表示后端服务器的数据回传时间,即在规定时间之后端服务器必须传完所有的数据,否则 Nginx 将断开这个连接 |
| 78 | +proxy_send_timeout 90; |
| 79 | +
|
| 80 | +# 设置 Nginx 从代理的后端服务器获取信息的时间,表示连接建立成功后,Nginx 等待后端服务器的响应时间,其实是 Nginx 已经进入后端的排队中等候处理的时间 |
| 81 | +proxy_read_timeout 90; |
| 82 | +
|
| 83 | +# 设置缓冲区大小,默认该缓冲区大小等于指令 proxy_buffers 设置的大小 |
| 84 | +proxy_buffer_size 4k; |
| 85 | +
|
| 86 | +# 设置缓冲区的数量和大小。Nginx 从代理的后端服务器获取的响应信息,会放置到缓冲区 |
| 87 | +proxy_buffers 4 32k; |
| 88 | +
|
| 89 | +# 用于设置系统很忙时可以使用的 proxy_buffers 大小,官方推荐大小为 proxu_buffers 的两倍 |
| 90 | +proxy_busy_buffers_size 64k; |
| 91 | +
|
| 92 | +# 指定 proxy 缓存临时文件的大小 |
| 93 | +proxy_temp_file_write_size 64k; |
| 94 | +shell > /usr/local/nginx-1.12.2/sbin/nginx -t |
| 95 | +nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok |
| 96 | +nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful |
| 97 | +
|
| 98 | +shell > /usr/local/nginx-1.12.2/sbin/nginx |
| 99 | +
|
| 100 | +shell > i=0; while [ $i -lt 10 ];do curl localhost; let i++;done |
| 101 | +welcome to tomcat2 |
| 102 | +welcome to tomcat3 |
| 103 | +welcome to tomcat3 |
| 104 | +welcome to tomcat2 |
| 105 | +welcome to tomcat1 |
| 106 | +welcome to tomcat3 |
| 107 | +welcome to tomcat2 |
| 108 | +welcome to tomcat3 |
| 109 | +welcome to tomcat3 |
| 110 | +welcome to tomcat2 |
| 111 | +``` |
| 112 | + |
| 113 | +总共请求10次,tomcat3 响应了5次,因为它的权重最高(weight=3)。 |
| 114 | + |
| 115 | +这样有一个问题,由于没有后端检测功能,当后端某一服务器无法提供服务时,该链接先被转发到这台机器,然后发现该机故障,而后才转发到其它机器。 |
| 116 | + |
| 117 | +导致资源浪费。 |
| 118 | + |
| 119 | +nginx_http_upstream_check_module |
| 120 | + |
| 121 | +``` |
| 122 | +shell > git clone https://github.com/yaoweibin/nginx_upstream_check_module.git |
| 123 | +
|
| 124 | +shell > yum -y install patch |
| 125 | +
|
| 126 | +shell > cd /usr/local/src/nginx-1.12.2; patch -p1 < /usr/local/src/nginx_upstream_check_module/check_1.12.1+.patch |
| 127 | +patching file src/http/modules/ngx_http_upstream_hash_module.c |
| 128 | +patching file src/http/modules/ngx_http_upstream_ip_hash_module.c |
| 129 | +patching file src/http/modules/ngx_http_upstream_least_conn_module.c |
| 130 | +patching file src/http/ngx_http_upstream_round_robin.c |
| 131 | +patching file src/http/ngx_http_upstream_round_robin.h |
| 132 | +切换到 Nginx 源码目录,打补丁 ( 注意与自己的 Nginx 版本匹配 ) |
| 133 | +
|
| 134 | +shell > ./configure --prefix=/usr/local/nginx-1.12.2 --add-module=/usr/local/src/nginx_upstream_check_module |
| 135 | +shell > make && make install |
| 136 | +``` |
| 137 | +重新编译、安装 Nginx,注意加上原来的编译参数 |
| 138 | + |
| 139 | +`vim /usr/local/nginx-1.12.2/conf/nginx.conf` |
| 140 | +配置文件如下所示: |
| 141 | +``` |
| 142 | +upstream ls { |
| 143 | + server 192.168.10.24:8080; |
| 144 | + server 192.168.10.24:8081; |
| 145 | + server 192.168.10.24:8082; |
| 146 | +
|
| 147 | + check interval=3000 rise=2 fall=5 timeout=1000 type=http; |
| 148 | +} |
| 149 | +
|
| 150 | +server { |
| 151 | + listen 80; |
| 152 | +
|
| 153 | + location / { |
| 154 | + proxy_pass http://ls; |
| 155 | + } |
| 156 | +
|
| 157 | + location /status { |
| 158 | + check_status; |
| 159 | + access_log off; |
| 160 | + # allow x.x.x.x; |
| 161 | + # deny all; |
| 162 | + } |
| 163 | +} |
| 164 | +``` |
| 165 | +去掉了权重值,注意:是可以同时存在的。 |
| 166 | + |
| 167 | +添加了一行,检测间隔3000毫秒,连续成功2次标记为UP,连续失败5次标记为DOWN,超时时间1000毫秒,检测类型HTTP。 |
| 168 | + |
| 169 | +``` |
| 170 | +shell > /usr/local/nginx-1.12.2/sbin/nginx -t |
| 171 | +nginx: the configuration file /usr/local/nginx-1.12.2/conf/nginx.conf syntax is ok |
| 172 | +nginx: configuration file /usr/local/nginx-1.12.2/conf/nginx.conf test is successful |
| 173 | +
|
| 174 | +shell > /usr/local/nginx-1.12.2/sbin/nginx -s stop |
| 175 | +shell > /usr/local/nginx-1.12.2/sbin/nginx |
| 176 | +``` |
| 177 | +直接 -s reload 貌似不行~ |
| 178 | + |
| 179 | +``` |
| 180 | +shell > curl localhost/status?format=json |
| 181 | +{"servers": |
| 182 | + { |
| 183 | + "total": 3, |
| 184 | + "generation": 1, |
| 185 | + "server": [ |
| 186 | + {"index": 0, "upstream": "ls", "name": "192.168.10.24:8080", "status": "up", "rise": 20, "fall": 0, "type": "http", "port": 0}, |
| 187 | + {"index": 1, "upstream": "ls", "name": "192.168.10.24:8081", "status": "up", "rise": 18, "fall": 0, "type": "http", "port": 0}, |
| 188 | + {"index": 2, "upstream": "ls", "name": "192.168.10.24:8082", "status": "up", "rise": 19, "fall": 0, "type": "http", "port": 0} |
| 189 | + ] |
| 190 | + } |
| 191 | +} |
| 192 | +``` |
| 193 | +总共有三台机器,都属于负载均衡 ls 组,状态 up,连续成功次数等等。 |
| 194 | + |
| 195 | +``` |
| 196 | +shell > curl localhost/status?format=json |
| 197 | +{"servers": |
| 198 | + { |
| 199 | + "total": 3, |
| 200 | + "generation": 1, |
| 201 | + "server": [ |
| 202 | + {"index": 0, "upstream": "ls", "name": "192.168.10.24:8080", "status": "up", "rise": 73, "fall": 0, "type": "http", "port": 0}, |
| 203 | + {"index": 1, "upstream": "ls", "name": "192.168.10.24:8081", "status": "down", "rise": 0, "fall": 6, "type": "http", "port": 0}, |
| 204 | + {"index": 2, "upstream": "ls", "name": "192.168.10.24:8082", "status": "up", "rise": 68, "fall": 0, "type": "http", "port": 0} |
| 205 | + ] |
| 206 | + } |
| 207 | +} |
| 208 | +``` |
| 209 | +关一台后端的话,就变成了这样!重启检测成功后,会被重新加入到负载均衡中! |
0 commit comments