summaryrefslogtreecommitdiff
path: root/pod/nginx/ngx_http_upstream_module.pod
blob: fe90aed9b57305dbbbd9c21adb3dc5c8d349f2e1 (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
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
=encoding utf-8

=head1 NAME

ngx_http_upstream_module - Module ngx_http_upstream_module




=head1



The C<ngx_http_upstream_module> module
is used to define groups of servers that can be referenced
by the L<ngx_http_proxy_module>,
L<ngx_http_fastcgi_module>,
L<ngx_http_uwsgi_module>,
L<ngx_http_scgi_module>,
L<ngx_http_memcached_module>, and
L<ngx_http_grpc_module> directives.




=head1 Example Configuration




    
    upstream <emphasis>backend</emphasis> {
        server backend1.example.com       weight=5;
        server backend2.example.com:8080;
        server unix:/tmp/backend3;
    
        server backup1.example.com:8080   backup;
        server backup2.example.com:8080   backup;
    }
    
    server {
        location / {
            proxy_pass http://<emphasis>backend</emphasis>;
        }
    }







Dynamically configurable group with
periodic L<health checks|ngx_http_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      weight=5;
        server backend2.example.com:8080 fail_timeout=5s slow_start=30s;
        server 192.0.2.1                 max_fails=3;
        server backend3.example.com      resolve;
        server backend4.example.com      service=http resolve;
    
        server backup1.example.com:8080  backup;
        server backup2.example.com:8080  backup;
    }
    
    server {
        location / {
            proxy_pass http://<emphasis>dynamic</emphasis>;
            health_check;
        }
    }






=head1 Directives

=head2 upstream


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



B<context:> I<http>





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 weight=5;
        server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
        server unix:/tmp/backend3;
    
        server backup1.example.com  backup;
    }







By default, requests are distributed between the servers using a
weighted round-robin balancing method.
In the above example, each 7 requests will be distributed as follows:
5 requests go to C<backend1.example.com>
and one request to each of the second and third servers.
If an error occurs during communication with a server, the request will
be passed to the next server, and so on until all of the functioning
servers will be tried.
If a successful response could not be obtained from any of the servers,
the client will receive the result of the communication with the last server.







=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 optional port, or as a UNIX-domain socket path
specified after the “C<unix:>” prefix.
If a port is not specified, the port 80 is used.
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 active
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>

If idle keepalive connections,
multiple L<workers|ngx_core_module>,
and the shared memory are enabled,
the total number of active and idle connections to the proxied server
may exceed the C<max_conns> value.


B<NOTE>

Since version 1.5.9 and 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.
What is considered an unsuccessful attempt is defined by the
L<ngx_http_proxy_module>,
L<ngx_http_fastcgi_module>,
L<ngx_http_uwsgi_module>,
L<ngx_http_scgi_module>,
L<ngx_http_memcached_module>, and
L<ngx_http_grpc_module>
directives.



=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.
It will be passed requests when the primary servers are unavailable.

B<NOTE>

The parameter cannot be used along with the
L</hash>, L</ip_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 (1.5.12).
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<http|ngx_http_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<route>=I<C<string>>





sets the server route name.



=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_http_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>, L</ip_hash>, and L</random>
load balancing methods.




=item 
C<drain>





puts the server into the “draining” mode (1.13.6).
In this mode, only requests bound to the server
will be proxied to it.

B<NOTE>

Prior to version 1.13.6,
the parameter could be changed only with the
L<API|ngx_http_api_module> module.





=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>



This directive appeared in version 1.9.0.





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>



This directive appeared in version 1.7.2.





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.
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 ip_hash




B<context:> I<upstream>





Specifies that a group should use a load balancing method where requests
are distributed between servers based on client IP addresses.
The first three octets of the client IPv4 address, or the entire IPv6 address,
are used as a hashing key.
The method ensures that requests from the same client will always be
passed to the same server except when this server is unavailable.
In the latter case client requests will be passed to another server.
Most probably, it will always be the same server as well.

B<NOTE>

IPv6 addresses are supported starting from versions 1.3.2 and 1.2.2.






