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