Skip to content

Commit 0196098

Browse files
committed
Merge pull request #11 from wandenberg/use_nginx_plus_syntax
Use nginx plus syntax
2 parents a34a814 + 986490e commit 0196098

File tree

4 files changed

+101
-69
lines changed

4 files changed

+101
-69
lines changed

README.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ http {
2424
resolver 8.8.8.8;
2525
2626
upstream example {
27-
dynamic_server example.com;
27+
server example.com resolve;
2828
}
2929
}
3030
```
@@ -33,7 +33,7 @@ http {
3333

3434
### dynamic_server
3535

36-
**Syntax:** *dynamic_server address [parameters]*;
36+
**Syntax:** *server address [parameters]*;
3737
**Context** *upstream*
3838

3939
Defines a server for an upstream. The differences between the default `server` and `dynamic_server` implementation:
@@ -43,11 +43,12 @@ Defines a server for an upstream. The differences between the default `server` a
4343

4444
The following parameters can be used (see nginx's [server documentation](http://nginx.org/en/docs/http/ngx_http_upstream_module.html#server) for details):
4545

46-
*weight=number*
47-
*max_fails=number*
48-
*fail_timeout=time*
49-
*backup*
50-
*down*
46+
*weight=number*
47+
*max_fails=number*
48+
*fail_timeout=time*
49+
*backup*
50+
*down*
51+
*resolve*
5152

5253
# Compatibility
5354

config

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
ngx_addon_name=ngx_http_upstream_dynamic_servers_module
2-
HTTP_MODULES="$HTTP_MODULES ngx_http_upstream_dynamic_servers_module"
2+
HTTP_MODULES=$(echo $HTTP_MODULES | sed "s/ngx_http_upstream_module/ngx_http_upstream_dynamic_servers_module ngx_http_upstream_module/")
33
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_upstream_dynamic_servers.c"

ngx_http_upstream_dynamic_servers.c

+36-34
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static ngx_resolver_node_t *ngx_resolver_lookup_name(ngx_resolver_t *r, ngx_str_
3737

3838
static ngx_command_t ngx_http_upstream_dynamic_servers_commands[] = {
3939
{
40-
ngx_string("dynamic_server"),
40+
ngx_string("server"),
4141
NGX_HTTP_UPS_CONF | NGX_CONF_1MORE,
4242
ngx_http_upstream_dynamic_server_directive,
4343
0,
@@ -77,17 +77,16 @@ ngx_module_t ngx_http_upstream_dynamic_servers_module = {
7777
NGX_MODULE_V1_PADDING
7878
};
7979

80-
// Implement the "dynamic_server" directive so it's compatible with the nginx
81-
// default "server" directive. Most of this function is based on the default
80+
// Overwrite the nginx "server" directive based on its
8281
// implementation of "ngx_http_upstream_server" from
8382
// src/http/ngx_http_upstream.c (nginx version 1.7.7), and should be kept in
8483
// sync with nginx's source code. Customizations noted in comments.
84+
// This make possible use the same syntax of nginx comercial version.
8585
static char * ngx_http_upstream_dynamic_server_directive(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) {
86-
// BEGIN CUSTOMIZATION: "dynamic_server" differs from "server" implementation
87-
ngx_http_upstream_dynamic_server_conf_t *dynamic_server;
86+
// BEGIN CUSTOMIZATION: differs from default "server" implementation
87+
ngx_http_upstream_srv_conf_t *uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
8888
ngx_http_upstream_dynamic_server_main_conf_t *udsmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_upstream_dynamic_servers_module);
89-
90-
ngx_http_upstream_srv_conf_t *uscf = ngx_http_conf_get_module_srv_conf(cf, ngx_http_upstream_module);
89+
ngx_http_upstream_dynamic_server_conf_t *dynamic_server = NULL;
9190
// END CUSTOMIZATION
9291

9392
time_t fail_timeout;
@@ -192,6 +191,35 @@ static char * ngx_http_upstream_dynamic_server_directive(ngx_conf_t *cf, ngx_com
192191
continue;
193192
}
194193

194+
// BEGIN CUSTOMIZATION: differs from default "server" implementation
195+
if (ngx_strcmp(value[i].data, "resolve") == 0) {
196+
// Determine if the server given is an IP address or a hostname by running
197+
// through ngx_parse_url with no_resolve enabled. Only if a hostname is given
198+
// will we add this to the list of dynamic servers that we will resolve again.
199+
200+
ngx_memzero(&u, sizeof(ngx_url_t));
201+
u.url = value[1];
202+
u.default_port = 80;
203+
u.no_resolve = 1;
204+
ngx_parse_url(cf->pool, &u);
205+
if (!u.addrs || !u.addrs[0].sockaddr) {
206+
dynamic_server = ngx_array_push(&udsmcf->dynamic_servers);
207+
if (dynamic_server == NULL) {
208+
return NGX_CONF_ERROR;
209+
}
210+
211+
ngx_memzero(dynamic_server, sizeof(ngx_http_upstream_dynamic_server_conf_t));
212+
dynamic_server->server = us;
213+
dynamic_server->upstream_conf = uscf;
214+
215+
dynamic_server->host = u.host;
216+
dynamic_server->port = (in_port_t) (u.no_port ? u.default_port : u.port);
217+
}
218+
219+
continue;
220+
}
221+
// END CUSTOMIZATION
222+
195223
goto invalid;
196224
}
197225

@@ -200,7 +228,7 @@ static char * ngx_http_upstream_dynamic_server_directive(ngx_conf_t *cf, ngx_com
200228
u.url = value[1];
201229
u.default_port = 80;
202230

203-
// BEGIN CUSTOMIZATION: "dynamic_server" differs from "server" implementation
231+
// BEGIN CUSTOMIZATION: differs from default "server" implementation
204232
if (ngx_parse_url(cf->pool, &u) != NGX_OK) {
205233
if (u.err) {
206234
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
@@ -237,32 +265,6 @@ static char * ngx_http_upstream_dynamic_server_directive(ngx_conf_t *cf, ngx_com
237265
us->max_fails = max_fails;
238266
us->fail_timeout = fail_timeout;
239267

240-
// BEGIN CUSTOMIZATION: "dynamic_server" differs from "server" implementation
241-
242-
// Determine if the server given is an IP address or a hostname by running
243-
// through ngx_parse_url with no_resolve enabled. Only if a hostname is given
244-
// will we add this to the list of dynamic servers that we will resolve again.
245-
246-
ngx_memzero(&u, sizeof(ngx_url_t));
247-
u.url = value[1];
248-
u.default_port = 80;
249-
u.no_resolve = 1;
250-
ngx_parse_url(cf->pool, &u);
251-
if (!u.addrs || !u.addrs[0].sockaddr) {
252-
dynamic_server = ngx_array_push(&udsmcf->dynamic_servers);
253-
if (dynamic_server == NULL) {
254-
return NGX_CONF_ERROR;
255-
}
256-
257-
ngx_memzero(dynamic_server, sizeof(ngx_http_upstream_dynamic_server_conf_t));
258-
dynamic_server->server = us;
259-
dynamic_server->upstream_conf = uscf;
260-
261-
dynamic_server->host = u.host;
262-
dynamic_server->port = (in_port_t) (u.no_port ? u.default_port : u.port);
263-
}
264-
// END CUSTOMIZATION
265-
266268
return NGX_CONF_OK;
267269

268270
invalid:

t/dynamic_server.t

+56-27
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ __DATA__
8383
8484
resolver $TEST_NGINX_RESOLVER;
8585
upstream test_upstream {
86-
dynamic_server foo.blah;
86+
server foo.blah resolve;
8787
}
8888
--- config
8989
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -100,7 +100,7 @@ upstream test_upstream:
100100
101101
resolver $TEST_NGINX_RESOLVER;
102102
upstream test_upstream {
103-
dynamic_server use.opendns.com;
103+
server use.opendns.com resolve;
104104
}
105105
--- config
106106
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -134,7 +134,7 @@ upstream test_upstream:
134134
135135
resolver $TEST_NGINX_RESOLVER;
136136
upstream test_upstream {
137-
dynamic_server use.opendns.com:8080;
137+
server use.opendns.com:8080 resolve;
138138
}
139139
--- config
140140
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -168,7 +168,7 @@ upstream test_upstream:
168168
169169
resolver $TEST_NGINX_RESOLVER;
170170
upstream test_upstream {
171-
dynamic_server 10.10.10.10;
171+
server 10.10.10.10 resolve;
172172
}
173173
--- config
174174
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -195,7 +195,7 @@ upstream test_upstream:
195195
196196
resolver $TEST_NGINX_RESOLVER;
197197
upstream test_upstream {
198-
dynamic_server [fe80::0202:b3ff:fe1e:8329];
198+
server [fe80::0202:b3ff:fe1e:8329] resolve;
199199
}
200200
--- config
201201
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -222,7 +222,7 @@ upstream test_upstream:
222222
223223
resolver $TEST_NGINX_RESOLVER;
224224
upstream test_upstream {
225-
dynamic_server [fe80::0202:b3ff:fe1e:8329]:8081;
225+
server [fe80::0202:b3ff:fe1e:8329]:8081 resolve;
226226
}
227227
--- config
228228
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -243,20 +243,20 @@ upstream test_upstream:
243243
addr = [fe80::0202:b3ff:fe1e:8329]:8081, weight = 1, fail_timeout = 10, max_fails = 1
244244
245245
246-
=== TEST 7: fails to start if the http level resolver is missing and a dynamic_server is present
246+
=== TEST 7: fails to start if the http level resolver is missing and a server is present
247247
--- http_config
248248
$TEST_NGINX_BASE_HTTP_CONF
249249
250250
upstream test_upstream {
251-
dynamic_server use.opendns.com;
251+
server use.opendns.com resolve;
252252
}
253253
--- config
254254
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
255255
--- must_die
256256
--- error_log
257257
resolver must be defined
258258
259-
=== TEST 8: starts if the http level resolver is missing and a dynamic_server is not present
259+
=== TEST 8: starts if the http level resolver is missing and a server is not present
260260
--- http_config
261261
$TEST_NGINX_BASE_HTTP_CONF
262262
@@ -278,7 +278,7 @@ upstream test_upstream:
278278
279279
resolver $TEST_NGINX_RESOLVER;
280280
upstream test_upstream {
281-
dynamic_server use.opendns.com;
281+
server use.opendns.com resolve;
282282
}
283283
--- config
284284
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -307,7 +307,7 @@ upstream test_upstream:
307307
308308
resolver $TEST_NGINX_RESOLVER;
309309
upstream test_upstream {
310-
dynamic_server use.opendns.com;
310+
server use.opendns.com resolve;
311311
}
312312
--- config
313313
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -350,7 +350,7 @@ upstream test_upstream:
350350
351351
resolver $TEST_NGINX_RESOLVER;
352352
upstream test_upstream {
353-
dynamic_server foo.blah;
353+
server foo.blah resolve;
354354
}
355355
--- config
356356
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -379,7 +379,7 @@ upstream test_upstream:
379379
380380
resolver $TEST_NGINX_RESOLVER;
381381
upstream test_upstream {
382-
dynamic_server foo.blah;
382+
server foo.blah resolve;
383383
}
384384
--- config
385385
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -410,7 +410,7 @@ upstream test_upstream:
410410
411411
resolver $TEST_NGINX_RESOLVER;
412412
upstream test_upstream {
413-
dynamic_server use.opendns.com;
413+
server use.opendns.com resolve;
414414
}
415415
--- config
416416
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -454,7 +454,7 @@ upstream test_upstream:
454454
455455
resolver $TEST_NGINX_RESOLVER;
456456
upstream test_upstream {
457-
dynamic_server multi.blah;
457+
server multi.blah resolve;
458458
}
459459
--- config
460460
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -485,8 +485,8 @@ upstream test_upstream:
485485
486486
resolver $TEST_NGINX_RESOLVER;
487487
upstream test_upstream {
488-
dynamic_server use.opendns.com weight=4 max_fails=8 fail_timeout=7;
489-
dynamic_server 127.0.0.8 backup down;
488+
server use.opendns.com weight=4 max_fails=8 fail_timeout=7 resolve;
489+
server 127.0.0.8 backup down resolve;
490490
}
491491
--- config
492492
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -504,21 +504,21 @@ upstream test_upstream:
504504
505505
resolver $TEST_NGINX_RESOLVER;
506506
upstream test_upstream {
507-
dynamic_server google.blah;
508-
dynamic_server yahoo.blah;
509-
dynamic_server bing.blah;
507+
server google.blah resolve;
508+
server yahoo.blah resolve;
509+
server bing.blah resolve;
510510
}
511511
512512
upstream test_upstream2 {
513-
dynamic_server youtube.blah;
514-
dynamic_server vimeo.blah;
515-
dynamic_server netflix.blah;
513+
server youtube.blah resolve;
514+
server vimeo.blah resolve;
515+
server netflix.blah resolve;
516516
}
517517
518518
upstream test_upstream3 {
519-
dynamic_server a.blah;
520-
dynamic_server b.blah;
521-
dynamic_server c.blah;
519+
server a.blah resolve;
520+
server b.blah resolve;
521+
server c.blah resolve;
522522
}
523523
--- config
524524
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -612,7 +612,7 @@ upstream test_upstream3:
612612
resolver $TEST_NGINX_RESOLVER;
613613
upstream test_upstream {
614614
keepalive 30;
615-
dynamic_server local.blah:1983;
615+
server local.blah:1983 resolve;
616616
}
617617
--- config
618618
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
@@ -653,3 +653,32 @@ upstream test_upstream:
653653
addr = 127.255.255.255:1983, weight = 1, fail_timeout = 10, down = true, max_fails = 1
654654
502
655655
--- timeout: 20s
656+
657+
=== TEST 18: do not resolve the IP if the server isn't set to do it
658+
--- http_config
659+
$TEST_NGINX_BASE_HTTP_CONF
660+
661+
resolver $TEST_NGINX_RESOLVER;
662+
upstream test_upstream {
663+
server foo.blah;
664+
}
665+
--- config
666+
$TEST_NGINX_PRINT_UPSTREAMS_LOCATION
667+
668+
location = /test {
669+
content_by_lua '
670+
ngx.sleep(2.1)
671+
ngx.print(ngx.location.capture("/print-upstreams").body)
672+
set_dns_records({"foo.blah 1 A 127.5.5.5"})
673+
ngx.sleep(2.1)
674+
ngx.print(ngx.location.capture("/print-upstreams").body)
675+
';
676+
}
677+
--- request
678+
GET /test
679+
--- response_body
680+
upstream test_upstream:
681+
addr = 127.255.255.255:80, weight = 1, fail_timeout = 10, down = true, max_fails = 1
682+
upstream test_upstream:
683+
addr = 127.255.255.255:80, weight = 1, fail_timeout = 10, down = true, max_fails = 1
684+
--- timeout: 5s

0 commit comments

Comments
 (0)