summaryrefslogtreecommitdiff
path: root/pod/nginx/ngx_stream_upstream_module.pod
blob: ec3340838debbcd2c0f0d4301e6b83d3b2c8b133 (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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
=encoding utf-8

=head1 NAME

ngx_stream_upstream_module - Module ngx_stream_upstream_module




=head1



The C<ngx_stream_upstream_module> module (1.9.0)
is used to define groups of servers that can be referenced
by the L<ngx_stream_proxy_module>
directive.




=head1 Example Configuration




    
    upstream <emphasis>backend</emphasis> {
        hash $remote_addr consistent;
    
        server backend1.example.com:12345  weight=5;
        server backend2.example.com:12345;
        server unix:/tmp/backend3;
    
        server backup1.example.com:12345   backup;
        server backup2.example.com:12345   backup;
    }
    
    server {
        listen 12346;
        proxy_pass <emphasis>backend</emphasis>;
    }







Dynamically configurable group with
periodic L<health checks|ngx_stream_upstream_hc_module> is
available as part of our
commercial subscription:

    
    resolver 10.0.0.1;
    
    upstream <emphasis>dynamic</emphasis> {
        zone upstream_dynamic 64k;
    
        server backend1.example.com:12345 weight=5;
        server backend2.example.com:12345 fail_timeout=5s slow_start=30s;
        server 192.0.2.1:12345            max_fails=3;
        server backend3.example.com:12345 resolve;
        server backend4.example.com       service=http resolve;
    
        server backup1.example.com:12345  backup;
        server backup2.example.com:12345  backup;
    }
    
    server {
        listen 12346;
        proxy_pass <emphasis>dynamic</emphasis>;
        health_check;
    }






=head1 Directives

=head2 upstream


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



B<context:> I<stream>





Defines a group of servers.
Servers can listen on different ports.
In addition, servers listening on TCP and UNIX-domain sockets
can be mixed.





Example:

    
    upstream backend {
        server backend1.example.com:12345 weight=5;
        server 127.0.0.1:12345            max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend2;
        server backend3.example.com:12345 resolve;
    
        server backup1.example.com:12345  backup;
    }







By default, connections are distributed between the servers using a
weighted round-robin balancing method.
In the above example, each 7 connections will be distributed as follows:
5 connections go to C<backend1.example.com:12345>
and one connection to each of the second and third servers.
If an error occurs during communication with a server, the connection will
be passed to the next server, and so on until all of the functioning
servers will be tried.
If communication with all servers fails, the connection will be closed.







=head2 server


B<syntax:> server I<I<C<address>> [I<C<parameters>>]>



B<context:> I<upstream>





Defines the I<C<address>> and other I<C<parameters>>
of a server.
The address can be specified as a domain name or IP address
with an obligatory port, or as a UNIX-domain socket path
specified after the “C<unix:>” prefix.
A domain name that resolves to several IP addresses defines
multiple servers at once.





The following parameters can be defined:

=over



=item 
C<weight>=I<C<number>>





sets the weight of the server, by default, 1.



=item 
C<max_conns>=I<C<number>>





limits the maximum I<C<number>> of simultaneous
connections to the proxied server (1.11.5).
Default value is zero, meaning there is no limit.
If the server group does not reside in the shared memory,
the limitation works per each worker process.

B<NOTE>

Prior to version 1.11.5, this parameter was available as part of our
commercial subscription.




=item 
C<max_fails>=I<C<number>>





sets the number of unsuccessful attempts to communicate with the server
that should happen in the duration set by the C<fail_timeout>
parameter to consider the server unavailable for a duration also set by the
C<fail_timeout> parameter.
By default, the number of unsuccessful attempts is set to 1.
The zero value disables the accounting of attempts.
Here, an unsuccessful attempt is an error or timeout
while establishing a connection with the server.



=item 
C<fail_timeout>=I<C<time>>





sets

=over




=item *

the time during which the specified number of unsuccessful attempts to
communicate with the server should happen to consider the server unavailable;



=item *

and the period of time the server will be considered unavailable.



=back


By default, the parameter is set to 10 seconds.



=item 
C<backup>





marks the server as a backup server.
Connections to the backup server will be passed
when the primary servers are unavailable.

B<NOTE>

The parameter cannot be used along with the
L</hash> and L</random> load balancing methods.




=item 
C<down>





marks the server as permanently unavailable.



=item 
C<resolve>





monitors changes of the IP addresses
that correspond to a domain name of the server,
and automatically modifies the upstream configuration
without the need of restarting nginx.
The server group must reside in the shared memory.


In order for this parameter to work,
the C<resolver> directive
must be specified in the
L<stream|ngx_stream_core_module> block
or in the corresponding upstream block.






B<NOTE>

Prior to version 1.27.3, this parameter was available only as part of our
commercial subscription.






=item 
C<service>=I<C<name>>





enables resolving of DNS
L<SRV|https://datatracker.ietf.org/doc/html/rfc2782>
records and sets the service I<C<name>> (1.9.13).
In order for this parameter to work, it is necessary to specify
the L</resolve> parameter for the server
and specify a hostname without a port number.


If the service name does not contain a dot (“C<.>”), then
the L<RFC|https://datatracker.ietf.org/doc/html/rfc2782>-compliant name
is constructed
and the TCP protocol is added to the service prefix.
For example, to look up the
C<_http._tcp.backend.example.com> SRV record,
it is necessary to specify the directive:

    
    server backend.example.com service=http resolve;


If the service name contains one or more dots, then the name is constructed
by joining the service prefix and the server name.
For example, to look up the C<_http._tcp.backend.example.com>
and C<server1.backend.example.com> SRV records,
it is necessary to specify the directives:

    
    server backend.example.com service=_http._tcp resolve;
    server example.com service=server1.backend resolve;







Highest-priority SRV records
(records with the same lowest-number priority value)
are resolved as primary servers,
the rest of SRV records are resolved as backup servers.
If the L</backup> parameter is specified for the server,
high-priority SRV records are resolved as backup servers,
the rest of SRV records are ignored.






B<NOTE>

Prior to version 1.27.3, this parameter was available only as part of our
commercial subscription.







=back







Additionally,
the following parameters are available as part of our
commercial subscription:

=over



=item 
C<slow_start>=I<C<time>>





sets the I<C<time>> during which the server will recover its weight
from zero to a nominal value, when unhealthy server becomes
L<healthy|ngx_stream_upstream_hc_module>,
or when the server becomes available after a period of time
it was considered unavailable.
Default value is zero, i.e. slow start is disabled.

B<NOTE>

The parameter cannot be used along with the
L</hash> and L</random> load balancing methods.





=back








B<NOTE>

If there is only a single server in a group, C<max_fails>,
C<fail_timeout> and C<slow_start> parameters
are ignored, and such a server will never be considered unavailable.








=head2 zone


B<syntax:> zone I<I<C<name>> [I<C<size>>]>



B<context:> I<upstream>





Defines the I<C<name>> and I<C<size>> of the shared
memory zone that keeps the group’s configuration and run-time state that are
shared between worker processes.
Several groups may share the same zone.
In this case, it is enough to specify the I<C<size>> only once.





Additionally,
as part of our commercial subscription,
such groups allow changing the group membership
or modifying the settings of a particular server
without the need of restarting nginx.
The configuration is accessible via the
L<API|ngx_http_api_module> module (1.13.3).

B<NOTE>

Prior to version 1.13.3,
the configuration was accessible only via a special location
handled by
L<ngx_http_upstream_conf_module>.








=head2 state


B<syntax:> state I<I<C<file>>>



B<context:> I<upstream>



This directive appeared in version 1.9.7.





Specifies a I<C<file>> that keeps the state
of the dynamically configurable group.





Examples:

    
    state /var/lib/nginx/state/servers.conf; # path for Linux
    state /var/db/nginx/state/servers.conf;  # path for FreeBSD







The state is currently limited to the list of servers with their parameters.
The file is read when parsing the configuration and is updated each time
the upstream configuration is
L<changed|ngx_http_api_module>.
Changing the file content directly should be avoided.
The directive cannot be used
along with the L</server> directive.






B<NOTE>

Changes made during
L<configuration reload|control>
or L<binary upgrade|control>
can be lost.







B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 hash


B<syntax:> hash I<I<C<key>> [C<consistent>]>



B<context:> I<upstream>





Specifies a load balancing method for a server group
where the client-server mapping is based on the hashed I<C<key>> value.
The I<C<key>> can contain text, variables,
and their combinations (1.11.2).
Usage example:

    
    hash $remote_addr;


Note that adding or removing a server from the group
may result in remapping most of the keys to different servers.
The method is compatible with the
L<Cache::Memcached|https://metacpan.org/pod/Cache::Memcached>
Perl library.





If the C<consistent> parameter is specified,
the L<ketama|https://www.metabrew.com/article/libketama-consistent-hashing-algo-memcached-clients>
consistent hashing method will be used instead.
The method ensures that only a few keys
will be remapped to different servers
when a server is added to or removed from the group.
This helps to achieve a higher cache hit ratio for caching servers.
The method is compatible with the
L<Cache::Memcached::Fast|https://metacpan.org/pod/Cache::Memcached::Fast>
Perl library with the I<C<ketama_points>> parameter set to 160.







=head2 least_conn




B<context:> I<upstream>





Specifies that a group should use a load balancing method where a connection
is passed to the server with the least number of active connections,
taking into account weights of servers.
If there are several such servers, they are tried in turn using a
weighted round-robin balancing method.







=head2 least_time


B<syntax:> least_time I<
    C<connect> E<verbar>
    C<first_byte> E<verbar>
    C<last_byte>
    [C<inflight>]>



B<context:> I<upstream>





Specifies that a group should use a load balancing method where a connection
is passed to the server with the least average time and
least number of active connections, taking into account weights of servers.
If there are several such servers, they are tried in turn using a
weighted round-robin balancing method.





If the C<connect> parameter is specified,
time to
connect
to the upstream server is used.
If the C<first_byte> parameter is specified,
time to receive the
first byte of data is used.
If the C<last_byte> is specified,
time to receive the
last byte of data is used.
If the C<inflight> parameter is specified (1.11.6),
incomplete connections are also taken into account.

B<NOTE>

Prior to version 1.11.6,
incomplete connections were taken into account by default.







B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 random


B<syntax:> random I<[C<two> [I<C<method>>]]>



B<context:> I<upstream>



This directive appeared in version 1.15.1.





Specifies that a group should use a load balancing method where a connection
is passed to a randomly selected server, taking into account weights
of servers.





The optional C<two> parameter
instructs nginx to randomly select
L<two|https://homes.cs.washington.edu/~karlin/papers/balls.pdf>
servers and then choose a server
using the specified C<method>.
The default method is C<least_conn>
which passes a connection to a server
with the least number of active connections.





The C<least_time> method passes a connection to a server
with the least average time and least number of active connections.
If C<least_time=connect> parameter is specified,
time to
connect
to the upstream server is used.
If C<least_time=first_byte> parameter is specified,
time to receive the
first byte of data is used.
If C<least_time=last_byte> is specified,
time to receive the
last byte of data is used.

B<NOTE>

The C<least_time> method is available as a part of our
commercial subscription.








=head2 resolver


B<syntax:> resolver I<
    I<C<address>> ...
    [C<valid>=I<C<time>>]
    [C<ipv4>=C<on>E<verbar>C<off>]
    [C<ipv6>=C<on>E<verbar>C<off>]
    [C<status_zone>=I<C<zone>>]>



B<context:> I<upstream>



This directive appeared in version 1.27.3.





Configures name servers used to resolve names of upstream servers
into addresses, for example:

    
    resolver 127.0.0.1 [::1]:5353;


The address can be specified as a domain name or IP address,
with an optional port.
If port is not specified, the port 53 is used.
Name servers are queried in a round-robin fashion.





By default, nginx will look up both IPv4 and IPv6 addresses while resolving.
If looking up of IPv4 or IPv6 addresses is not desired,
the C<ipv4=off> (1.23.1) or
the C<ipv6=off> parameter can be specified.





By default, nginx caches answers using the TTL value of a response.
The optional C<valid> parameter allows overriding it:

    
    resolver 127.0.0.1 [::1]:5353 valid=30s;



B<NOTE>

To prevent DNS spoofing, it is recommended
configuring DNS servers in a properly secured trusted local network.






The optional C<status_zone> parameter (1.17.5)
enables
L<collection|ngx_http_api_module>
of DNS server statistics of requests and responses
in the specified I<C<zone>>.
The parameter is available as part of our
commercial subscription.






B<NOTE>

Since version 1.17.5 and prior to version 1.27.3,
this directive was available only as part of our
commercial subscription.








=head2 resolver_timeout


B<syntax:> resolver_timeout I<I<C<time>>>


B<default:> I<30s>


B<context:> I<upstream>



This directive appeared in version 1.27.3.





Sets a timeout for name resolution, for example:

    
    resolver_timeout 5s;








B<NOTE>

Since version 1.17.5 and prior to version 1.27.3,
this directive was available only as part of our
commercial subscription.








=head1 Embedded Variables



The C<ngx_stream_upstream_module> module
supports the following embedded variables:

=over



=item C<$upstream_addr>




keeps the IP address and port,
or the path to the UNIX-domain socket of the upstream server (1.11.4).
If several servers were contacted during proxying,
their addresses are separated by commas, e.g.
“C<192.168.1.1:12345, 192.168.1.2:12345, unix:E<sol>tmpE<sol>sock>”.
If a server cannot be selected,
the variable keeps the name of the server group.



=item C<$upstream_bytes_received>




number of bytes received from an upstream server (1.11.4).
Values from several connections
are separated by commas like addresses in the
$upstream_addr variable.



=item C<$upstream_bytes_sent>




number of bytes sent to an upstream server (1.11.4).
Values from several connections
are separated by commas like addresses in the
$upstream_addr variable.



=item C<$upstream_connect_time>




time to connect to the upstream server (1.11.4);
the time is kept in seconds with millisecond resolution.
Times of several connections
are separated by commas like addresses in the
$upstream_addr variable.



=item C<$upstream_first_byte_time>




time to receive the first byte of data (1.11.4);
the time is kept in seconds with millisecond resolution.
Times of several connections
are separated by commas like addresses in the
$upstream_addr variable.



=item C<$upstream_session_time>




session duration in seconds with millisecond resolution (1.11.4).
Times of several connections
are separated by commas like addresses in the
$upstream_addr variable.




=back