多业务复用同组WEB通过独立upstream来敏捷运维

场景
在大部分企业中都遇到一组服务器多个业务复用的现象,比如以下场景
www.a.com www.b.com 都使用了同一组web服务器,在代理上,我们传统的配置方法是这样的 :

www.a.com.conf

1
2
3
4
5
6
7
8
9
10

……
upstream a_cluster_backend {
server 1.1.1.1:80 weight=1 max_fails=2 fail_timeout=10s;
server 1.1.1.2:80 weight=1 max_fails=2 fail_timeout=10s;
}
……
location / {
proxy_pass http://a_cluster_backend;
}

www.b.mi.com.conf

1
2
3
4
5
6
7
8
9
10

……
upstream b_cluster_backend {
server 1.1.1.1:80 weight=1 max_fails=2 fail_timeout=10s;
server 1.1.1.2:80 weight=1 max_fails=2 fail_timeout=10s;
}
……
location / {
proxy_pass http://b_cluster_backend;
}

然而,这样的话,如果我的 1.1.1.1 这台服务器有故障,要下线,需要我更改两个配置文件,即www.a.com.conf www.b.com.conf

那么问题来了,如果有10+个业务都在复用这一组WEB服务器呢,那就需要修改10+个配置文件了,以下是本人测试的两种方法与大家分享:

方法一:
cluster.conf

1
2
3
4
5

upstream cluster_backend {
server 1.1.1.1:80 weight=1 max_fails=2 fail_timeout=10s;
server 1.1.1.2:80 weight=1 max_fails=2 fail_timeout=10s;
}

www.a.com.conf

1
2
3
4
5

……
location / {
proxy_pass http://cluster_backend;

}

www.b.mi.com.conf

1
2
3
4
5

……
location / {
proxy_pass http://cluster_backend;

}

nginx.conf

1
2
3
4
5

http {
……
include vhosts/*.conf;
}

方法二:
cluster.ini

1
2
3

server 1.1.1.1:80 weight=1 max_fails=2 fail_timeout=10s;
server 1.1.1.2:80 weight=1 max_fails=2 fail_timeout=10s;

www.a.com.conf

1
2
3
4
5
6
7
8

upstream a_cluster_backend {
include vhosts/cluster.ini;


location / {
proxy_pass http://a_cluster_backend;
}

www.b.mi.com.conf

1
2
3
4
5
6
7

upstream b_cluster_backend {
include vhosts/cluster.ini;

location / {
proxy_pass http://b_cluster_backend;
}

nginx.conf

1
2
3
4
5

http {
……
include vhosts/*.conf;
}

注:此方法需要nginx的upstream支持include,但NGINX 默认是不支持的,所以需要更改源码重新编译来支持
vim src/http/ngx_http_upstream.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

306 static ngx_command_t ngx_http_upstream_commands[] = {
307
308 { ngx_string("upstream"),
309 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE1,
310 ngx_http_upstream,
311 0,
312 0,
313 NULL },
+ 314 { ngx_string("include"),
+ 315 NGX_HTTP_UPS_CONF|NGX_CONF_TAKE1,
+ 316 ngx_conf_include,
+ 317 0,
+ 318 0,
+ 319 NULL },
320
321 { ngx_string("server"),
322 NGX_HTTP_UPS_CONF|NGX_CONF_1MORE,
323 ngx_http_upstream_server,
324 NGX_HTTP_SRV_CONF_OFFSET,
325 0,
326 NULL },
327
328 ngx_null_command
329 };

接下来再分享一个坑
我在测试第一种方法时,发现怎么也通不过语法检测,最后发现是因为我当前测试机vhosts/里我有统一配置nginx_status.conf文件,和这个fastcgi_pass冲突导致,请朋友们测试时一定要注意这里。

1
2
3
4
5
6
7
8
9

location ~ ^/(php-fpm_status|php-fpm_ping)$ {
# fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
access_log off;
allow 127.0.0.1;
allow 10.0.0.0/8;
deny all;
}
文章目录
,