Mercurial > hg > nginx
annotate src/stream/ngx_stream_proxy_module.c @ 6201:24488e6db782
Stream: upstream and downstream limit rates.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Tue, 23 Jun 2015 20:17:48 +0300 |
parents | abee77018d3a |
children | 6345822f0abb |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Roman Arutyunyan | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 typedef void (*ngx_stream_proxy_handler_pt)(ngx_stream_session_t *s); | |
14 | |
15 | |
16 typedef struct { | |
17 ngx_msec_t connect_timeout; | |
18 ngx_msec_t timeout; | |
19 ngx_msec_t next_upstream_timeout; | |
20 size_t downstream_buf_size; | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
21 size_t downstream_limit_rate; |
6115 | 22 size_t upstream_buf_size; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
23 size_t upstream_limit_rate; |
6115 | 24 ngx_uint_t next_upstream_tries; |
25 ngx_flag_t next_upstream; | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
26 ngx_flag_t proxy_protocol; |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
27 ngx_addr_t *local; |
6115 | 28 |
29 #if (NGX_STREAM_SSL) | |
30 ngx_flag_t ssl_enable; | |
31 ngx_flag_t ssl_session_reuse; | |
32 ngx_uint_t ssl_protocols; | |
33 ngx_str_t ssl_ciphers; | |
34 ngx_str_t ssl_name; | |
35 ngx_flag_t ssl_server_name; | |
36 | |
37 ngx_flag_t ssl_verify; | |
38 ngx_uint_t ssl_verify_depth; | |
39 ngx_str_t ssl_trusted_certificate; | |
40 ngx_str_t ssl_crl; | |
41 ngx_str_t ssl_certificate; | |
42 ngx_str_t ssl_certificate_key; | |
43 ngx_array_t *ssl_passwords; | |
44 | |
45 ngx_ssl_t *ssl; | |
46 #endif | |
47 | |
48 ngx_stream_upstream_srv_conf_t *upstream; | |
49 } ngx_stream_proxy_srv_conf_t; | |
50 | |
51 | |
52 static void ngx_stream_proxy_handler(ngx_stream_session_t *s); | |
53 static void ngx_stream_proxy_connect(ngx_stream_session_t *s); | |
54 static void ngx_stream_proxy_init_upstream(ngx_stream_session_t *s); | |
55 static void ngx_stream_proxy_upstream_handler(ngx_event_t *ev); | |
56 static void ngx_stream_proxy_downstream_handler(ngx_event_t *ev); | |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
57 static void ngx_stream_proxy_process_connection(ngx_event_t *ev, |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
58 ngx_uint_t from_upstream); |
6115 | 59 static void ngx_stream_proxy_connect_handler(ngx_event_t *ev); |
60 static ngx_int_t ngx_stream_proxy_test_connect(ngx_connection_t *c); | |
61 static ngx_int_t ngx_stream_proxy_process(ngx_stream_session_t *s, | |
62 ngx_uint_t from_upstream, ngx_uint_t do_write); | |
63 static void ngx_stream_proxy_next_upstream(ngx_stream_session_t *s); | |
64 static void ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_int_t rc); | |
65 static u_char *ngx_stream_proxy_log_error(ngx_log_t *log, u_char *buf, | |
66 size_t len); | |
67 | |
68 static void *ngx_stream_proxy_create_srv_conf(ngx_conf_t *cf); | |
69 static char *ngx_stream_proxy_merge_srv_conf(ngx_conf_t *cf, void *parent, | |
70 void *child); | |
71 static char *ngx_stream_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, | |
72 void *conf); | |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
73 static char *ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd, |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
74 void *conf); |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
75 static ngx_int_t ngx_stream_proxy_send_proxy_protocol(ngx_stream_session_t *s); |
6115 | 76 |
77 #if (NGX_STREAM_SSL) | |
78 | |
79 static char *ngx_stream_proxy_ssl_password_file(ngx_conf_t *cf, | |
80 ngx_command_t *cmd, void *conf); | |
81 static void ngx_stream_proxy_ssl_init_connection(ngx_stream_session_t *s); | |
82 static void ngx_stream_proxy_ssl_handshake(ngx_connection_t *pc); | |
83 static ngx_int_t ngx_stream_proxy_ssl_name(ngx_stream_session_t *s); | |
84 static ngx_int_t ngx_stream_proxy_set_ssl(ngx_conf_t *cf, | |
85 ngx_stream_proxy_srv_conf_t *pscf); | |
86 | |
87 | |
88 static ngx_conf_bitmask_t ngx_stream_proxy_ssl_protocols[] = { | |
89 { ngx_string("SSLv2"), NGX_SSL_SSLv2 }, | |
90 { ngx_string("SSLv3"), NGX_SSL_SSLv3 }, | |
91 { ngx_string("TLSv1"), NGX_SSL_TLSv1 }, | |
92 { ngx_string("TLSv1.1"), NGX_SSL_TLSv1_1 }, | |
93 { ngx_string("TLSv1.2"), NGX_SSL_TLSv1_2 }, | |
94 { ngx_null_string, 0 } | |
95 }; | |
96 | |
97 #endif | |
98 | |
99 | |
100 static ngx_command_t ngx_stream_proxy_commands[] = { | |
101 | |
102 { ngx_string("proxy_pass"), | |
103 NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
104 ngx_stream_proxy_pass, | |
105 NGX_STREAM_SRV_CONF_OFFSET, | |
106 0, | |
107 NULL }, | |
108 | |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
109 { ngx_string("proxy_bind"), |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
110 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
111 ngx_stream_proxy_bind, |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
112 NGX_STREAM_SRV_CONF_OFFSET, |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
113 0, |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
114 NULL }, |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
115 |
6115 | 116 { ngx_string("proxy_connect_timeout"), |
117 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
118 ngx_conf_set_msec_slot, | |
119 NGX_STREAM_SRV_CONF_OFFSET, | |
120 offsetof(ngx_stream_proxy_srv_conf_t, connect_timeout), | |
121 NULL }, | |
122 | |
123 { ngx_string("proxy_timeout"), | |
124 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
125 ngx_conf_set_msec_slot, | |
126 NGX_STREAM_SRV_CONF_OFFSET, | |
127 offsetof(ngx_stream_proxy_srv_conf_t, timeout), | |
128 NULL }, | |
129 | |
130 { ngx_string("proxy_downstream_buffer"), | |
131 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
132 ngx_conf_set_size_slot, | |
133 NGX_STREAM_SRV_CONF_OFFSET, | |
134 offsetof(ngx_stream_proxy_srv_conf_t, downstream_buf_size), | |
135 NULL }, | |
136 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
137 { ngx_string("proxy_downstream_limit_rate"), |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
138 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
139 ngx_conf_set_size_slot, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
140 NGX_STREAM_SRV_CONF_OFFSET, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
141 offsetof(ngx_stream_proxy_srv_conf_t, downstream_limit_rate), |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
142 NULL }, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
143 |
6115 | 144 { ngx_string("proxy_upstream_buffer"), |
145 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
146 ngx_conf_set_size_slot, | |
147 NGX_STREAM_SRV_CONF_OFFSET, | |
148 offsetof(ngx_stream_proxy_srv_conf_t, upstream_buf_size), | |
149 NULL }, | |
150 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
151 { ngx_string("proxy_upstream_limit_rate"), |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
152 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
153 ngx_conf_set_size_slot, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
154 NGX_STREAM_SRV_CONF_OFFSET, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
155 offsetof(ngx_stream_proxy_srv_conf_t, upstream_limit_rate), |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
156 NULL }, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
157 |
6115 | 158 { ngx_string("proxy_next_upstream"), |
159 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, | |
160 ngx_conf_set_flag_slot, | |
161 NGX_STREAM_SRV_CONF_OFFSET, | |
162 offsetof(ngx_stream_proxy_srv_conf_t, next_upstream), | |
163 NULL }, | |
164 | |
165 { ngx_string("proxy_next_upstream_tries"), | |
166 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
167 ngx_conf_set_num_slot, | |
168 NGX_STREAM_SRV_CONF_OFFSET, | |
169 offsetof(ngx_stream_proxy_srv_conf_t, next_upstream_tries), | |
170 NULL }, | |
171 | |
172 { ngx_string("proxy_next_upstream_timeout"), | |
173 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
174 ngx_conf_set_msec_slot, | |
175 NGX_STREAM_SRV_CONF_OFFSET, | |
176 offsetof(ngx_stream_proxy_srv_conf_t, next_upstream_timeout), | |
177 NULL }, | |
178 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
179 { ngx_string("proxy_protocol"), |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
180 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
181 ngx_conf_set_flag_slot, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
182 NGX_STREAM_SRV_CONF_OFFSET, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
183 offsetof(ngx_stream_proxy_srv_conf_t, proxy_protocol), |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
184 NULL }, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
185 |
6115 | 186 #if (NGX_STREAM_SSL) |
187 | |
188 { ngx_string("proxy_ssl"), | |
189 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, | |
190 ngx_conf_set_flag_slot, | |
191 NGX_STREAM_SRV_CONF_OFFSET, | |
192 offsetof(ngx_stream_proxy_srv_conf_t, ssl_enable), | |
193 NULL }, | |
194 | |
195 { ngx_string("proxy_ssl_session_reuse"), | |
196 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, | |
197 ngx_conf_set_flag_slot, | |
198 NGX_STREAM_SRV_CONF_OFFSET, | |
199 offsetof(ngx_stream_proxy_srv_conf_t, ssl_session_reuse), | |
200 NULL }, | |
201 | |
202 { ngx_string("proxy_ssl_protocols"), | |
203 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_1MORE, | |
204 ngx_conf_set_bitmask_slot, | |
205 NGX_STREAM_SRV_CONF_OFFSET, | |
206 offsetof(ngx_stream_proxy_srv_conf_t, ssl_protocols), | |
207 &ngx_stream_proxy_ssl_protocols }, | |
208 | |
209 { ngx_string("proxy_ssl_ciphers"), | |
210 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
211 ngx_conf_set_str_slot, | |
212 NGX_STREAM_SRV_CONF_OFFSET, | |
213 offsetof(ngx_stream_proxy_srv_conf_t, ssl_ciphers), | |
214 NULL }, | |
215 | |
216 { ngx_string("proxy_ssl_name"), | |
217 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
218 ngx_conf_set_str_slot, | |
219 NGX_STREAM_SRV_CONF_OFFSET, | |
220 offsetof(ngx_stream_proxy_srv_conf_t, ssl_name), | |
221 NULL }, | |
222 | |
223 { ngx_string("proxy_ssl_server_name"), | |
224 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, | |
225 ngx_conf_set_flag_slot, | |
226 NGX_STREAM_SRV_CONF_OFFSET, | |
227 offsetof(ngx_stream_proxy_srv_conf_t, ssl_server_name), | |
228 NULL }, | |
229 | |
230 { ngx_string("proxy_ssl_verify"), | |
231 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_FLAG, | |
232 ngx_conf_set_flag_slot, | |
233 NGX_STREAM_SRV_CONF_OFFSET, | |
234 offsetof(ngx_stream_proxy_srv_conf_t, ssl_verify), | |
235 NULL }, | |
236 | |
237 { ngx_string("proxy_ssl_verify_depth"), | |
238 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
239 ngx_conf_set_num_slot, | |
240 NGX_STREAM_SRV_CONF_OFFSET, | |
241 offsetof(ngx_stream_proxy_srv_conf_t, ssl_verify_depth), | |
242 NULL }, | |
243 | |
244 { ngx_string("proxy_ssl_trusted_certificate"), | |
245 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
246 ngx_conf_set_str_slot, | |
247 NGX_STREAM_SRV_CONF_OFFSET, | |
248 offsetof(ngx_stream_proxy_srv_conf_t, ssl_trusted_certificate), | |
249 NULL }, | |
250 | |
251 { ngx_string("proxy_ssl_crl"), | |
252 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
253 ngx_conf_set_str_slot, | |
254 NGX_STREAM_SRV_CONF_OFFSET, | |
255 offsetof(ngx_stream_proxy_srv_conf_t, ssl_crl), | |
256 NULL }, | |
257 | |
258 { ngx_string("proxy_ssl_certificate"), | |
259 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
260 ngx_conf_set_str_slot, | |
261 NGX_STREAM_SRV_CONF_OFFSET, | |
262 offsetof(ngx_stream_proxy_srv_conf_t, ssl_certificate), | |
263 NULL }, | |
264 | |
265 { ngx_string("proxy_ssl_certificate_key"), | |
266 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
267 ngx_conf_set_str_slot, | |
268 NGX_STREAM_SRV_CONF_OFFSET, | |
269 offsetof(ngx_stream_proxy_srv_conf_t, ssl_certificate_key), | |
270 NULL }, | |
271 | |
272 { ngx_string("proxy_ssl_password_file"), | |
273 NGX_STREAM_MAIN_CONF|NGX_STREAM_SRV_CONF|NGX_CONF_TAKE1, | |
274 ngx_stream_proxy_ssl_password_file, | |
275 NGX_STREAM_SRV_CONF_OFFSET, | |
276 0, | |
277 NULL }, | |
278 | |
279 #endif | |
280 | |
281 ngx_null_command | |
282 }; | |
283 | |
284 | |
285 static ngx_stream_module_t ngx_stream_proxy_module_ctx = { | |
6174
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6157
diff
changeset
|
286 NULL, /* postconfiguration */ |
68c106e6fa0a
Stream: added postconfiguration method to stream modules.
Vladimir Homutov <vl@nginx.com>
parents:
6157
diff
changeset
|
287 |
6115 | 288 NULL, /* create main configuration */ |
289 NULL, /* init main configuration */ | |
290 | |
291 ngx_stream_proxy_create_srv_conf, /* create server configuration */ | |
292 ngx_stream_proxy_merge_srv_conf /* merge server configuration */ | |
293 }; | |
294 | |
295 | |
296 ngx_module_t ngx_stream_proxy_module = { | |
297 NGX_MODULE_V1, | |
298 &ngx_stream_proxy_module_ctx, /* module context */ | |
299 ngx_stream_proxy_commands, /* module directives */ | |
300 NGX_STREAM_MODULE, /* module type */ | |
301 NULL, /* init master */ | |
302 NULL, /* init module */ | |
303 NULL, /* init process */ | |
304 NULL, /* init thread */ | |
305 NULL, /* exit thread */ | |
306 NULL, /* exit process */ | |
307 NULL, /* exit master */ | |
308 NGX_MODULE_V1_PADDING | |
309 }; | |
310 | |
311 | |
312 static void | |
313 ngx_stream_proxy_handler(ngx_stream_session_t *s) | |
314 { | |
315 u_char *p; | |
316 ngx_connection_t *c; | |
317 ngx_stream_upstream_t *u; | |
318 ngx_stream_proxy_srv_conf_t *pscf; | |
319 ngx_stream_upstream_srv_conf_t *uscf; | |
320 | |
321 c = s->connection; | |
322 | |
323 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
324 | |
325 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, | |
326 "proxy connection handler"); | |
327 | |
328 u = ngx_pcalloc(c->pool, sizeof(ngx_stream_upstream_t)); | |
329 if (u == NULL) { | |
330 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
331 return; | |
332 } | |
333 | |
334 s->upstream = u; | |
335 | |
336 s->log_handler = ngx_stream_proxy_log_error; | |
337 | |
338 u->peer.log = c->log; | |
339 u->peer.log_error = NGX_ERROR_ERR; | |
340 | |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
341 u->peer.local = pscf->local; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
342 |
6115 | 343 uscf = pscf->upstream; |
344 | |
345 if (uscf->peer.init(s, uscf) != NGX_OK) { | |
346 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
347 return; | |
348 } | |
349 | |
350 u->peer.start_time = ngx_current_msec; | |
351 | |
352 if (pscf->next_upstream_tries | |
353 && u->peer.tries > pscf->next_upstream_tries) | |
354 { | |
355 u->peer.tries = pscf->next_upstream_tries; | |
356 } | |
357 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
358 u->proxy_protocol = pscf->proxy_protocol; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
359 u->start_sec = ngx_time(); |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
360 |
6115 | 361 p = ngx_pnalloc(c->pool, pscf->downstream_buf_size); |
362 if (p == NULL) { | |
363 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
364 return; | |
365 } | |
366 | |
367 u->downstream_buf.start = p; | |
368 u->downstream_buf.end = p + pscf->downstream_buf_size; | |
369 u->downstream_buf.pos = p; | |
370 u->downstream_buf.last = p; | |
371 | |
372 c->write->handler = ngx_stream_proxy_downstream_handler; | |
373 c->read->handler = ngx_stream_proxy_downstream_handler; | |
374 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
375 if (u->proxy_protocol |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
376 #if (NGX_STREAM_SSL) |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
377 && pscf->ssl == NULL |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
378 #endif |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
379 && pscf->downstream_buf_size >= NGX_PROXY_PROTOCOL_MAX_HEADER |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
380 ) |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
381 { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
382 /* optimization for a typical case */ |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
383 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
384 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
385 "stream proxy send PROXY protocol header"); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
386 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
387 p = ngx_proxy_protocol_write(c, u->downstream_buf.last, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
388 u->downstream_buf.end); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
389 if (p == NULL) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
390 ngx_stream_proxy_finalize(s, NGX_ERROR); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
391 return; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
392 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
393 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
394 u->downstream_buf.last = p; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
395 u->proxy_protocol = 0; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
396 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
397 |
6115 | 398 if (ngx_stream_proxy_process(s, 0, 0) != NGX_OK) { |
399 return; | |
400 } | |
401 | |
402 ngx_stream_proxy_connect(s); | |
403 } | |
404 | |
405 | |
406 static void | |
407 ngx_stream_proxy_connect(ngx_stream_session_t *s) | |
408 { | |
409 ngx_int_t rc; | |
410 ngx_connection_t *c, *pc; | |
411 ngx_stream_upstream_t *u; | |
412 ngx_stream_proxy_srv_conf_t *pscf; | |
413 | |
414 c = s->connection; | |
415 | |
416 c->log->action = "connecting to upstream"; | |
417 | |
418 u = s->upstream; | |
419 | |
420 rc = ngx_event_connect_peer(&u->peer); | |
421 | |
422 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "proxy connect: %i", rc); | |
423 | |
424 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
425 | |
426 if (rc == NGX_ERROR) { | |
427 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
428 return; | |
429 } | |
430 | |
431 if (rc == NGX_BUSY) { | |
432 ngx_log_error(NGX_LOG_ERR, c->log, 0, "no live upstreams"); | |
433 ngx_stream_proxy_finalize(s, NGX_DECLINED); | |
434 return; | |
435 } | |
436 | |
437 if (rc == NGX_DECLINED) { | |
438 ngx_stream_proxy_next_upstream(s); | |
439 return; | |
440 } | |
441 | |
442 /* rc == NGX_OK || rc == NGX_AGAIN || rc == NGX_DONE */ | |
443 | |
444 pc = u->peer.connection; | |
445 | |
446 pc->data = s; | |
447 pc->log = c->log; | |
448 pc->pool = c->pool; | |
449 pc->read->log = c->log; | |
450 pc->write->log = c->log; | |
451 | |
452 if (rc != NGX_AGAIN) { | |
453 ngx_stream_proxy_init_upstream(s); | |
454 return; | |
455 } | |
456 | |
457 pc->read->handler = ngx_stream_proxy_connect_handler; | |
458 pc->write->handler = ngx_stream_proxy_connect_handler; | |
459 | |
460 ngx_add_timer(pc->write, pscf->connect_timeout); | |
461 } | |
462 | |
463 | |
464 static void | |
465 ngx_stream_proxy_init_upstream(ngx_stream_session_t *s) | |
466 { | |
467 u_char *p; | |
468 ngx_connection_t *c, *pc; | |
469 ngx_log_handler_pt handler; | |
470 ngx_stream_upstream_t *u; | |
471 ngx_stream_proxy_srv_conf_t *pscf; | |
472 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
473 u = s->upstream; |
6115 | 474 |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
475 if (u->proxy_protocol) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
476 if (ngx_stream_proxy_send_proxy_protocol(s) != NGX_OK) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
477 return; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
478 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
479 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
480 u->proxy_protocol = 0; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
481 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
482 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
483 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); |
6115 | 484 |
485 pc = u->peer.connection; | |
486 | |
487 #if (NGX_STREAM_SSL) | |
488 if (pscf->ssl && pc->ssl == NULL) { | |
489 ngx_stream_proxy_ssl_init_connection(s); | |
490 return; | |
491 } | |
492 #endif | |
493 | |
494 c = s->connection; | |
495 | |
496 if (c->log->log_level >= NGX_LOG_INFO) { | |
497 ngx_str_t s; | |
498 u_char addr[NGX_SOCKADDR_STRLEN]; | |
499 | |
500 s.len = NGX_SOCKADDR_STRLEN; | |
501 s.data = addr; | |
502 | |
503 if (ngx_connection_local_sockaddr(pc, &s, 1) == NGX_OK) { | |
504 handler = c->log->handler; | |
505 c->log->handler = NULL; | |
506 | |
507 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxy %V connected to %V", | |
508 &s, u->peer.name); | |
509 | |
510 c->log->handler = handler; | |
511 } | |
512 } | |
513 | |
514 c->log->action = "proxying connection"; | |
515 | |
516 p = ngx_pnalloc(c->pool, pscf->upstream_buf_size); | |
517 if (p == NULL) { | |
518 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
519 return; | |
520 } | |
521 | |
522 u->upstream_buf.start = p; | |
523 u->upstream_buf.end = p + pscf->upstream_buf_size; | |
524 u->upstream_buf.pos = p; | |
525 u->upstream_buf.last = p; | |
526 | |
527 pc->read->handler = ngx_stream_proxy_upstream_handler; | |
528 pc->write->handler = ngx_stream_proxy_upstream_handler; | |
529 | |
530 if (ngx_stream_proxy_process(s, 1, 0) != NGX_OK) { | |
531 return; | |
532 } | |
533 | |
534 ngx_stream_proxy_process(s, 0, 1); | |
535 } | |
536 | |
537 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
538 static ngx_int_t |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
539 ngx_stream_proxy_send_proxy_protocol(ngx_stream_session_t *s) |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
540 { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
541 u_char *p; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
542 ssize_t n, size; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
543 ngx_connection_t *c, *pc; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
544 ngx_stream_upstream_t *u; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
545 ngx_stream_proxy_srv_conf_t *pscf; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
546 u_char buf[NGX_PROXY_PROTOCOL_MAX_HEADER]; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
547 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
548 c = s->connection; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
549 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
550 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
551 "stream proxy send PROXY protocol header"); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
552 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
553 p = ngx_proxy_protocol_write(c, buf, buf + NGX_PROXY_PROTOCOL_MAX_HEADER); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
554 if (p == NULL) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
555 ngx_stream_proxy_finalize(s, NGX_ERROR); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
556 return NGX_ERROR; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
557 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
558 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
559 u = s->upstream; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
560 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
561 pc = u->peer.connection; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
562 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
563 size = p - buf; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
564 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
565 n = pc->send(pc, buf, size); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
566 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
567 if (n == NGX_AGAIN) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
568 if (ngx_handle_write_event(pc->write, 0) != NGX_OK) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
569 ngx_stream_proxy_finalize(s, NGX_ERROR); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
570 return NGX_ERROR; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
571 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
572 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
573 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
574 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
575 ngx_add_timer(pc->write, pscf->timeout); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
576 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
577 pc->write->handler = ngx_stream_proxy_connect_handler; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
578 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
579 return NGX_AGAIN; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
580 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
581 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
582 if (n == NGX_ERROR) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
583 ngx_stream_proxy_finalize(s, NGX_DECLINED); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
584 return NGX_ERROR; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
585 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
586 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
587 if (n != size) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
588 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
589 /* |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
590 * PROXY protocol specification: |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
591 * The sender must always ensure that the header |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
592 * is sent at once, so that the transport layer |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
593 * maintains atomicity along the path to the receiver. |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
594 */ |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
595 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
596 ngx_log_error(NGX_LOG_ERR, c->log, 0, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
597 "could not send PROXY protocol header at once"); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
598 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
599 ngx_stream_proxy_finalize(s, NGX_DECLINED); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
600 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
601 return NGX_ERROR; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
602 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
603 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
604 return NGX_OK; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
605 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
606 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
607 |
6115 | 608 #if (NGX_STREAM_SSL) |
609 | |
610 static char * | |
611 ngx_stream_proxy_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, | |
612 void *conf) | |
613 { | |
614 ngx_stream_proxy_srv_conf_t *pscf = conf; | |
615 | |
616 ngx_str_t *value; | |
617 | |
618 if (pscf->ssl_passwords != NGX_CONF_UNSET_PTR) { | |
619 return "is duplicate"; | |
620 } | |
621 | |
622 value = cf->args->elts; | |
623 | |
624 pscf->ssl_passwords = ngx_ssl_read_password_file(cf, &value[1]); | |
625 | |
626 if (pscf->ssl_passwords == NULL) { | |
627 return NGX_CONF_ERROR; | |
628 } | |
629 | |
630 return NGX_CONF_OK; | |
631 } | |
632 | |
633 | |
634 static void | |
635 ngx_stream_proxy_ssl_init_connection(ngx_stream_session_t *s) | |
636 { | |
637 ngx_int_t rc; | |
638 ngx_connection_t *pc; | |
639 ngx_stream_upstream_t *u; | |
640 ngx_stream_proxy_srv_conf_t *pscf; | |
641 | |
642 u = s->upstream; | |
643 | |
644 pc = u->peer.connection; | |
645 | |
646 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
647 | |
648 if (ngx_ssl_create_connection(pscf->ssl, pc, NGX_SSL_BUFFER|NGX_SSL_CLIENT) | |
649 != NGX_OK) | |
650 { | |
651 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
652 return; | |
653 } | |
654 | |
655 if (pscf->ssl_server_name || pscf->ssl_verify) { | |
656 if (ngx_stream_proxy_ssl_name(s) != NGX_OK) { | |
657 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
658 return; | |
659 } | |
660 } | |
661 | |
662 if (pscf->ssl_session_reuse) { | |
663 if (u->peer.set_session(&u->peer, u->peer.data) != NGX_OK) { | |
664 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
665 return; | |
666 } | |
667 } | |
668 | |
669 s->connection->log->action = "SSL handshaking to upstream"; | |
670 | |
671 rc = ngx_ssl_handshake(pc); | |
672 | |
673 if (rc == NGX_AGAIN) { | |
674 | |
675 if (!pc->write->timer_set) { | |
676 ngx_add_timer(pc->write, pscf->connect_timeout); | |
677 } | |
678 | |
679 pc->ssl->handler = ngx_stream_proxy_ssl_handshake; | |
680 return; | |
681 } | |
682 | |
683 ngx_stream_proxy_ssl_handshake(pc); | |
684 } | |
685 | |
686 | |
687 static void | |
688 ngx_stream_proxy_ssl_handshake(ngx_connection_t *pc) | |
689 { | |
690 long rc; | |
691 ngx_stream_session_t *s; | |
692 ngx_stream_upstream_t *u; | |
693 ngx_stream_proxy_srv_conf_t *pscf; | |
694 | |
695 s = pc->data; | |
696 | |
697 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
698 | |
699 if (pc->ssl->handshaked) { | |
700 | |
701 if (pscf->ssl_verify) { | |
702 rc = SSL_get_verify_result(pc->ssl->connection); | |
703 | |
704 if (rc != X509_V_OK) { | |
705 ngx_log_error(NGX_LOG_ERR, pc->log, 0, | |
706 "upstream SSL certificate verify error: (%l:%s)", | |
707 rc, X509_verify_cert_error_string(rc)); | |
708 goto failed; | |
709 } | |
710 | |
711 u = s->upstream; | |
712 | |
713 if (ngx_ssl_check_host(pc, &u->ssl_name) != NGX_OK) { | |
714 ngx_log_error(NGX_LOG_ERR, pc->log, 0, | |
715 "upstream SSL certificate does not match \"%V\"", | |
716 &u->ssl_name); | |
717 goto failed; | |
718 } | |
719 } | |
720 | |
721 if (pscf->ssl_session_reuse) { | |
722 u = s->upstream; | |
723 u->peer.save_session(&u->peer, u->peer.data); | |
724 } | |
725 | |
726 ngx_stream_proxy_init_upstream(s); | |
727 | |
728 return; | |
729 } | |
730 | |
731 failed: | |
732 | |
733 ngx_stream_proxy_next_upstream(s); | |
734 } | |
735 | |
736 | |
737 static ngx_int_t | |
738 ngx_stream_proxy_ssl_name(ngx_stream_session_t *s) | |
739 { | |
740 u_char *p, *last; | |
741 ngx_str_t name; | |
742 ngx_stream_upstream_t *u; | |
743 ngx_stream_proxy_srv_conf_t *pscf; | |
744 | |
745 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
746 | |
747 u = s->upstream; | |
748 | |
749 name = pscf->ssl_name; | |
750 | |
751 if (name.len == 0) { | |
752 name = pscf->upstream->host; | |
753 } | |
754 | |
755 if (name.len == 0) { | |
756 goto done; | |
757 } | |
758 | |
759 /* | |
760 * ssl name here may contain port, strip it for compatibility | |
761 * with the http module | |
762 */ | |
763 | |
764 p = name.data; | |
765 last = name.data + name.len; | |
766 | |
767 if (*p == '[') { | |
768 p = ngx_strlchr(p, last, ']'); | |
769 | |
770 if (p == NULL) { | |
771 p = name.data; | |
772 } | |
773 } | |
774 | |
775 p = ngx_strlchr(p, last, ':'); | |
776 | |
777 if (p != NULL) { | |
778 name.len = p - name.data; | |
779 } | |
780 | |
781 if (!pscf->ssl_server_name) { | |
782 goto done; | |
783 } | |
784 | |
785 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME | |
786 | |
787 /* as per RFC 6066, literal IPv4 and IPv6 addresses are not permitted */ | |
788 | |
789 if (name.len == 0 || *name.data == '[') { | |
790 goto done; | |
791 } | |
792 | |
793 if (ngx_inet_addr(name.data, name.len) != INADDR_NONE) { | |
794 goto done; | |
795 } | |
796 | |
797 /* | |
798 * SSL_set_tlsext_host_name() needs a null-terminated string, | |
799 * hence we explicitly null-terminate name here | |
800 */ | |
801 | |
802 p = ngx_pnalloc(s->connection->pool, name.len + 1); | |
803 if (p == NULL) { | |
804 return NGX_ERROR; | |
805 } | |
806 | |
807 (void) ngx_cpystrn(p, name.data, name.len + 1); | |
808 | |
809 name.data = p; | |
810 | |
811 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
812 "upstream SSL server name: \"%s\"", name.data); | |
813 | |
814 if (SSL_set_tlsext_host_name(u->peer.connection->ssl->connection, name.data) | |
815 == 0) | |
816 { | |
817 ngx_ssl_error(NGX_LOG_ERR, s->connection->log, 0, | |
818 "SSL_set_tlsext_host_name(\"%s\") failed", name.data); | |
819 return NGX_ERROR; | |
820 } | |
821 | |
822 #endif | |
823 | |
824 done: | |
825 | |
826 u->ssl_name = name; | |
827 | |
828 return NGX_OK; | |
829 } | |
830 | |
831 #endif | |
832 | |
833 | |
834 static void | |
835 ngx_stream_proxy_downstream_handler(ngx_event_t *ev) | |
836 { | |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
837 ngx_stream_proxy_process_connection(ev, ev->write); |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
838 } |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
839 |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
840 |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
841 static void |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
842 ngx_stream_proxy_upstream_handler(ngx_event_t *ev) |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
843 { |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
844 ngx_stream_proxy_process_connection(ev, !ev->write); |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
845 } |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
846 |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
847 |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
848 static void |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
849 ngx_stream_proxy_process_connection(ngx_event_t *ev, ngx_uint_t from_upstream) |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
850 { |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
851 ngx_connection_t *c, *pc; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
852 ngx_stream_session_t *s; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
853 ngx_stream_upstream_t *u; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
854 ngx_stream_proxy_srv_conf_t *pscf; |
6115 | 855 |
856 c = ev->data; | |
857 s = c->data; | |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
858 u = s->upstream; |
6115 | 859 |
860 if (ev->timedout) { | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
861 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
862 if (ev->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
863 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
864 ev->timedout = 0; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
865 ev->delayed = 0; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
866 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
867 if (!ev->ready) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
868 if (ngx_handle_read_event(ev, 0) != NGX_OK) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
869 ngx_stream_proxy_finalize(s, NGX_ERROR); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
870 return; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
871 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
872 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
873 if (u->upstream_buf.start) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
874 pc = u->peer.connection; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
875 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
876 if (!c->read->delayed && !pc->read->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
877 pscf = ngx_stream_get_module_srv_conf(s, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
878 ngx_stream_proxy_module); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
879 ngx_add_timer(c->write, pscf->timeout); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
880 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
881 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
882 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
883 return; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
884 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
885 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
886 } else { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
887 ngx_connection_error(c, NGX_ETIMEDOUT, "connection timed out"); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
888 ngx_stream_proxy_finalize(s, NGX_DECLINED); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
889 return; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
890 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
891 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
892 } else if (ev->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
893 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
894 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
895 "stream connection delayed"); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
896 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
897 if (ngx_handle_read_event(ev, 0) != NGX_OK) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
898 ngx_stream_proxy_finalize(s, NGX_ERROR); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
899 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
900 |
6115 | 901 return; |
902 } | |
903 | |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
904 if (from_upstream && u->upstream_buf.start == NULL) { |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
905 return; |
6115 | 906 } |
907 | |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
908 ngx_stream_proxy_process(s, from_upstream, ev->write); |
6115 | 909 } |
910 | |
911 | |
912 static void | |
913 ngx_stream_proxy_connect_handler(ngx_event_t *ev) | |
914 { | |
915 ngx_connection_t *c; | |
916 ngx_stream_session_t *s; | |
917 | |
918 c = ev->data; | |
919 s = c->data; | |
920 | |
921 if (ev->timedout) { | |
922 ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT, "upstream timed out"); | |
923 ngx_stream_proxy_next_upstream(s); | |
924 return; | |
925 } | |
926 | |
927 ngx_del_timer(c->write); | |
928 | |
929 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, | |
930 "stream proxy connect upstream"); | |
931 | |
932 if (ngx_stream_proxy_test_connect(c) != NGX_OK) { | |
933 ngx_stream_proxy_next_upstream(s); | |
934 return; | |
935 } | |
936 | |
937 ngx_stream_proxy_init_upstream(s); | |
938 } | |
939 | |
940 | |
941 static ngx_int_t | |
942 ngx_stream_proxy_test_connect(ngx_connection_t *c) | |
943 { | |
944 int err; | |
945 socklen_t len; | |
946 | |
947 #if (NGX_HAVE_KQUEUE) | |
948 | |
949 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { | |
950 err = c->write->kq_errno ? c->write->kq_errno : c->read->kq_errno; | |
951 | |
952 if (err) { | |
953 (void) ngx_connection_error(c, err, | |
954 "kevent() reported that connect() failed"); | |
955 return NGX_ERROR; | |
956 } | |
957 | |
958 } else | |
959 #endif | |
960 { | |
961 err = 0; | |
962 len = sizeof(int); | |
963 | |
964 /* | |
965 * BSDs and Linux return 0 and set a pending error in err | |
966 * Solaris returns -1 and sets errno | |
967 */ | |
968 | |
969 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len) | |
970 == -1) | |
971 { | |
972 err = ngx_socket_errno; | |
973 } | |
974 | |
975 if (err) { | |
976 (void) ngx_connection_error(c, err, "connect() failed"); | |
977 return NGX_ERROR; | |
978 } | |
979 } | |
980 | |
981 return NGX_OK; | |
982 } | |
983 | |
984 | |
985 static ngx_int_t | |
986 ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream, | |
987 ngx_uint_t do_write) | |
988 { | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
989 off_t *received, limit; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
990 size_t size, limit_rate; |
6115 | 991 ssize_t n; |
992 ngx_buf_t *b; | |
6124
f1f222db290b
Stream: prevent repeated event notifications after eof.
Roman Arutyunyan <arut@nginx.com>
parents:
6115
diff
changeset
|
993 ngx_uint_t flags; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
994 ngx_msec_t delay; |
6115 | 995 ngx_connection_t *c, *pc, *src, *dst; |
996 ngx_log_handler_pt handler; | |
997 ngx_stream_upstream_t *u; | |
998 ngx_stream_proxy_srv_conf_t *pscf; | |
999 | |
1000 u = s->upstream; | |
1001 | |
1002 c = s->connection; | |
1003 pc = u->upstream_buf.start ? u->peer.connection : NULL; | |
1004 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1005 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1006 |
6115 | 1007 if (from_upstream) { |
1008 src = pc; | |
1009 dst = c; | |
1010 b = &u->upstream_buf; | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1011 limit_rate = pscf->upstream_limit_rate; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1012 received = &u->received; |
6115 | 1013 |
1014 } else { | |
1015 src = c; | |
1016 dst = pc; | |
1017 b = &u->downstream_buf; | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1018 limit_rate = pscf->downstream_limit_rate; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1019 received = &s->received; |
6115 | 1020 } |
1021 | |
1022 for ( ;; ) { | |
1023 | |
1024 if (do_write) { | |
1025 | |
1026 size = b->last - b->pos; | |
1027 | |
1028 if (size && dst && dst->write->ready) { | |
1029 | |
1030 n = dst->send(dst, b->pos, size); | |
1031 | |
1032 if (n == NGX_ERROR) { | |
1033 ngx_stream_proxy_finalize(s, NGX_DECLINED); | |
1034 return NGX_ERROR; | |
1035 } | |
1036 | |
1037 if (n > 0) { | |
1038 b->pos += n; | |
1039 | |
1040 if (b->pos == b->last) { | |
1041 b->pos = b->start; | |
1042 b->last = b->start; | |
1043 } | |
1044 } | |
1045 } | |
1046 } | |
1047 | |
1048 size = b->end - b->last; | |
1049 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1050 if (size && src->read->ready && !src->read->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1051 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1052 if (limit_rate) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1053 limit = (off_t) limit_rate * (ngx_time() - u->start_sec + 1) |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1054 - *received; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1055 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1056 if (limit <= 0) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1057 src->read->delayed = 1; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1058 delay = (ngx_msec_t) (- limit * 1000 / limit_rate + 1); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1059 ngx_add_timer(src->read, delay); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1060 break; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1061 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1062 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1063 if (size > (size_t) limit) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1064 size = limit; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1065 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1066 } |
6115 | 1067 |
1068 n = src->recv(src, b->last, size); | |
1069 | |
1070 if (n == NGX_AGAIN || n == 0) { | |
1071 break; | |
1072 } | |
1073 | |
1074 if (n > 0) { | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1075 if (limit_rate) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1076 delay = (ngx_msec_t) (n * 1000 / limit_rate); |
6115 | 1077 |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1078 if (delay > 0) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1079 src->read->delayed = 1; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1080 ngx_add_timer(src->read, delay); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1081 } |
6115 | 1082 } |
1083 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1084 *received += n; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1085 b->last += n; |
6115 | 1086 do_write = 1; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1087 |
6115 | 1088 continue; |
1089 } | |
1090 | |
1091 if (n == NGX_ERROR) { | |
1092 src->read->eof = 1; | |
1093 } | |
1094 } | |
1095 | |
1096 break; | |
1097 } | |
1098 | |
1099 if (src->read->eof && (b->pos == b->last || (dst && dst->read->eof))) { | |
1100 handler = c->log->handler; | |
1101 c->log->handler = NULL; | |
1102 | |
1103 ngx_log_error(NGX_LOG_INFO, c->log, 0, | |
1104 "%s disconnected" | |
1105 ", bytes from/to client:%O/%O" | |
1106 ", bytes from/to upstream:%O/%O", | |
1107 from_upstream ? "upstream" : "client", | |
1108 s->received, c->sent, u->received, pc ? pc->sent : 0); | |
1109 | |
1110 c->log->handler = handler; | |
1111 | |
1112 ngx_stream_proxy_finalize(s, NGX_OK); | |
1113 return NGX_DONE; | |
1114 } | |
1115 | |
6124
f1f222db290b
Stream: prevent repeated event notifications after eof.
Roman Arutyunyan <arut@nginx.com>
parents:
6115
diff
changeset
|
1116 flags = src->read->eof ? NGX_CLOSE_EVENT : 0; |
f1f222db290b
Stream: prevent repeated event notifications after eof.
Roman Arutyunyan <arut@nginx.com>
parents:
6115
diff
changeset
|
1117 |
f1f222db290b
Stream: prevent repeated event notifications after eof.
Roman Arutyunyan <arut@nginx.com>
parents:
6115
diff
changeset
|
1118 if (ngx_handle_read_event(src->read, flags) != NGX_OK) { |
6115 | 1119 ngx_stream_proxy_finalize(s, NGX_ERROR); |
1120 return NGX_ERROR; | |
1121 } | |
1122 | |
1123 if (dst) { | |
1124 if (ngx_handle_write_event(dst->write, 0) != NGX_OK) { | |
1125 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
1126 return NGX_ERROR; | |
1127 } | |
1128 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1129 if (!c->read->delayed && !pc->read->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1130 ngx_add_timer(c->write, pscf->timeout); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1131 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1132 } else if (c->write->timer_set) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1133 ngx_del_timer(c->write); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1134 } |
6115 | 1135 } |
1136 | |
1137 return NGX_OK; | |
1138 } | |
1139 | |
1140 | |
1141 static void | |
1142 ngx_stream_proxy_next_upstream(ngx_stream_session_t *s) | |
1143 { | |
1144 ngx_msec_t timeout; | |
1145 ngx_connection_t *pc; | |
1146 ngx_stream_upstream_t *u; | |
1147 ngx_stream_proxy_srv_conf_t *pscf; | |
1148 | |
1149 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
1150 "stream proxy next upstream"); | |
1151 | |
1152 u = s->upstream; | |
1153 | |
1154 if (u->peer.sockaddr) { | |
1155 u->peer.free(&u->peer, u->peer.data, NGX_PEER_FAILED); | |
1156 u->peer.sockaddr = NULL; | |
1157 } | |
1158 | |
1159 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
1160 | |
1161 timeout = pscf->next_upstream_timeout; | |
1162 | |
1163 if (u->peer.tries == 0 | |
1164 || !pscf->next_upstream | |
1165 || (timeout && ngx_current_msec - u->peer.start_time >= timeout)) | |
1166 { | |
1167 ngx_stream_proxy_finalize(s, NGX_DECLINED); | |
1168 return; | |
1169 } | |
1170 | |
1171 pc = u->peer.connection; | |
1172 | |
1173 if (pc) { | |
1174 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
1175 "close proxy upstream connection: %d", pc->fd); | |
1176 | |
1177 #if (NGX_STREAM_SSL) | |
1178 if (pc->ssl) { | |
1179 pc->ssl->no_wait_shutdown = 1; | |
1180 pc->ssl->no_send_shutdown = 1; | |
1181 | |
1182 (void) ngx_ssl_shutdown(pc); | |
1183 } | |
1184 #endif | |
1185 | |
1186 ngx_close_connection(pc); | |
1187 u->peer.connection = NULL; | |
1188 } | |
1189 | |
1190 ngx_stream_proxy_connect(s); | |
1191 } | |
1192 | |
1193 | |
1194 static void | |
1195 ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_int_t rc) | |
1196 { | |
1197 ngx_connection_t *pc; | |
1198 ngx_stream_upstream_t *u; | |
1199 | |
1200 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
1201 "finalize stream proxy: %i", rc); | |
1202 | |
1203 u = s->upstream; | |
1204 | |
1205 if (u == NULL) { | |
1206 goto noupstream; | |
1207 } | |
1208 | |
1209 if (u->peer.free && u->peer.sockaddr) { | |
1210 u->peer.free(&u->peer, u->peer.data, 0); | |
1211 u->peer.sockaddr = NULL; | |
1212 } | |
1213 | |
1214 pc = u->peer.connection; | |
1215 | |
1216 if (pc) { | |
1217 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
1218 "close stream proxy upstream connection: %d", pc->fd); | |
1219 | |
1220 #if (NGX_STREAM_SSL) | |
1221 if (pc->ssl) { | |
1222 pc->ssl->no_wait_shutdown = 1; | |
1223 (void) ngx_ssl_shutdown(pc); | |
1224 } | |
1225 #endif | |
1226 | |
1227 ngx_close_connection(pc); | |
1228 u->peer.connection = NULL; | |
1229 } | |
1230 | |
1231 noupstream: | |
1232 | |
1233 ngx_stream_close_connection(s->connection); | |
1234 } | |
1235 | |
1236 | |
1237 static u_char * | |
1238 ngx_stream_proxy_log_error(ngx_log_t *log, u_char *buf, size_t len) | |
1239 { | |
1240 u_char *p; | |
1241 ngx_connection_t *pc; | |
1242 ngx_stream_session_t *s; | |
1243 ngx_stream_upstream_t *u; | |
1244 | |
1245 s = log->data; | |
1246 | |
1247 u = s->upstream; | |
1248 | |
1249 p = buf; | |
1250 | |
1251 if (u->peer.name) { | |
1252 p = ngx_snprintf(p, len, ", upstream: \"%V\"", u->peer.name); | |
1253 len -= p - buf; | |
1254 } | |
1255 | |
1256 pc = u->peer.connection; | |
1257 | |
1258 p = ngx_snprintf(p, len, | |
1259 ", bytes from/to client:%O/%O" | |
1260 ", bytes from/to upstream:%O/%O", | |
1261 s->received, s->connection->sent, | |
1262 u->received, pc ? pc->sent : 0); | |
1263 | |
1264 return p; | |
1265 } | |
1266 | |
1267 | |
1268 static void * | |
1269 ngx_stream_proxy_create_srv_conf(ngx_conf_t *cf) | |
1270 { | |
1271 ngx_stream_proxy_srv_conf_t *conf; | |
1272 | |
1273 conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_proxy_srv_conf_t)); | |
1274 if (conf == NULL) { | |
1275 return NULL; | |
1276 } | |
1277 | |
1278 /* | |
1279 * set by ngx_pcalloc(): | |
1280 * | |
1281 * conf->ssl_protocols = 0; | |
1282 * conf->ssl_ciphers = { 0, NULL }; | |
1283 * conf->ssl_name = { 0, NULL }; | |
1284 * conf->ssl_trusted_certificate = { 0, NULL }; | |
1285 * conf->ssl_crl = { 0, NULL }; | |
1286 * conf->ssl_certificate = { 0, NULL }; | |
1287 * conf->ssl_certificate_key = { 0, NULL }; | |
1288 * | |
1289 * conf->ssl = NULL; | |
1290 * conf->upstream = NULL; | |
1291 */ | |
1292 | |
1293 conf->connect_timeout = NGX_CONF_UNSET_MSEC; | |
1294 conf->timeout = NGX_CONF_UNSET_MSEC; | |
1295 conf->next_upstream_timeout = NGX_CONF_UNSET_MSEC; | |
1296 conf->downstream_buf_size = NGX_CONF_UNSET_SIZE; | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1297 conf->downstream_limit_rate = NGX_CONF_UNSET_SIZE; |
6115 | 1298 conf->upstream_buf_size = NGX_CONF_UNSET_SIZE; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1299 conf->upstream_limit_rate = NGX_CONF_UNSET_SIZE; |
6115 | 1300 conf->next_upstream_tries = NGX_CONF_UNSET_UINT; |
1301 conf->next_upstream = NGX_CONF_UNSET; | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
1302 conf->proxy_protocol = NGX_CONF_UNSET; |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1303 conf->local = NGX_CONF_UNSET_PTR; |
6115 | 1304 |
1305 #if (NGX_STREAM_SSL) | |
1306 conf->ssl_enable = NGX_CONF_UNSET; | |
1307 conf->ssl_session_reuse = NGX_CONF_UNSET; | |
1308 conf->ssl_server_name = NGX_CONF_UNSET; | |
1309 conf->ssl_verify = NGX_CONF_UNSET; | |
1310 conf->ssl_verify_depth = NGX_CONF_UNSET_UINT; | |
1311 conf->ssl_passwords = NGX_CONF_UNSET_PTR; | |
1312 #endif | |
1313 | |
1314 return conf; | |
1315 } | |
1316 | |
1317 | |
1318 static char * | |
1319 ngx_stream_proxy_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) | |
1320 { | |
1321 ngx_stream_proxy_srv_conf_t *prev = parent; | |
1322 ngx_stream_proxy_srv_conf_t *conf = child; | |
1323 | |
1324 ngx_conf_merge_msec_value(conf->connect_timeout, | |
1325 prev->connect_timeout, 60000); | |
1326 | |
1327 ngx_conf_merge_msec_value(conf->timeout, | |
1328 prev->timeout, 10 * 60000); | |
1329 | |
1330 ngx_conf_merge_msec_value(conf->next_upstream_timeout, | |
1331 prev->next_upstream_timeout, 0); | |
1332 | |
1333 ngx_conf_merge_size_value(conf->downstream_buf_size, | |
1334 prev->downstream_buf_size, 16384); | |
1335 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1336 ngx_conf_merge_size_value(conf->downstream_limit_rate, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1337 prev->downstream_limit_rate, 0); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1338 |
6115 | 1339 ngx_conf_merge_size_value(conf->upstream_buf_size, |
1340 prev->upstream_buf_size, 16384); | |
1341 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1342 ngx_conf_merge_size_value(conf->upstream_limit_rate, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1343 prev->upstream_limit_rate, 0); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1344 |
6115 | 1345 ngx_conf_merge_uint_value(conf->next_upstream_tries, |
1346 prev->next_upstream_tries, 0); | |
1347 | |
1348 ngx_conf_merge_value(conf->next_upstream, prev->next_upstream, 1); | |
1349 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
1350 ngx_conf_merge_value(conf->proxy_protocol, prev->proxy_protocol, 0); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
1351 |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1352 ngx_conf_merge_ptr_value(conf->local, prev->local, NULL); |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1353 |
6115 | 1354 #if (NGX_STREAM_SSL) |
1355 | |
1356 ngx_conf_merge_value(conf->ssl_enable, prev->ssl_enable, 0); | |
1357 | |
1358 ngx_conf_merge_value(conf->ssl_session_reuse, | |
1359 prev->ssl_session_reuse, 1); | |
1360 | |
1361 ngx_conf_merge_bitmask_value(conf->ssl_protocols, prev->ssl_protocols, | |
6157
b2899e7d0ef8
Disabled SSLv3 by default (ticket #653).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6124
diff
changeset
|
1362 (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1 |
b2899e7d0ef8
Disabled SSLv3 by default (ticket #653).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6124
diff
changeset
|
1363 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2)); |
6115 | 1364 |
1365 ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers, "DEFAULT"); | |
1366 | |
1367 ngx_conf_merge_str_value(conf->ssl_name, prev->ssl_name, ""); | |
1368 | |
1369 ngx_conf_merge_value(conf->ssl_server_name, prev->ssl_server_name, 0); | |
1370 | |
1371 ngx_conf_merge_value(conf->ssl_verify, prev->ssl_verify, 0); | |
1372 | |
1373 ngx_conf_merge_uint_value(conf->ssl_verify_depth, | |
1374 prev->ssl_verify_depth, 1); | |
1375 | |
1376 ngx_conf_merge_str_value(conf->ssl_trusted_certificate, | |
1377 prev->ssl_trusted_certificate, ""); | |
1378 | |
1379 ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, ""); | |
1380 | |
1381 ngx_conf_merge_str_value(conf->ssl_certificate, | |
1382 prev->ssl_certificate, ""); | |
1383 | |
1384 ngx_conf_merge_str_value(conf->ssl_certificate_key, | |
1385 prev->ssl_certificate_key, ""); | |
1386 | |
1387 ngx_conf_merge_ptr_value(conf->ssl_passwords, prev->ssl_passwords, NULL); | |
1388 | |
1389 if (conf->ssl_enable && ngx_stream_proxy_set_ssl(cf, conf) != NGX_OK) { | |
1390 return NGX_CONF_ERROR; | |
1391 } | |
1392 | |
1393 #endif | |
1394 | |
1395 return NGX_CONF_OK; | |
1396 } | |
1397 | |
1398 | |
1399 #if (NGX_STREAM_SSL) | |
1400 | |
1401 static ngx_int_t | |
1402 ngx_stream_proxy_set_ssl(ngx_conf_t *cf, ngx_stream_proxy_srv_conf_t *pscf) | |
1403 { | |
1404 ngx_pool_cleanup_t *cln; | |
1405 | |
1406 pscf->ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t)); | |
1407 if (pscf->ssl == NULL) { | |
1408 return NGX_ERROR; | |
1409 } | |
1410 | |
1411 pscf->ssl->log = cf->log; | |
1412 | |
1413 if (ngx_ssl_create(pscf->ssl, pscf->ssl_protocols, NULL) != NGX_OK) { | |
1414 return NGX_ERROR; | |
1415 } | |
1416 | |
1417 cln = ngx_pool_cleanup_add(cf->pool, 0); | |
1418 if (cln == NULL) { | |
1419 return NGX_ERROR; | |
1420 } | |
1421 | |
1422 cln->handler = ngx_ssl_cleanup_ctx; | |
1423 cln->data = pscf->ssl; | |
1424 | |
1425 if (pscf->ssl_certificate.len) { | |
1426 | |
1427 if (pscf->ssl_certificate_key.len == 0) { | |
1428 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
1429 "no \"proxy_ssl_certificate_key\" is defined " | |
1430 "for certificate \"%V\"", &pscf->ssl_certificate); | |
1431 return NGX_ERROR; | |
1432 } | |
1433 | |
1434 if (ngx_ssl_certificate(cf, pscf->ssl, &pscf->ssl_certificate, | |
1435 &pscf->ssl_certificate_key, pscf->ssl_passwords) | |
1436 != NGX_OK) | |
1437 { | |
1438 return NGX_ERROR; | |
1439 } | |
1440 } | |
1441 | |
1442 if (SSL_CTX_set_cipher_list(pscf->ssl->ctx, | |
1443 (const char *) pscf->ssl_ciphers.data) | |
1444 == 0) | |
1445 { | |
1446 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0, | |
1447 "SSL_CTX_set_cipher_list(\"%V\") failed", | |
1448 &pscf->ssl_ciphers); | |
1449 return NGX_ERROR; | |
1450 } | |
1451 | |
1452 if (pscf->ssl_verify) { | |
1453 if (pscf->ssl_trusted_certificate.len == 0) { | |
1454 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
1455 "no proxy_ssl_trusted_certificate for proxy_ssl_verify"); | |
1456 return NGX_ERROR; | |
1457 } | |
1458 | |
1459 if (ngx_ssl_trusted_certificate(cf, pscf->ssl, | |
1460 &pscf->ssl_trusted_certificate, | |
1461 pscf->ssl_verify_depth) | |
1462 != NGX_OK) | |
1463 { | |
1464 return NGX_ERROR; | |
1465 } | |
1466 | |
1467 if (ngx_ssl_crl(cf, pscf->ssl, &pscf->ssl_crl) != NGX_OK) { | |
1468 return NGX_ERROR; | |
1469 } | |
1470 } | |
1471 | |
1472 return NGX_OK; | |
1473 } | |
1474 | |
1475 #endif | |
1476 | |
1477 | |
1478 static char * | |
1479 ngx_stream_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1480 { | |
1481 ngx_stream_proxy_srv_conf_t *pscf = conf; | |
1482 | |
1483 ngx_url_t u; | |
1484 ngx_str_t *value, *url; | |
1485 ngx_stream_core_srv_conf_t *cscf; | |
1486 | |
1487 if (pscf->upstream) { | |
1488 return "is duplicate"; | |
1489 } | |
1490 | |
1491 cscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_core_module); | |
1492 | |
1493 cscf->handler = ngx_stream_proxy_handler; | |
1494 | |
1495 value = cf->args->elts; | |
1496 | |
1497 url = &value[1]; | |
1498 | |
1499 ngx_memzero(&u, sizeof(ngx_url_t)); | |
1500 | |
1501 u.url = *url; | |
1502 u.no_resolve = 1; | |
1503 | |
1504 pscf->upstream = ngx_stream_upstream_add(cf, &u, 0); | |
1505 if (pscf->upstream == NULL) { | |
1506 return NGX_CONF_ERROR; | |
1507 } | |
1508 | |
1509 return NGX_CONF_OK; | |
1510 } | |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1511 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1512 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1513 static char * |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1514 ngx_stream_proxy_bind(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1515 { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1516 ngx_stream_proxy_srv_conf_t *pscf = conf; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1517 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1518 ngx_int_t rc; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1519 ngx_str_t *value; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1520 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1521 if (pscf->local != NGX_CONF_UNSET_PTR) { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1522 return "is duplicate"; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1523 } |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1524 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1525 value = cf->args->elts; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1526 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1527 if (ngx_strcmp(value[1].data, "off") == 0) { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1528 pscf->local = NULL; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1529 return NGX_CONF_OK; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1530 } |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1531 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1532 pscf->local = ngx_palloc(cf->pool, sizeof(ngx_addr_t)); |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1533 if (pscf->local == NULL) { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1534 return NGX_CONF_ERROR; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1535 } |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1536 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1537 rc = ngx_parse_addr(cf->pool, pscf->local, value[1].data, value[1].len); |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1538 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1539 switch (rc) { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1540 case NGX_OK: |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1541 pscf->local->name = value[1]; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1542 return NGX_CONF_OK; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1543 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1544 case NGX_DECLINED: |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1545 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1546 "invalid address \"%V\"", &value[1]); |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1547 /* fall through */ |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1548 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1549 default: |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1550 return NGX_CONF_ERROR; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1551 } |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1552 } |