summaryrefslogtreecommitdiff
path: root/pod/nginx/ngx_http_upstream_hc_module.pod
blob: c640419c63e30f7e6989f5fcfbb01e867ceffb89 (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
=encoding utf-8

=head1 NAME

ngx_http_upstream_hc_module - Module ngx_http_upstream_hc_module




=head1



The C<ngx_http_upstream_hc_module> module
allows enabling periodic health checks of the servers in a
L<group|ngx_http_upstream_module>
referenced in the surrounding location.
The server group must reside in the
L<shared memory|ngx_http_upstream_module>.





If a health check fails,
the server will be considered unhealthy.
If several health checks are defined for the same group of servers,
a single failure of any check will make the corresponding server be
considered unhealthy.
Client requests are not passed to unhealthy servers
and servers in the “checking” state.






B<NOTE>

Please note that most of the variables will have empty values
when used with health checks.







B<NOTE>

This module is available as part of our
commercial subscription.





=head1 Example Configuration




    
    upstream dynamic {
        zone upstream_dynamic 64k;
    
        server backend1.example.com      weight=5;
        server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
        server 192.0.2.1                 max_fails=3;
    
        server backup1.example.com:8080  backup;
        server backup2.example.com:8080  backup;
    }
    
    server {
        location / {
            proxy_pass http://dynamic;
            health_check;
        }
    }


With this configuration, nginx will send “C<E<sol>>” requests to each
server in the C<backend> group every five seconds.
If any communication error or timeout occurs, or a
proxied server responds with the status code other than
2xx or 3xx, the health check will fail, and the server will
be considered unhealthy.





Health checks can be configured to test the status code of a response,
presence of certain header fields and their values,
and the body contents.
Tests are configured separately using the L</match> directive
and referenced in the C<match> parameter
of the  L</health_check> directive:

    
    http {
        server {
        ...
            location / {
                proxy_pass http://backend;
                health_check match=welcome;
            }
        }
    
        match welcome {
            status 200;
            header Content-Type = text/html;
            body ~ "Welcome to nginx!";
        }
    }


This configuration shows that in order for a health check to pass, the response
to a health check request should succeed, have status 200,
and contain “C<Welcome to nginx!>” in the body.




=head1 Directives

=head2 health_check


B<syntax:> health_check I<[I<C<parameters>>]>



B<context:> I<location>





Enables periodic health checks of the servers in a
L<group|ngx_http_upstream_module>
referenced in the surrounding location.





The following optional parameters are supported:

=over



=item 
C<interval>=I<C<time>>





sets the interval between two consecutive health checks,
by default, 5 seconds.



=item 
C<jitter>=I<C<time>>





sets the time within which
each health check will be randomly delayed,
by default, there is no delay.



=item 
C<fails>=I<C<number>>





sets the number of consecutive failed health checks of a particular server
after which this server will be considered unhealthy,
by default, 1.



=item 
C<passes>=I<C<number>>





sets the number of consecutive passed health checks of a particular server
after which the server will be considered healthy,
by default, 1.



=item 
C<uri>=I<C<uri>>





defines the URI used in health check requests,
by default, “C<E<sol>>”.



=item 
C<mandatory> [C<persistent>]







sets the initial “checking” state for a server
until the first health check is completed (1.11.7).
Client requests are not passed to servers in the “checking” state.
If the parameter is not specified,
the server will be initially considered healthy.





The C<persistent> parameter (1.19.7)
sets the initial “up” state for a server after reload
if the server was considered healthy before reload.






=item 
C<match>=I<C<name>>





specifies the C<match> block configuring the tests that a
response should pass in order for a health check to pass.
By default, the response should have status code 2xx or 3xx.



=item 
C<port>=I<C<number>>





defines the port used when connecting to a server
to perform a health check (1.9.7).
By default, equals the
L<ngx_http_upstream_module> port.



=item 
C<type>=C<grpc>
[C<grpc_service>=I<C<name>>]
[C<grpc_status>=I<C<code>>]





enables periodic
L<health
checks|https://github.com/grpc/grpc/blob/master/doc/health-checking.md#grpc-health-checking-protocol> of a gRPC server
or a particular gRPC service specified with the optional
C<grpc_service> parameter (1.19.5).
If the server does not support the gRPC Health Checking Protocol,
the optional C<grpc_status> parameter can be used
to specify non-zero gRPC
L<status|https://github.com/grpc/grpc/blob/master/doc/statuscodes.md#status-codes-and-their-use-in-grpc>
(for example,
status code “C<12>” E<sol> “C<UNIMPLEMENTED>”)
that will be treated as healthy:

    
    health_check mandatory type=grpc grpc_status=12;


The C<type>=C<grpc> parameter
must be specified after all other directive parameters,
C<grpc_service> and C<grpc_status>
must follow C<type>=C<grpc>.
The parameter is not compatible with
C<uri> or
C<match> parameters.



=item 
C<keepalive_time>=I<C<time>>





enables L<keepalive|ngx_http_upstream_module>
connections for health checks and specifies the time during which
requests can be processed through one keepalive connection (1.21.7).
By default keepalive connections are disabled.




=back









=head2 match


B<syntax:> match I<I<C<name>> { B<...> } >



B<context:> I<http>





Defines the named test set used to verify responses to health check requests.





The following items can be tested in a response:

=over



=item C<status 200;>



status is 200


=item C<status ! 500;>



status is not 500


=item C<status 200 204;>



status is 200 or 204


=item C<status ! 301 302;>



status is neither 301 nor 302


=item C<status 200-399;>



status is in the range from 200 to 399


=item C<status ! 400-599;>



status is not in the range from 400 to 599


=item C<status 301-303 307;>



status is either 301, 302, 303, or 307



=back




=over



=item C<header Content-Type = textE<sol>html;>




header contains C<Content-Type>
with value C<textE<sol>html>



=item C<header Content-Type != textE<sol>html;>




header contains C<Content-Type>
with value other than C<textE<sol>html>



=item C<header Connection ~ close;>




header contains C<Connection>
with value matching regular expression C<close>



=item C<header Connection !~ close;>




header contains C<Connection>
with value not matching regular expression C<close>



=item C<header Host;>



header contains C<Host>


=item C<header ! X-Accel-Redirect;>



header lacks C<X-Accel-Redirect>



=back




=over



=item C<body ~ "Welcome to nginx!";>




body matches regular expression “C<Welcome to nginx!>”



=item C<body !~ "Welcome to nginx!";>




body does not match regular expression “C<Welcome to nginx!>”




=back




=over



=item C<require>
                             I<C<$variable>>
                             C<...;>




all specified variables are not empty and not equal to “0” (1.15.9).




=back







If several tests are specified,
the response matches only if it matches all tests.

B<NOTE>

Only the first 256k of the response body are examined.






Examples:

    
    # status is 200, content type is "text/html",
    # and body contains "Welcome to nginx!"
    match welcome {
        status 200;
        header Content-Type = text/html;
        body ~ "Welcome to nginx!";
    }




    
    # status is not one of 301, 302, 303, or 307, and header does not have "Refresh:"
    match not_redirect {
        status ! 301-303 307;
        header ! Refresh;
    }




    
    # status ok and not in maintenance mode
    match server_ok {
        status 200-399;
        body !~ "maintenance mode";
    }




    
    # status is 200 or 204
    map $upstream_status $good_status {
        200 1;
        204 1;
    }
    
    match server_ok {
        require $good_status;
    }