forked from coreos/corelb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginx.conf
64 lines (54 loc) · 1.38 KB
/
nginx.conf
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
worker_processes 1;
daemon off;
error_log stderr debug;
events {
worker_connections 1024;
}
env ETCD_URL;
env COREINIT_UNIT;
http {
lua_package_path './lib/?.lua;;';
lua_shared_dict lb 10m;
init_by_lua '
';
server {
listen 8080;
location / {
set $_url "";
set $best_upstream "";
lua_need_request_body on;
access_by_lua '
local shlb = ngx.shared.lb
local function set_upstream(premature)
local insync = shlb:get("insync")
if insync ~= true then
shlb:set("insync", true, 1)
local loadbalancer = require("coreinit/loadbalancer")
lb = loadbalancer:new{
etcd=os.getenv("ETCD_URL"),
unit=os.getenv("COREINIT_UNIT")
}
-- TOOD: use a long poll with etcd instead of this hack of caching for 1 second
local err = lb:sync()
if err ~= nil then
print("Error syncing loadbalancer " .. err.errorCode)
else
-- TODO: dont call etcd on every request
local upstream = lb:upstream()
shlb:set("upstream", upstream)
print("Setting upstream to " .. upstream)
end
end
end
local ok, err = ngx.timer.at(0, set_upstream)
if not ok then
ngx.log(ngx.ERR, "failed to create timer: ", err)
return
end
ngx.var.best_upstream = shlb:get("upstream")
ngx.log(ngx.INFO, "Upstream is " .. ngx.var.best_upstream)
';
proxy_pass $best_upstream;
}
}
}