Skip to content

Commit 208423f

Browse files
committed
Initial commit.
0 parents  commit 208423f

10 files changed

+1489
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/t/build
2+
/t/servroot
3+
/t/tmp

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2014 Nick Muerdter
2+
3+
MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Makefile

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
export PATH := $(PWD)/t/build/sbin:$(PWD)/t/build/bin:$(PATH)
2+
export PERL5LIB := $(PWD)/t/build/lib/perl5
3+
export UNBOUND_PID := $(PWD)/t/build/etc/unbound/unbound.pid
4+
5+
nginx=nginx-1.7.7
6+
nginx_url=http://nginx.org/download/$(nginx).tar.gz
7+
8+
clean:
9+
rm -rf t/build t/servroot t/tmp
10+
11+
test: t/build/lib/perl5 t/build/sbin/unbound t/build/sbin/nginx
12+
echo "" > /tmp/nginx_upstream_dynamic_servers_unbound_active_test.conf
13+
echo "" > /tmp/unbound.log
14+
if [ -f $(UNBOUND_PID) ] && ps -p `cat $(UNBOUND_PID)` > /dev/null; then kill -QUIT `cat $(UNBOUND_PID)`; fi
15+
sleep 0.2
16+
env PATH=$(PATH) unbound -c $(PWD)/t/unbound/unbound.conf -vvv > $(PWD)/t/tmp/unbound.log 2>&1
17+
env PATH=$(PATH) PERL5LIB=$(PERL5LIB) UNBOUND_PID=$(UNBOUND_PID) LD_LIBRARY_PATH=$(PWD)/t/build/lib:$(LD_LIBRARY_PATH) prove
18+
STATUS=$$?
19+
if [ -f $(UNBOUND_PID) ] && ps -p `cat $(UNBOUND_PID)` > /dev/null; then kill -QUIT `cat $(UNBOUND_PID)`; fi
20+
exit $$STATUS
21+
22+
grind:
23+
env TEST_NGINX_USE_VALGRIND=1 $(MAKE) test
24+
25+
t/tmp:
26+
mkdir -p $@
27+
touch $@
28+
29+
t/tmp/cpanm: | t/tmp
30+
curl -o $@ -L http://cpanmin.us
31+
chmod +x $@
32+
touch $@
33+
34+
t/build/lib/perl5: t/tmp/cpanm
35+
$< -L t/build --notest LWP::Protocol::https
36+
$< -L t/build --notest https://github.com/openresty/test-nginx/archive/8d5c8668364251cdae01ccf1ef933d80b642982d.tar.gz
37+
touch $@
38+
39+
t/tmp/unbound-1.4.22.tar.gz: | t/tmp
40+
curl -o $@ "http://unbound.net/downloads/unbound-1.4.22.tar.gz"
41+
42+
t/tmp/unbound-1.4.22: t/tmp/unbound-1.4.22.tar.gz
43+
tar -C t/tmp -xf $<
44+
touch $@
45+
46+
t/tmp/unbound-1.4.22/Makefile: | t/tmp/unbound-1.4.22
47+
cd t/tmp/unbound-1.4.22 && ./configure --prefix=$(PWD)/t/build
48+
touch $@
49+
50+
t/tmp/unbound-1.4.22/unbound: t/tmp/unbound-1.4.22/Makefile
51+
cd t/tmp/unbound-1.4.22 && make
52+
touch $@
53+
54+
t/build/sbin/unbound: t/tmp/unbound-1.4.22/unbound
55+
cd t/tmp/unbound-1.4.22 && make install
56+
touch $@
57+
58+
t/tmp/LuaJIT-2.0.3.tar.gz: | t/tmp
59+
curl -o $@ "http://luajit.org/download/LuaJIT-2.0.3.tar.gz"
60+
61+
t/tmp/LuaJIT-2.0.3: t/tmp/LuaJIT-2.0.3.tar.gz
62+
tar -C t/tmp -xf $<
63+
touch $@
64+
65+
t/tmp/LuaJIT-2.0.3/src/luajit: | t/tmp/LuaJIT-2.0.3
66+
cd t/tmp/LuaJIT-2.0.3 && make PREFIX=$(PWD)/t/build
67+
touch $@
68+
69+
t/build/bin/luajit: t/tmp/LuaJIT-2.0.3/src/luajit
70+
cd t/tmp/LuaJIT-2.0.3 && make install PREFIX=$(PWD)/t/build
71+
touch $@
72+
73+
t/tmp/lua-nginx-module-0.9.13rc1.tar.gz: | t/tmp
74+
curl -Lo $@ "https://github.com/openresty/lua-nginx-module/archive/v0.9.13rc1.tar.gz"
75+
76+
t/tmp/lua-nginx-module-0.9.13rc1: t/tmp/lua-nginx-module-0.9.13rc1.tar.gz
77+
tar -C t/tmp -xf $<
78+
touch $@
79+
80+
t/tmp/lua-upstream-nginx-module-0.02.tar.gz: | t/tmp
81+
curl -Lo $@ "https://github.com/openresty/lua-upstream-nginx-module/archive/v0.02.tar.gz"
82+
83+
t/tmp/lua-upstream-nginx-module-0.02: t/tmp/lua-upstream-nginx-module-0.02.tar.gz
84+
tar -C t/tmp -xf $<
85+
touch $@
86+
87+
t/tmp/$(nginx).tar.gz: | t/tmp
88+
curl -o $@ $(nginx_url)
89+
90+
t/tmp/$(nginx): t/tmp/$(nginx).tar.gz
91+
tar -C t/tmp -xf $<
92+
touch $@
93+
94+
t/tmp/$(nginx)/.patches-applied: | t/tmp/$(nginx)
95+
curl https://raw.githubusercontent.com/openresty/no-pool-nginx/master/$(nginx)-no_pool.patch | patch -d t/tmp/$(nginx) -p1 --quiet
96+
touch $@
97+
98+
t/tmp/$(nginx)/Makefile: config | t/tmp/$(nginx) t/tmp/$(nginx)/.patches-applied t/build/bin/luajit t/tmp/lua-nginx-module-0.9.13rc1 t/tmp/lua-upstream-nginx-module-0.02
99+
cd t/tmp/$(nginx) && env \
100+
LUAJIT_LIB=$(PWD)/t/build/lib \
101+
LUAJIT_INC=$(PWD)/t/build/include/luajit-2.0 \
102+
./configure \
103+
--prefix=$(PWD)/t/build \
104+
--with-debug \
105+
--with-ipv6 \
106+
--add-module=$(PWD)/t/tmp/lua-nginx-module-0.9.13rc1 \
107+
--add-module=$(PWD)/t/tmp/lua-upstream-nginx-module-0.02 \
108+
--add-module=$(PWD) \
109+
--without-http_charset_module \
110+
--without-http_userid_module \
111+
--without-http_auth_basic_module \
112+
--without-http_autoindex_module \
113+
--without-http_geo_module \
114+
--without-http_split_clients_module \
115+
--without-http_referer_module \
116+
--without-http_fastcgi_module \
117+
--without-http_uwsgi_module \
118+
--without-http_scgi_module \
119+
--without-http_memcached_module \
120+
--without-http_limit_conn_module \
121+
--without-http_limit_req_module \
122+
--without-http_empty_gif_module \
123+
--without-http_browser_module \
124+
--without-http_upstream_ip_hash_module
125+
126+
t/tmp/$(nginx)/objs/nginx: t/tmp/$(nginx)/Makefile *.c
127+
cd t/tmp/$(nginx) && make
128+
129+
t/build/sbin/nginx: t/tmp/$(nginx)/objs/nginx
130+
cd t/tmp/$(nginx) && make install