If one of the servers needs to be temporarily removed, it should
be marked with the C<down> parameter in
order to preserve the current hashing of client IP addresses.





Example:

    
    upstream backend {
        ip_hash;
    
        server backend1.example.com;
        server backend2.example.com;
        server backend3.example.com <emphasis>down</emphasis>;
        server backend4.example.com;
    }








B<NOTE>

Until versions 1.3.1 and 1.2.2, it was not possible to specify a weight for
servers using the C<ip_hash> load balancing method.








=head2 keepalive


B<syntax:> keepalive I<I<C<connections>>>



B<context:> I<upstream>



This directive appeared in version 1.1.4.





Activates the cache for connections to upstream servers.





The I<C<connections>> parameter sets the maximum number of
idle keepalive connections to upstream servers that are preserved in
the cache of each worker process.
When this number is exceeded, the least recently used connections
are closed.

B<NOTE>

It should be particularly noted that the C<keepalive> directive
does not limit the total number of connections to upstream servers
that an nginx worker process can open.
The I<C<connections>> parameter should be set to a number small enough
to let upstream servers process new incoming connections as well.



B<NOTE>

When using load balancing methods other than the default
round-robin method, it is necessary to activate them before
the C<keepalive> directive.






Example configuration of memcached upstream with keepalive connections:

    
    upstream memcached_backend {
        server 127.0.0.1:11211;
        server 10.0.0.2:11211;
    
        keepalive 32;
    }
    
    server {
        ...
    
        location /memcached/ {
            set $memcached_key $uri;
            memcached_pass memcached_backend;
        }
    
    }







For HTTP, the L<ngx_http_proxy_module>
directive should be set to “C<1.1>”
and the C<Connection> header field should be cleared:

    
    upstream http_backend {
        server 127.0.0.1:8080;
    
        keepalive 16;
    }
    
    server {
        ...
    
        location /http/ {
            proxy_pass http://http_backend;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            ...
        }
    }








B<NOTE>

Alternatively, HTTPE<sol>1.0 persistent connections can be used by passing the
C<Connection: Keep-Alive> header field to an upstream server,
though this method is not recommended.






For FastCGI servers, it is required to set
L<ngx_http_fastcgi_module>
for keepalive connections to work:

    
    upstream fastcgi_backend {
        server 127.0.0.1:9000;
    
        keepalive 8;
    }
    
    server {
        ...
    
        location /fastcgi/ {
            fastcgi_pass fastcgi_backend;
            fastcgi_keep_conn on;
            ...
        }
    }








B<NOTE>

SCGI and uwsgi protocols do not have a notion of keepalive connections.








=head2 keepalive_requests


B<syntax:> keepalive_requests I<I<C<number>>>


B<default:> I<1000>


B<context:> I<upstream>



This directive appeared in version 1.15.3.





Sets the maximum number of requests that can be
served through one keepalive connection.
After the maximum number of requests is made, the connection is closed.





Closing connections periodically is necessary to free
per-connection memory allocations.
Therefore, using too high maximum number of requests
could result in excessive memory usage and not recommended.






B<NOTE>

Prior to version 1.19.10, the default value was 100.








=head2 keepalive_time


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


B<default:> I<1h>


B<context:> I<upstream>



This directive appeared in version 1.19.10.





Limits the maximum time during which
requests can be processed through one keepalive connection.
After this time is reached, the connection is closed
following the subsequent request processing.







=head2 keepalive_timeout


B<syntax:> keepalive_timeout I<I<C<timeout>>>


B<default:> I<60s>


B<context:> I<upstream>



This directive appeared in version 1.15.3.





Sets a timeout during which an idle keepalive
connection to an upstream server will stay open.







=head2 ntlm




B<context:> I<upstream>



This directive appeared in version 1.9.2.





Allows proxying requests with
L<NTLM
Authentication|https://en.wikipedia.org/wiki/Integrated_Windows_Authentication>.
The upstream connection is bound to the client connection
once the client sends a request with the C<Authorization>
header field value
starting with “C<Negotiate>” or “C<NTLM>”.
Further client requests will be proxied through the same upstream connection,
keeping the authentication context.





