forked from chronolaw/annotated_nginx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_stream_proxy_module.c
2794 lines (2143 loc) · 80.9 KB
/
ngx_stream_proxy_module.c
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
// annotated by chrono since 2016
//
// * ngx_stream_proxy_pass
// * ngx_stream_proxy_handler
// * ngx_stream_proxy_connect_handler
// * ngx_stream_proxy_init_upstream
// * ngx_stream_proxy_process
/*
* Copyright (C) Roman Arutyunyan
* Copyright (C) Nginx, Inc.
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_stream.h>
// 定义各种连接上游服务器的参数
// 例如超时、缓冲区大小
typedef struct {
ngx_addr_t *addr;
ngx_stream_complex_value_t *value;
#if (NGX_HAVE_TRANSPARENT_PROXY)
ngx_uint_t transparent; /* unsigned transparent:1; */
#endif
} ngx_stream_upstream_local_t;
// proxy_pass的各种配置参数,比较容易理解
typedef struct {
ngx_msec_t connect_timeout;
ngx_msec_t timeout;
ngx_msec_t next_upstream_timeout;
size_t buffer_size;
ngx_stream_complex_value_t *upload_rate;
ngx_stream_complex_value_t *download_rate;
ngx_uint_t requests;
ngx_uint_t responses;
ngx_uint_t next_upstream_tries;
ngx_flag_t next_upstream;
ngx_flag_t proxy_protocol;
ngx_flag_t half_close;
ngx_stream_upstream_local_t *local;
ngx_flag_t socket_keepalive;
#if (NGX_STREAM_SSL)
ngx_flag_t ssl_enable;
ngx_flag_t ssl_session_reuse;
ngx_uint_t ssl_protocols;
ngx_str_t ssl_ciphers;
ngx_stream_complex_value_t *ssl_name;
ngx_flag_t ssl_server_name;
ngx_flag_t ssl_verify;
ngx_uint_t ssl_verify_depth;
ngx_str_t ssl_trusted_certificate;
ngx_str_t ssl_crl;
ngx_stream_complex_value_t *ssl_certificate;
ngx_stream_complex_value_t *ssl_certificate_key;
ngx_array_t *ssl_passwords;
ngx_array_t *ssl_conf_commands;
ngx_ssl_t *ssl;
#endif
// 将要转发的上游服务器集群
// 定义为一个upstream{}
ngx_stream_upstream_srv_conf_t *upstream;
// proxy_pass支持复杂变量
ngx_stream_complex_value_t *upstream_value;
} ngx_stream_proxy_srv_conf_t;
// ngx_stream_init_connection->ngx_stream_init_session之后调用,处理请求
// 即content handler
//
// 创建连接上游的结构体
// 里面有如何获取负载均衡server、上下游buf等
// 负载均衡算法初始化
// 准备开始连接,设置开始时间,秒数,没有毫秒
// 最后启动连接
static void ngx_stream_proxy_handler(ngx_stream_session_t *s);
static ngx_int_t ngx_stream_proxy_eval(ngx_stream_session_t *s,
ngx_stream_proxy_srv_conf_t *pscf);
static ngx_int_t ngx_stream_proxy_set_local(ngx_stream_session_t *s,
ngx_stream_upstream_t *u, ngx_stream_upstream_local_t *local);
// 连接上游
// 使用ngx_peer_connection_t连接上游服务器
// 连接失败,需要尝试下一个上游server
// 连接成功要调用init初始化上游
static void ngx_stream_proxy_connect(ngx_stream_session_t *s);
// 分配供上游读取数据的缓冲区
// 进入此函数,肯定已经成功连接了上游服务器
// 修改上游读写事件,不再测试连接,改为ngx_stream_proxy_upstream_handler
// 实际是ngx_stream_proxy_process_connection(ev, !ev->write);
static void ngx_stream_proxy_init_upstream(ngx_stream_session_t *s);
static void ngx_stream_proxy_resolve_handler(ngx_resolver_ctx_t *ctx);
// 连接成功之后的事件处理函数
// 实际是ngx_stream_proxy_process_connection(ev, !ev->write);
static void ngx_stream_proxy_upstream_handler(ngx_event_t *ev);
// 处理下游的handler
// ev->write==true,即向下游写数据
static void ngx_stream_proxy_downstream_handler(ngx_event_t *ev);
// 处理上下游的数据收发
// from_upstream参数标记是否是上游,使用的是ev->write
// 可读是上游,可写是下游
static void ngx_stream_proxy_process_connection(ngx_event_t *ev,
ngx_uint_t from_upstream);
// 第一次连接上游不成功后的handler
// 当上游连接再次有读写事件发生时测试连接
// 测试连接是否成功,失败就再试下一个上游
// 最后还是要调用init初始化上游
static void ngx_stream_proxy_connect_handler(ngx_event_t *ev);
// 测试连接是否成功,失败就再试下一个上游
static ngx_int_t ngx_stream_proxy_test_connect(ngx_connection_t *c);
// 核心处理函数,处理两个连接的数据收发
// 可以处理上下游的数据收发
// 参数标记是否是上游、是否写数据
static void ngx_stream_proxy_process(ngx_stream_session_t *s,
ngx_uint_t from_upstream, ngx_uint_t do_write);
// 1.15.7
static ngx_int_t ngx_stream_proxy_test_finalize(ngx_stream_session_t *s,
ngx_uint_t from_upstream);
// 连接失败,尝试下一个上游server
static void ngx_stream_proxy_next_upstream(ngx_stream_session_t *s);
// 关闭上下游的连接
static void ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_uint_t rc);
static u_char *ngx_stream_proxy_log_error(ngx_log_t *log, u_char *buf,
size_t len);
// 配置结构体相关函数,比较简单
static void *ngx_stream_proxy_create_srv_conf(ngx_conf_t *cf);
static char *ngx_stream_proxy_merge_srv_conf(ngx_conf_t *cf, void *parent,
void *child);
// 解析proxy_pass指令,设置处理handler,在init建立连接之后会调用
// 获取一个upstream{}块的配置信息
static char *ngx_stream_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
static char *ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd,
void *conf);
#if (NGX_STREAM_SSL)
static ngx_int_t ngx_stream_proxy_send_proxy_protocol(ngx_stream_session_t *s);
static char *ngx_stream_proxy_ssl_password_file(ngx_conf_t *cf,
ngx_command_t *cmd, void *conf);
static char *ngx_stream_proxy_ssl_conf_command_check(ngx_conf_t *cf, void *post,
void *data);
static void ngx_stream_proxy_ssl_init_connection(ngx_stream_session_t *s);
static void ngx_stream_proxy_ssl_handshake(ngx_connection_t *pc);
static void ngx_stream_proxy_ssl_save_session(ngx_connection_t *c);
static ngx_int_t ngx_stream_proxy_ssl_name(ngx_stream_session_t *s);
static ngx_int_t ngx_stream_proxy_ssl_certificate(ngx_stream_session_t *s);
static ngx_int_t ngx_stream_proxy_set_ssl(ngx_conf_t *cf,
ngx_stream_proxy_srv_conf_t *pscf);
static ngx_conf_bitmask_t ngx_stream_proxy_ssl_protocols[] = {
{ ngx_string("SSLv2"), NGX_SSL_SSLv2 },
{ ngx_string("SSLv3"), NGX_SSL_SSLv3 },
{ ngx_string("TLSv1"), NGX_SSL_TLSv1 },
{ ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 },
{ ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 },
{ ngx_string("TLSv1.3"), NGX_SSL_TLSv1_3 },
{ ngx_null_string, 0 }
};
static ngx_conf_post_t ngx_stream_proxy_ssl_conf_command_post =
{ ngx_stream_proxy_ssl_conf_command_check };
#endif
static ngx_conf_deprecated_t ngx_conf_deprecated_proxy_downstream_buffer = {
ngx_conf_deprecated, "proxy_downstream_buffer", "proxy_buffer_size"
};
static ngx_conf_deprecated_t ngx_conf_deprecated_proxy_upstream_buffer = {
ngx_conf_deprecated, "proxy_upstream_buffer", "proxy_buffer_size"
};
// proxy模块定义的各种指令
static ngx_command_t ngx_stream_proxy_commands[] = {
// 关键指令, 设置处理handler,在init建立连接之后会调用
{ ngx_string("proxy_pass"),
NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_stream_proxy_pass,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("proxy_bind"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE12,
ngx_stream_proxy_bind,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("proxy_socket_keepalive"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, socket_keepalive),
NULL },
{ ngx_string("proxy_connect_timeout"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, connect_timeout),
NULL },
{ ngx_string("proxy_timeout"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, timeout),
NULL },
{ ngx_string("proxy_buffer_size"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, buffer_size),
NULL },
{ ngx_string("proxy_downstream_buffer"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, buffer_size),
&ngx_conf_deprecated_proxy_downstream_buffer },
{ ngx_string("proxy_upstream_buffer"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, buffer_size),
&ngx_conf_deprecated_proxy_upstream_buffer },
{ ngx_string("proxy_upload_rate"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_stream_set_complex_value_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, upload_rate),
NULL },
{ ngx_string("proxy_download_rate"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_stream_set_complex_value_size_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, download_rate),
NULL },
{ ngx_string("proxy_requests"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, requests),
NULL },
{ ngx_string("proxy_responses"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, responses),
NULL },
{ ngx_string("proxy_next_upstream"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, next_upstream),
NULL },
{ ngx_string("proxy_next_upstream_tries"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, next_upstream_tries),
NULL },
{ ngx_string("proxy_next_upstream_timeout"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, next_upstream_timeout),
NULL },
{ ngx_string("proxy_protocol"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, proxy_protocol),
NULL },
{ ngx_string("proxy_half_close"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, half_close),
NULL },
#if (NGX_STREAM_SSL)
{ ngx_string("proxy_ssl"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_enable),
NULL },
{ ngx_string("proxy_ssl_session_reuse"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_session_reuse),
NULL },
{ ngx_string("proxy_ssl_protocols"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_protocols),
&ngx_stream_proxy_ssl_protocols },
{ ngx_string("proxy_ssl_ciphers"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_ciphers),
NULL },
{ ngx_string("proxy_ssl_name"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_stream_set_complex_value_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_name),
NULL },
{ ngx_string("proxy_ssl_server_name"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_server_name),
NULL },
{ ngx_string("proxy_ssl_verify"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG,
ngx_conf_set_flag_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_verify),
NULL },
{ ngx_string("proxy_ssl_verify_depth"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_verify_depth),
NULL },
{ ngx_string("proxy_ssl_trusted_certificate"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_trusted_certificate),
NULL },
{ ngx_string("proxy_ssl_crl"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_crl),
NULL },
{ ngx_string("proxy_ssl_certificate"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_stream_set_complex_value_zero_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_certificate),
NULL },
{ ngx_string("proxy_ssl_certificate_key"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_stream_set_complex_value_zero_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_certificate_key),
NULL },
{ ngx_string("proxy_ssl_password_file"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1,
ngx_stream_proxy_ssl_password_file,
NGX_STREAM_SRV_CONF_OFFSET,
0,
NULL },
{ ngx_string("proxy_ssl_conf_command"),
NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE2,
ngx_conf_set_keyval_slot,
NGX_STREAM_SRV_CONF_OFFSET,
offsetof(ngx_stream_proxy_srv_conf_t, ssl_conf_commands),
&ngx_stream_proxy_ssl_conf_command_post },
#endif
ngx_null_command
};
// 没有main配置,都在server{}里
static ngx_stream_module_t ngx_stream_proxy_module_ctx = {
NULL, /* preconfiguration */
NULL, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
ngx_stream_proxy_create_srv_conf, /* create server configuration */
ngx_stream_proxy_merge_srv_conf /* merge server configuration */
};
// 模块功能集成定义
ngx_module_t ngx_stream_proxy_module = {
NGX_MODULE_V1,
&ngx_stream_proxy_module_ctx, /* module context */
ngx_stream_proxy_commands, /* module directives */
NGX_STREAM_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
// ngx_stream_init_connection->ngx_stream_init_session之后调用,处理请求
// 创建连接上游的结构体
// 里面有如何获取负载均衡server、上下游buf等
// 负载均衡算法初始化
// 准备开始连接,设置开始时间,秒数,没有毫秒
// 使用内存池分配接收数据的缓冲区
// 最后启动连接
static void
ngx_stream_proxy_handler(ngx_stream_session_t *s)
{
u_char *p;
ngx_str_t *host;
ngx_uint_t i;
ngx_connection_t *c;
ngx_resolver_ctx_t *ctx, temp;
ngx_stream_upstream_t *u;
ngx_stream_core_srv_conf_t *cscf;
ngx_stream_proxy_srv_conf_t *pscf;
ngx_stream_upstream_srv_conf_t *uscf, **uscfp;
ngx_stream_upstream_main_conf_t *umcf;
// 获取连接对象
c = s->connection;
pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module);
ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0,
"proxy connection handler");
// 创建连接上游的结构体
// 里面有如何获取负载均衡server、上下游buf等
u = ngx_pcalloc(c->pool, sizeof(ngx_stream_upstream_t));
if (u == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
// 关联到会话对象
s->upstream = u;
s->log_handler = ngx_stream_proxy_log_error;
// 1.15新增
u->requests = 1;
u->peer.log = c->log;
u->peer.log_error = NGX_ERROR_ERR;
if (ngx_stream_proxy_set_local(s, u, pscf->local) != NGX_OK) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
if (pscf->socket_keepalive) {
u->peer.so_keepalive = 1;
}
// 连接的类型,tcp or udp
u->peer.type = c->type;
// 开始连接后端的时间
// 准备开始连接,设置开始时间,秒数,没有毫秒
u->start_sec = ngx_time();
// 连接的读写事件都设置为ngx_stream_proxy_downstream_handler
// 注意这个连接是客户端发起的连接,即下游
// 当客户端连接可读或可写时就会调用ngx_stream_proxy_downstream_handler
c->write->handler = ngx_stream_proxy_downstream_handler;
c->read->handler = ngx_stream_proxy_downstream_handler;
// 使用数组,可能会连接多个上游服务器
s->upstream_states = ngx_array_create(c->pool, 1,
sizeof(ngx_stream_upstream_state_t));
if (s->upstream_states == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
// 1.15给udp/tcp都创建一个缓冲区,用来接收数据
p = ngx_pnalloc(c->pool, pscf->buffer_size);
if (p == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
// 注意是给下游使用的缓冲区
u->downstream_buf.start = p;
u->downstream_buf.end = p + pscf->buffer_size;
u->downstream_buf.pos = p;
u->downstream_buf.last = p;
// 连接可读,表示客户端有数据发过来
// 加入到&ngx_posted_events
// 稍后由ngx_stream_proxy_downstream_handler来处理
if (c->read->ready) {
ngx_post_event(c->read, &ngx_posted_events);
}
// udp不需要,始终用一个固定大小的数组接收数据
// proxy_pass支持复杂变量
// 如果使用了"proxy_pass $xxx",那么就要解析复杂变量
if (pscf->upstream_value) {
if (ngx_stream_proxy_eval(s, pscf) != NGX_OK) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
}
// 检查proxy_pass的目标地址
if (u->resolved == NULL) {
uscf = pscf->upstream;
} else {
#if (NGX_STREAM_SSL)
u->ssl_name = u->resolved->host;
#endif
host = &u->resolved->host;
// 获取上游的配置结构体
// 在ngx_stream_proxy_pass里设置的
//uscf = pscf->upstream;
umcf = ngx_stream_get_module_main_conf(s, ngx_stream_upstream_module);
uscfp = umcf->upstreams.elts;
for (i = 0; i < umcf->upstreams.nelts; i++) {
uscf = uscfp[i];
if (uscf->host.len == host->len
&& ((uscf->port == 0 && u->resolved->no_port)
|| uscf->port == u->resolved->port)
&& ngx_strncasecmp(uscf->host.data, host->data, host->len) == 0)
{
goto found;
}
}
if (u->resolved->sockaddr) {
if (u->resolved->port == 0
&& u->resolved->sockaddr->sa_family != AF_UNIX)
{
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"no port in upstream \"%V\"", host);
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
if (ngx_stream_upstream_create_round_robin_peer(s, u->resolved)
!= NGX_OK)
{
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
ngx_stream_proxy_connect(s);
return;
}
if (u->resolved->port == 0) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"no port in upstream \"%V\"", host);
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
temp.name = *host;
cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module);
ctx = ngx_resolve_start(cscf->resolver, &temp);
if (ctx == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
if (ctx == NGX_NO_RESOLVER) {
ngx_log_error(NGX_LOG_ERR, c->log, 0,
"no resolver defined to resolve %V", host);
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
ctx->name = *host;
ctx->handler = ngx_stream_proxy_resolve_handler;
ctx->data = s;
ctx->timeout = cscf->resolver_timeout;
u->resolved->ctx = ctx;
if (ngx_resolve_name(ctx) != NGX_OK) {
u->resolved->ctx = NULL;
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
return;
}
found:
if (uscf == NULL) {
ngx_log_error(NGX_LOG_ALERT, c->log, 0, "no upstream configuration");
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
u->upstream = uscf;
#if (NGX_STREAM_SSL)
u->ssl_name = uscf->host;
#endif
// 负载均衡算法初始化
if (uscf->peer.init(s, uscf) != NGX_OK) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
// 准备开始连接,设置开始时间,毫秒
u->peer.start_time = ngx_current_msec;
// 设置负载均衡的重试次数
if (pscf->next_upstream_tries
&& u->peer.tries > pscf->next_upstream_tries)
{
u->peer.tries = pscf->next_upstream_tries;
}
//u->proxy_protocol = pscf->proxy_protocol;
// 最后启动连接
// 使用ngx_peer_connection_t连接上游服务器
// 连接失败,需要尝试下一个上游server
// 连接成功要调用init初始化上游
ngx_stream_proxy_connect(s);
}
// proxy_pass支持复杂变量
// 如果使用了"proxy_pass $xxx",那么就要解析复杂变量
static ngx_int_t
ngx_stream_proxy_eval(ngx_stream_session_t *s,
ngx_stream_proxy_srv_conf_t *pscf)
{
ngx_str_t host;
ngx_url_t url;
ngx_stream_upstream_t *u;
if (ngx_stream_complex_value(s, pscf->upstream_value, &host) != NGX_OK) {
return NGX_ERROR;
}
ngx_memzero(&url, sizeof(ngx_url_t));
url.url = host;
url.no_resolve = 1;
if (ngx_parse_url(s->connection->pool, &url) != NGX_OK) {
if (url.err) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"%s in upstream \"%V\"", url.err, &url.url);
}
return NGX_ERROR;
}
u = s->upstream;
u->resolved = ngx_pcalloc(s->connection->pool,
sizeof(ngx_stream_upstream_resolved_t));
if (u->resolved == NULL) {
return NGX_ERROR;
}
if (url.addrs) {
u->resolved->sockaddr = url.addrs[0].sockaddr;
u->resolved->socklen = url.addrs[0].socklen;
u->resolved->name = url.addrs[0].name;
u->resolved->naddrs = 1;
}
u->resolved->host = url.host;
u->resolved->port = url.port;
u->resolved->no_port = url.no_port;
return NGX_OK;
}
static ngx_int_t
ngx_stream_proxy_set_local(ngx_stream_session_t *s, ngx_stream_upstream_t *u,
ngx_stream_upstream_local_t *local)
{
ngx_int_t rc;
ngx_str_t val;
ngx_addr_t *addr;
if (local == NULL) {
u->peer.local = NULL;
return NGX_OK;
}
#if (NGX_HAVE_TRANSPARENT_PROXY)
u->peer.transparent = local->transparent;
#endif
if (local->value == NULL) {
u->peer.local = local->addr;
return NGX_OK;
}
if (ngx_stream_complex_value(s, local->value, &val) != NGX_OK) {
return NGX_ERROR;
}
if (val.len == 0) {
return NGX_OK;
}
addr = ngx_palloc(s->connection->pool, sizeof(ngx_addr_t));
if (addr == NULL) {
return NGX_ERROR;
}
rc = ngx_parse_addr_port(s->connection->pool, addr, val.data, val.len);
if (rc == NGX_ERROR) {
return NGX_ERROR;
}
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
"invalid local address \"%V\"", &val);
return NGX_OK;
}
addr->name = val;
u->peer.local = addr;
return NGX_OK;
}
// 连接上游
// 使用ngx_peer_connection_t连接上游服务器
// 连接失败,需要尝试下一个上游server
// 连接成功要调用init初始化上游
static void
ngx_stream_proxy_connect(ngx_stream_session_t *s)
{
ngx_int_t rc;
ngx_connection_t *c, *pc;
ngx_stream_upstream_t *u;
ngx_stream_proxy_srv_conf_t *pscf;
// 获取连接对象
c = s->connection;
c->log->action = "connecting to upstream";
pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module);
// 连接上游的结构体
// 里面有如何获取负载均衡server、上下游buf等
u = s->upstream;
u->connected = 0;
u->proxy_protocol = pscf->proxy_protocol;
// 何时会执行这个?
if (u->state) {
u->state->response_time = ngx_current_msec - u->start_time;
}
// 把一个上游的状态添加进会话的数组
u->state = ngx_array_push(s->upstream_states);
if (u->state == NULL) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
ngx_memzero(u->state, sizeof(ngx_stream_upstream_state_t));
// 1.15.7
u->start_time = ngx_current_msec;
// 这两个值置为-1,表示未初始化
u->state->connect_time = (ngx_msec_t) -1;
u->state->first_byte_time = (ngx_msec_t) -1;
// 用来计算响应时间,保存当前的毫秒值
// 之后连接成功后就会两者相减
u->state->response_time = (ngx_msec_t) -1;
// 连接上游
// 使用ngx_peer_connection_t连接上游服务器
// 从upstream{}里获取一个上游server地址
// 从cycle的连接池获取一个空闲连接
// 设置连接的数据收发接口函数
// 向epoll添加连接,即同时添加读写事件
// 当与上游服务器有任何数据收发时都会触发epoll
// socket api调用连接上游服务器
// 写事件ready,即可以立即向上游发送数据
rc = ngx_event_connect_peer(&u->peer);
ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "proxy connect: %i", rc);
if (rc == NGX_ERROR) {
ngx_stream_proxy_finalize(s, NGX_STREAM_INTERNAL_SERVER_ERROR);
return;
}
u->state->peer = u->peer.name;
// 所有上游都busy
if (rc == NGX_BUSY) {
ngx_log_error(NGX_LOG_ERR, c->log, 0, "no live upstreams");
ngx_stream_proxy_finalize(s, NGX_STREAM_BAD_GATEWAY);
return;
}
// 连接失败,需要尝试下一个上游server
if (rc == NGX_DECLINED) {
ngx_stream_proxy_next_upstream(s);
return;
}
/* rc == NGX_OK || rc == NGX_AGAIN || rc == NGX_DONE */
// 连接“成功”,again/done表示正在连接过程中
pc = u->peer.connection;
pc->data = s;
pc->log = c->log;
pc->pool = c->pool;
pc->read->log = c->log;
pc->write->log = c->log;
// 连接成功
// 分配供上游读取数据的缓冲区
// 修改上游读写事件,不再测试连接,改为ngx_stream_proxy_upstream_handler
// 实际是ngx_stream_proxy_process_connection(ev, !ev->write);
if (rc != NGX_AGAIN) {
ngx_stream_proxy_init_upstream(s);
return;
}
// again,要再次尝试连接
// 设置上游的读写事件处理函数是ngx_stream_proxy_connect_handler
// 第一次连接上游不成功后的handler
// 当上游连接再次有读写事件发生时测试连接
// 测试连接是否成功,失败就再试下一个上游
// 最后还是要调用init初始化上游
pc->read->handler = ngx_stream_proxy_connect_handler;
pc->write->handler = ngx_stream_proxy_connect_handler;
// 连接上游先写,所以设置写事件的超时时间
ngx_add_timer(pc->write, pscf->connect_timeout);
}
// 分配供上游读取数据的缓冲区
// 进入此函数,肯定已经成功连接了上游服务器
// 修改上游读写事件,不再测试连接,改为ngx_stream_proxy_upstream_handler
// 实际是ngx_stream_proxy_process_connection(ev, !ev->write);
static void
ngx_stream_proxy_init_upstream(ngx_stream_session_t *s)
{
u_char *p;
ngx_chain_t *cl;
ngx_connection_t *c, *pc;
ngx_log_handler_pt handler;
ngx_stream_upstream_t *u;
ngx_stream_core_srv_conf_t *cscf;
ngx_stream_proxy_srv_conf_t *pscf;
// u保存了上游相关的信息
u = s->upstream;
// pc是上游的连接对象
pc = u->peer.connection;
cscf = ngx_stream_get_module_srv_conf(s, ngx_stream_core_module);
if (pc->type == SOCK_STREAM
&& cscf->tcp_nodelay
&& ngx_tcp_nodelay(pc) != NGX_OK)
{
ngx_stream_proxy_next_upstream(s);
return;
}
pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module);
#if (NGX_STREAM_SSL)
if (pc->type == SOCK_STREAM && pscf->ssl) {
if (u->proxy_protocol) {
if (ngx_stream_proxy_send_proxy_protocol(s) != NGX_OK) {
return;
}
u->proxy_protocol = 0;
}
if (pc->ssl == NULL) {
ngx_stream_proxy_ssl_init_connection(s);
return;
}
}
#endif
// c是到客户端即下游的连接对象
c = s->connection;
if (c->log->log_level >= NGX_LOG_INFO) {
ngx_str_t str;
u_char addr[NGX_SOCKADDR_STRLEN];
str.len = NGX_SOCKADDR_STRLEN;
str.data = addr;
if (ngx_connection_local_sockaddr(pc, &str, 1) == NGX_OK) {
handler = c->log->handler;
c->log->handler = NULL;
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"%sproxy %V connected to %V",
pc->type == SOCK_DGRAM ? "udp " : "",