README.md

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# nginx-upstream-dyanmic-servers
2+
3+
An nginx module to resolve domain names inside upstreams and keep them up to date.
4+
5+
By default, servers defined in nginx upstreams are only resolved when nginx starts. This module provides an alternative `dynamic_server` directive that can be used to asyncronously resolve upstream domain names. This keeps the upstream definition up to date according to the DNS TTL of each domain names. This can be useful if you want to use upstreams for dynamic types of domain names that may frequently change IP addresses.
6+
7+
This module also allows nginx to start if an upstream contains a defunct domain name that no longer resolves. By default, nginx will fail to start if an upstream server contains an unresolvable domain name. With this module, nginx is still allowed to start with invalid domain names, but an error will be logged and the unresolvable domain names will be marked as down.
8+
9+
## Installation
10+
11+
```sh
12+
./configure --add-module=/path/to/nginx-upstream-dynamic-servers
13+
make && make install
14+
```
15+
16+
## Usage
17+
18+
Use the `dynamic_server` definition inside your upstreams instead of the built-in `server` (`dyamic_server` should be a drop-in replacement).
19+
20+
*Note:* A `resolver` must be defined at the `http` level of nginx's config for `dynamic_server` to work.
21+
22+
```
23+
http {
24+
resolver 8.8.8.8;
25+
26+
upstream example {
27+
dynamic_server example.com;
28+
}
29+
}
30+
```
31+
32+
## Directives
33+
34+
### dynamic_server
35+
36+
**Syntax:** *dynamic_server address [parameters]*;
37+
**Context** *upstream*
38+
39+
Defines a server for an upstream. The differences between the default `server` and `dynamic_server` implementation:
40+
41+
- Domain names will be resolved on an ongoing basis and kept up to date according to the TTL of each domain name.
42+
- Unresolvable domain names are considered non-fatal errors (but logged). nginx is allowed to startup if a domain name can't be resolved, but the server is marked as down.
43+
44+
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):
45+
46+
*weight=number*
47+
*max_fails=number*
48+
*fail_timeout=time*
49+
*backup*
50+
*down*
51+
52+
# Compatibility
53+
54+
Tested with nginx 1.7.7.
55+
56+
## Alternatives
57+
58+
- [proxy_pass + resolver](http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass): If you only need to proxy to 1 domain and don't need the additional capabilities of upstreams, nginx's `proxy_pass` can perform resolving at run-time.
59+
- [ngx_upstream_jdomain](http://wiki.nginx.org/HttpUpstreamJdomainModule): An nginx module that asyncronously resolves domain names. The primary differences between jdomain and this module is that this module keeps domain names up to date even if no server traffic is being generated (jdomain requires traffic to each upstream in order to keep it up to date). This module also allows nginx to startup if unresolvable domain names are given.
60+
- [tengine's dynamic_resolve](https://github.com/alibaba/tengine/blob/master/docs/modules/ngx_http_upstream_dynamic.md): If you're using tengine (an nginx fork), there's a new feature (currently unreleased) to support resolving domain names in upstreams at run-time.
61+
- [NGINX Plus](http://nginx.com/resources/admin-guide/load-balancer/#resolve)
62+
63+
## License
64+
65+
nginx-upstream-dyanmic-servers is open sourced under the [MIT license](https://github.com/GUI/nginx-upstream-dyanmic-servers/blob/master/LICENSE.txt).

circle.yml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dependencies:
2+
cache_directories:
3+
- "t/build"
4+
- "t/tmp"
5+
test:
6+
override:
7+
- make test
8+
- make grind

config

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ngx_addon_name=ngx_http_upstream_dynamic_servers_module
2+
HTTP_MODULES="$HTTP_MODULES ngx_http_upstream_dynamic_servers_module"
3+
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_upstream_dynamic_servers.c"

0 commit comments

Comments
 (0)