-
Notifications
You must be signed in to change notification settings - Fork 3
/
healthcheck.conf
133 lines (112 loc) · 4.75 KB
/
healthcheck.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#
# NGINX Advanced Healtcheck
# This configuration requires NGINX Plus
#
# Import the javascript code
js_import healthcheck from conf.d/healthcheck.js;
# Log format for healthcheck requests
log_format healthcheck_log '[$time_local] $remote_addr:$remote_port '
'$server_addr:$server_port '
'"$request" $status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# Internal upstream for time-based healthcheck
upstream server_check {
zone server_check 64k;
# Servers here must be 127.0.0.1:<port>
# <port> is an arbitrary and unique port referenced in
# the "REST API healtcheck monitoring endpoints map" here below
# and in the backserver servers mapping "map"
server 127.0.0.1:10080;
server 127.0.0.1:10081;
}
#
# REST API healtcheck monitoring endpoints map
#
# This map must be defined as:
# key -> all ports declared in the "server_check" upstream
# value -> the REST API endpoint to be called for healthcheck evaluation (see healtcheck.js)
#
map $server_port $healthcheck_endpoint {
10080 http://192.168.1.30:5000/stats;
10081 http://192.168.1.31:5000/stats;
}
#
# Mapping between port number and backend server position
# in the http_balancing_upstream upstream for HTTP(S) traffic (declared in conf.d/loadbalancer.conf)
# and in the tcp_balancing_upstream for TCP/UDP traffic (declared in stream-conf.d/stream-loadbalancer.conf)
#
# This map must be defined as:
# key -> all ports declared in the "server_check" upstream
# value -> position of the upstream server used for load balancing as listed in the "http_balancing_upstream" upstream (for HTTP/S traffic) or the "tcp_balancing_upstream" (for TCP/UDP traffic)
#
# In this example 1 is 192.168.1.30:8080 and 2 is 192.168.1.31:8080 (see conf.d/loadbalancer.conf)
#
map $server_port $backend_server_entry {
10080 1;
10081 2;
}
#
# Internal monitoring server - checks destination servers health every 3 seconds
#
server {
location /dummy {
internal;
health_check interval=3;
proxy_pass http://server_check;
}
}
# Healtcheck server - njs-based healthcheck evaluation and dynamic balancing_upstream configuration
# This server must listen on all ip:port defined as servers in the "server_check" upstream
server {
listen 127.0.0.1:10080;
listen 127.0.0.1:10081;
access_log /var/log/nginx/healthcheck-access_log healthcheck_log;
error_log /var/log/nginx/healthcheck-error_log notice;
# Main location: hooks the javascript code
location / {
js_content healthcheck.check;
}
# Internal location called by the javascript code through a subrequest.
# This location sends the actual REST API request to the monitoring endpoint
location ~ /healthCheck/(.*) {
internal;
proxy_pass $1$query_string;
}
# Internal location to set an upstream server as "up"
# This gets called by the javascript code through a subrequest
location ~ /upstream/up/(.*) {
internal;
proxy_method PATCH;
proxy_set_body '{"down":"false"}';
# Use to dynamically update the HTTP (conf.d) upstream configured in conf.d/loadbalancer.conf
proxy_pass http://127.0.0.1:8080/api/8/http/upstreams/http_balancing_upstream/servers/$1;
# Use to dynamically update the TCP/UDP (stream-conf.d) upstream configured in stream-conf.d/stream-loadbalancer.conf
#proxy_pass http://127.0.0.1:8080/api/8/stream/upstreams/tcp_balancing_upstream/servers/$1;
}
# Internal location to set an upstream server as "down"
# This gets called by the javascript code through a subrequest
location ~ /upstream/down/(.*) {
internal;
proxy_method PATCH;
proxy_set_body '{"down":"true"}';
# Use to dynamically update the HTTP (conf.d) upstream configured in conf.d/loadbalancer.conf
proxy_pass http://127.0.0.1:8080/api/8/http/upstreams/http_balancing_upstream/servers/$1;
# Use to dynamically update the TCP/UDP (stream-conf.d) upstream configured in stream-conf.d/stream-loadbalancer.conf
#proxy_pass http://127.0.0.1:8080/api/8/stream/upstreams/tcp_balancing_upstream/servers/$1;
}
}
# This enables NGINX Plus REST API access, needed by the javascript code
server {
listen 0.0.0.0:8080;
location /api {
api write=on;
# Allows full API access, restrict access for real-world usage
allow all;
}
location / {
root /usr/share/nginx/html;
index dashboard.html;
# Allows full Dashboard access, restrict access for real-world usage
allow all;
}
}