nginx上游重复上游

时间:2015-04-14 08:47:45

标签: nginx

我使用的场景是这样的:

www.a.com.conf

……
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

……
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;
}

每次我同时关闭服务器,要更改上面两个配置文件,我想让主机独立,然后两个配置文件包含,这样一个文件的每一行只能被修改。 像这样:

www.a.com.conf

……
include vhosts/cluster.ini;
……
location / {
    proxy_pass http://cluster_backend;
}

www.b.mi.com.conf

……
include vhosts/cluster.ini;
……
location / {
    proxy_pass http://cluster_backend;
}

cluster.ini

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;  
    }

实际上,这还不够,它会提示不支持,如下:

nginx: [emerg] duplicate upstream "cluster_backend" in /usr/local/nginx/conf/vhosts/cluster.ini:1
configuration file /usr/local/nginx/conf/nginx.conf test failed
问:在这个场景中我做的比较方便,只是想让Hosts独立,让更多的vh​​ost包括在内。所以我vhosts同时使用了多台机器,一台服务器,只需要修改一个vhosts文件

1 个答案:

答案 0 :(得分:0)

已经解决了 添加了对上游包含代码的支持,编译

 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 };

请参阅:http://trac.nginx.org/nginx/attachment/ticket/635/upstream-include.patch