In order for NTLM authentication to work,
it is necessary to enable keepalive connections to upstream servers.
The L<ngx_http_proxy_module>
directive should be set to “C<1.1>”
and the C<Connection> header field should be cleared:

    
    upstream http_backend {
        server 127.0.0.1:8080;
    
        ntlm;
    }
    
    server {
        ...
    
        location /http/ {
            proxy_pass http://http_backend;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            ...
        }
    }








B<NOTE>

When using load balancer methods other than the default
round-robin method, it is necessary to activate them before
the C<ntlm> directive.







B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 least_conn




B<context:> I<upstream>



This directive appeared in version 1.3.1.



This directive appeared in version 1.2.2.





Specifies that a group should use a load balancing method where a request
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<header> E<verbar>
    C<last_byte>
    [C<inflight>]>



B<context:> I<upstream>



This directive appeared in version 1.7.10.





Specifies that a group should use a load balancing method where a request
is passed to the server with the least average response 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<header> parameter is specified,
time to receive the
response header is used.
If the C<last_byte> parameter is specified,
time to receive the full response
is used.
If the C<inflight> parameter is specified (1.11.6),
incomplete requests are also taken into account.

B<NOTE>

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







B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 queue


B<syntax:> queue I<
I<C<number>>
[C<timeout>=I<C<time>>]>



B<context:> I<upstream>



This directive appeared in version 1.5.12.





If an upstream server cannot be selected immediately
while processing a request,
the request will be placed into the queue.
The directive specifies the maximum I<C<number>> of requests
that can be in the queue at the same time.
If the queue is filled up,
or the server to pass the request to cannot be selected within
the time period specified in the C<timeout> parameter,
the C<502> (C<Bad Gateway>)
error will be returned to the client.





The default value of the C<timeout> parameter is 60 seconds.






B<NOTE>

When using load balancer methods other than the default
round-robin method, it is necessary to activate them before
the C<queue> directive.



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 request
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 request to a server
with the least number of active connections.





The C<least_time> method passes a request to a server
with the least average response time and least number of active connections.
If C<least_time=header> is specified, the time to receive the
response header is used.
If C<least_time=last_byte> is specified, the time to receive the
full response 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.
An 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.








=head2 sticky


B<syntax:> sticky I<
    C<cookie> I<C<name>>
    [C<expires=>I<C<time>>]
    [C<domain=>I<C<domain>>]
    [C<httponly>]
    [C<samesite=>C<strict>E<verbar>C<lax>E<verbar>C<none>E<verbar>I<C<$variable>>]
    [C<secure>]
    [C<path=>I<C<path>>]>


B<syntax:> sticky I<
    C<route> I<C<$variable>> ...>


B<syntax:> sticky I<
    C<learn>
    C<create=>I<C<$variable>>
    C<lookup=>I<C<$variable>>
    C<zone=>I<C<name>>:I<C<size>>
    [C<timeout=>I<C<time>>]
    [C<header>]
    [C<sync>]>



B<context:> I<upstream>



This directive appeared in version 1.5.7.





Enables session affinity, which causes requests from the same client to be
passed to the same server in a group of servers.
Three methods are available:

=over


=item C<cookie>







When the C<cookie> method is used, information about the
designated server is passed in an HTTP cookie generated by nginx:

    
    upstream backend {
        server backend1.example.com;
        server backend2.example.com;
    
        sticky cookie srv_id expires=1h domain=.example.com path=/;
    }







A request that comes from a client not yet bound to a particular server
is passed to the server selected by the configured balancing method.
Further requests with this cookie will be passed to the designated server.
If the designated server cannot process a request, the new server is
selected as if the client has not been bound yet.


B<NOTE>

As a load balancing method always tries to evenly distribute the load
considering already bound requests,
the server with a higher number of active bound requests
has less possibility of getting new unbound requests.






The first parameter sets the name of the cookie to be set or inspected.
The cookie value is
a hexadecimal representation of the MD5 hash of the IP address and port,
or of the UNIX-domain socket path.
However, if the “C<route>” parameter of the
L</server> directive is specified, the cookie value will be
the value of the “C<route>” parameter:

    
    upstream backend {
        server backend1.example.com route=<emphasis>a</emphasis>;
        server backend2.example.com route=<emphasis>b</emphasis>;
    
        sticky cookie srv_id expires=1h domain=.example.com path=/;
    }


In this case, the value of the “C<srv_id>” cookie will be
either I<C<a>> or I<C<b>>.





Additional parameters may be as follows:

=over



=item C<expires=>I<C<time>>




Sets the I<C<time>> for which a browser should keep the cookie.
The special value C<max> will cause the cookie to expire on
“C<31 Dec 2037 23:55:55 GMT>”.
If the parameter is not specified, it will cause the cookie to expire at
the end of a browser session.



=item C<domain=>I<C<domain>>




Defines the I<C<domain>> for which the cookie is set.
Parameter value can contain variables (1.11.5).



=item C<httponly>




Adds the C<HttpOnly> attribute to the cookie (1.7.11).



=item C<samesite=>C<strict> E<verbar>
C<lax> E<verbar> C<none> E<verbar> I<C<$variable>>




Adds the C<SameSite> (1.19.4) attribute to the cookie
with one of the following values:
C<Strict>,
C<Lax>,
C<None>, or
using variables (1.23.3).
In the latter case, if the variable value is empty,
the C<SameSite> attribute will not be added to the cookie,
if the value is resolved to
C<Strict>,
C<Lax>, or
C<None>,
the corresponding value will be assigned,
otherwise the C<Strict> value will be assigned.



=item C<secure>




Adds the C<Secure> attribute to the cookie (1.7.11).



=item C<path=>I<C<path>>




Defines the I<C<path>> for which the cookie is set.




=back


If any parameters are omitted, the corresponding cookie fields are not set.





=item C<route>







When the C<route> method is used, proxied server assigns
client a route on receipt of the first request.
All subsequent requests from this client will carry routing information
in a cookie or URI.
This information is compared with the “C<route>” parameter
of the L</server> directive to identify the server to which the
request should be proxied.
If the “C<route>” parameter is not specified, the route name
will be a hexadecimal representation of the MD5 hash of the IP address and port,
or of the UNIX-domain socket path.
If the designated server cannot process a request, the new server is
selected by the configured balancing method as if there is no routing
information in the request.





The parameters of the C<route> method specify variables that
may contain routing information.
The first non-empty variable is used to find the matching server.





Example:

    
    map $cookie_jsessionid $route_cookie {
        ~.+\.(?P<route>\w+)$ $route;
    }
    
    map $request_uri $route_uri {
        ~jsessionid=.+\.(?P<route>\w+)$ $route;
    }
    
    upstream backend {
        server backend1.example.com route=a;
        server backend2.example.com route=b;
    
        sticky route $route_cookie $route_uri;
    }


Here, the route is taken from the “C<JSESSIONID>” cookie
if present in a request.
Otherwise, the route from the URI is used.






=item C<learn>






When the C<learn> method (1.7.1) is used, nginx
analyzes upstream server responses and learns server-initiated sessions
usually passed in an HTTP cookie.

    
    upstream backend {
       server backend1.example.com:8080;
       server backend2.example.com:8081;
    
       sticky learn
              create=$upstream_cookie_examplecookie
              lookup=$cookie_examplecookie
              zone=client_sessions:1m;
    }



In the example, the upstream server creates a session by setting the
cookie “C<EXAMPLECOOKIE>” in the response.
Further requests with this cookie will be passed to the same server.
If the server cannot process the request, the new server is
selected as if the client has not been bound yet.





The parameters C<create> and C<lookup>
specify variables that indicate how new sessions are created and existing
sessions are searched, respectively.
Both parameters may be specified more than once, in which case the first
non-empty variable is used.





Sessions are stored in a shared memory zone, whose I<C<name>> and
I<C<size>> are configured by the C<zone> parameter.
One megabyte zone can store about 4000 sessions on the 64-bit platform.
The sessions that are not accessed during the time specified by the
C<timeout> parameter get removed from the zone.
By default, C<timeout> is set to 10 minutes.





The C<header> parameter (1.13.1) allows creating a session
right after receiving response headers from the upstream server.





The C<sync> parameter (1.13.8) enables
L<synchronization|ngx_stream_zone_sync_module>
of the shared memory zone.






=back








B<NOTE>

This directive is available as part of our
commercial subscription.








=head2 sticky_cookie_insert


B<syntax:> sticky_cookie_insert I<I<C<name>>
[C<expires=>I<C<time>>]
[C<domain=>I<C<domain>>]
[C<path=>I<C<path>>]>



B<context:> I<upstream>





This directive is obsolete since version 1.5.7.
An equivalent
L</sticky> directive with a new syntax should be used instead:

B<NOTE>

C<sticky cookie> I<C<name>>
[C<expires=>I<C<time>>]
[C<domain=>I<C<domain>>]
[C<path=>I<C<path>>];








=head1 Embedded Variables



The C<ngx_http_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.
If several servers were contacted during request processing,
their addresses are separated by commas, e.g.
“C<192.168.1.1:80, 192.168.1.2:80, unix:E<sol>tmpE<sol>sock>”.
If an internal redirect from one server group to another happens,
initiated by
C<X-Accel-Redirect> or
L<ngx_http_core_module>,
then the server addresses from different groups are separated by colons, e.g.
“C<192.168.1.1:80, 192.168.1.2:80, unix:E<sol>tmpE<sol>sock : 192.168.10.1:80, 192.168.10.2:80>”.
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 and colons like addresses in the
$upstream_addr variable.



=item C<$upstream_bytes_sent>




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



=item C<$upstream_cache_status>





keeps the status of accessing a response cache (0.8.3).
The status can be either “C<MISS>”,
“C<BYPASS>”, “C<EXPIRED>”,
“C<STALE>”, “C<UPDATING>”,
“C<REVALIDATED>”, or “C<HIT>”.



=item C<$upstream_connect_time>





keeps time spent on establishing a connection with the upstream server (1.9.1);
the time is kept in seconds with millisecond resolution.
In case of SSL, includes time spent on handshake.
Times of several connections
are separated by commas and colons like addresses in the
$upstream_addr variable.



=item C<$upstream_cookie_>I<C<name>>





cookie with the specified I<C<name>> sent by the upstream server
in the C<Set-Cookie> response header field (1.7.1).
Only the cookies from the response of the last server are saved.



=item C<$upstream_header_time>





keeps time
spent on receiving the response header from the upstream server (1.7.10);
the time is kept in seconds with millisecond resolution.
Times of several responses
are separated by commas and colons like addresses in the
$upstream_addr variable.



=item C<$upstream_http_>I<C<name>>




keep server response header fields.
For example, the C<Server> response header field
is available through the C<$upstream_http_server> variable.
The rules of converting header field names to variable names are the same
as for the variables that start with the
“L<$http_|ngx_http_core_module>” prefix.
Only the header fields from the response of the last server are saved.



=item C<$upstream_last_server_name>




keeps the name of last selected upstream server (1.25.3);
allows passing it
L<through SNI|ngx_http_proxy_module>:

    
    proxy_ssl_server_name on;
    proxy_ssl_name        $upstream_last_server_name;






B<NOTE>

This variable is available as part of our
commercial subscription.







=item C<$upstream_queue_time>




keeps time the request spent in the upstream queue
(1.13.9);
the time is kept in seconds with millisecond resolution.
Times of several responses
are separated by commas and colons like addresses in the
$upstream_addr variable.



=item C<$upstream_response_length>





keeps the length of the response obtained from the upstream server (0.7.27);
the length is kept in bytes.
Lengths of several responses
are separated by commas and colons like addresses in the
$upstream_addr variable.



=item C<$upstream_response_time>





keeps time spent on receiving the response from the upstream server;
the time is kept in seconds with millisecond resolution.
Times of several responses
are separated by commas and colons like addresses in the
$upstream_addr variable.



=item C<$upstream_status>




keeps status code of the response obtained from the upstream server.
Status codes of several responses
are separated by commas and colons like addresses in the
$upstream_addr variable.
If a server cannot be selected,
the variable keeps the C<502> (C<Bad Gateway>) status code.



=item C<$upstream_trailer_>I<C<name>>




keeps fields from the end of the response
obtained from the upstream server (1.13.10).




=back