Mercurial > hg > nginx
annotate src/stream/ngx_stream_proxy_module.c @ 6216:543f10fe34d2
Style.
author | Roman Arutyunyan <arut@nginx.com> |
---|---|
date | Wed, 29 Jul 2015 14:36:36 -0700 |
parents | 8ee6a08ea3eb |
children | b544f8e0d921 |
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 |
6216 | 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 /* optimization for a typical case */ |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
374 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
375 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
|
376 "stream proxy send PROXY protocol header"); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
377 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
378 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
|
379 u->downstream_buf.end); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
380 if (p == NULL) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
381 ngx_stream_proxy_finalize(s, NGX_ERROR); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
382 return; |
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 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
385 u->downstream_buf.last = p; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
386 u->proxy_protocol = 0; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
387 } |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
388 |
6115 | 389 if (ngx_stream_proxy_process(s, 0, 0) != NGX_OK) { |
390 return; | |
391 } | |
392 | |
393 ngx_stream_proxy_connect(s); | |
394 } | |
395 | |
396 | |
397 static void | |
398 ngx_stream_proxy_connect(ngx_stream_session_t *s) | |
399 { | |
400 ngx_int_t rc; | |
401 ngx_connection_t *c, *pc; | |
402 ngx_stream_upstream_t *u; | |
403 ngx_stream_proxy_srv_conf_t *pscf; | |
404 | |
405 c = s->connection; | |
406 | |
407 c->log->action = "connecting to upstream"; | |
408 | |
409 u = s->upstream; | |
410 | |
411 rc = ngx_event_connect_peer(&u->peer); | |
412 | |
413 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, c->log, 0, "proxy connect: %i", rc); | |
414 | |
415 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
416 | |
417 if (rc == NGX_ERROR) { | |
418 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
419 return; | |
420 } | |
421 | |
422 if (rc == NGX_BUSY) { | |
423 ngx_log_error(NGX_LOG_ERR, c->log, 0, "no live upstreams"); | |
424 ngx_stream_proxy_finalize(s, NGX_DECLINED); | |
425 return; | |
426 } | |
427 | |
428 if (rc == NGX_DECLINED) { | |
429 ngx_stream_proxy_next_upstream(s); | |
430 return; | |
431 } | |
432 | |
433 /* rc == NGX_OK || rc == NGX_AGAIN || rc == NGX_DONE */ | |
434 | |
435 pc = u->peer.connection; | |
436 | |
437 pc->data = s; | |
438 pc->log = c->log; | |
439 pc->pool = c->pool; | |
440 pc->read->log = c->log; | |
441 pc->write->log = c->log; | |
442 | |
443 if (rc != NGX_AGAIN) { | |
444 ngx_stream_proxy_init_upstream(s); | |
445 return; | |
446 } | |
447 | |
448 pc->read->handler = ngx_stream_proxy_connect_handler; | |
449 pc->write->handler = ngx_stream_proxy_connect_handler; | |
450 | |
451 ngx_add_timer(pc->write, pscf->connect_timeout); | |
452 } | |
453 | |
454 | |
455 static void | |
456 ngx_stream_proxy_init_upstream(ngx_stream_session_t *s) | |
457 { | |
458 u_char *p; | |
459 ngx_connection_t *c, *pc; | |
460 ngx_log_handler_pt handler; | |
461 ngx_stream_upstream_t *u; | |
462 ngx_stream_proxy_srv_conf_t *pscf; | |
463 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
464 u = s->upstream; |
6115 | 465 |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
466 if (u->proxy_protocol) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
467 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
|
468 return; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
469 } |
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 u->proxy_protocol = 0; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
472 } |
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 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); |
6115 | 475 |
476 pc = u->peer.connection; | |
477 | |
478 #if (NGX_STREAM_SSL) | |
479 if (pscf->ssl && pc->ssl == NULL) { | |
480 ngx_stream_proxy_ssl_init_connection(s); | |
481 return; | |
482 } | |
483 #endif | |
484 | |
485 c = s->connection; | |
486 | |
487 if (c->log->log_level >= NGX_LOG_INFO) { | |
488 ngx_str_t s; | |
489 u_char addr[NGX_SOCKADDR_STRLEN]; | |
490 | |
491 s.len = NGX_SOCKADDR_STRLEN; | |
492 s.data = addr; | |
493 | |
494 if (ngx_connection_local_sockaddr(pc, &s, 1) == NGX_OK) { | |
495 handler = c->log->handler; | |
496 c->log->handler = NULL; | |
497 | |
498 ngx_log_error(NGX_LOG_INFO, c->log, 0, "proxy %V connected to %V", | |
499 &s, u->peer.name); | |
500 | |
501 c->log->handler = handler; | |
502 } | |
503 } | |
504 | |
505 c->log->action = "proxying connection"; | |
506 | |
6215
8ee6a08ea3eb
Stream: added proxy_buffer_size to set the size of data buffers.
Roman Arutyunyan <arut@nginx.com>
parents:
6208
diff
changeset
|
507 p = ngx_pnalloc(c->pool, pscf->buffer_size); |
6115 | 508 if (p == NULL) { |
509 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
510 return; | |
511 } | |
512 | |
513 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
|
514 u->upstream_buf.end = p + pscf->buffer_size; |
6115 | 515 u->upstream_buf.pos = p; |
516 u->upstream_buf.last = p; | |
517 | |
6202
6345822f0abb
Stream: upstream "connected" flag.
Roman Arutyunyan <arut@nginx.com>
parents:
6201
diff
changeset
|
518 u->connected = 1; |
6345822f0abb
Stream: upstream "connected" flag.
Roman Arutyunyan <arut@nginx.com>
parents:
6201
diff
changeset
|
519 |
6115 | 520 pc->read->handler = ngx_stream_proxy_upstream_handler; |
521 pc->write->handler = ngx_stream_proxy_upstream_handler; | |
522 | |
523 if (ngx_stream_proxy_process(s, 1, 0) != NGX_OK) { | |
524 return; | |
525 } | |
526 | |
527 ngx_stream_proxy_process(s, 0, 1); | |
528 } | |
529 | |
530 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
531 static ngx_int_t |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
532 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
|
533 { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
534 u_char *p; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
535 ssize_t n, size; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
536 ngx_connection_t *c, *pc; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
537 ngx_stream_upstream_t *u; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
538 ngx_stream_proxy_srv_conf_t *pscf; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
539 u_char buf[NGX_PROXY_PROTOCOL_MAX_HEADER]; |
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 c = s->connection; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
542 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
543 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
|
544 "stream proxy send PROXY protocol header"); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
545 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
546 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
|
547 if (p == NULL) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
548 ngx_stream_proxy_finalize(s, NGX_ERROR); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
549 return NGX_ERROR; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
550 } |
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 u = s->upstream; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
553 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
554 pc = u->peer.connection; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
555 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
556 size = p - buf; |
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 n = pc->send(pc, buf, size); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
559 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
560 if (n == NGX_AGAIN) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
561 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
|
562 ngx_stream_proxy_finalize(s, NGX_ERROR); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
563 return NGX_ERROR; |
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 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
566 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
|
567 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
568 ngx_add_timer(pc->write, pscf->timeout); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
569 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
570 pc->write->handler = ngx_stream_proxy_connect_handler; |
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 return NGX_AGAIN; |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
573 } |
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 if (n == NGX_ERROR) { |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
576 ngx_stream_proxy_finalize(s, NGX_DECLINED); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
577 return NGX_ERROR; |
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 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
580 if (n != size) { |
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 /* |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
583 * PROXY protocol specification: |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
584 * The sender must always ensure that the header |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
585 * is sent at once, so that the transport layer |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
586 * maintains atomicity along the path to the receiver. |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
587 */ |
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 ngx_log_error(NGX_LOG_ERR, c->log, 0, |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
590 "could not send PROXY protocol header at once"); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
591 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
592 ngx_stream_proxy_finalize(s, NGX_DECLINED); |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
593 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
594 return NGX_ERROR; |
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 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
597 return NGX_OK; |
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 |
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
600 |
6115 | 601 #if (NGX_STREAM_SSL) |
602 | |
603 static char * | |
604 ngx_stream_proxy_ssl_password_file(ngx_conf_t *cf, ngx_command_t *cmd, | |
605 void *conf) | |
606 { | |
607 ngx_stream_proxy_srv_conf_t *pscf = conf; | |
608 | |
609 ngx_str_t *value; | |
610 | |
611 if (pscf->ssl_passwords != NGX_CONF_UNSET_PTR) { | |
612 return "is duplicate"; | |
613 } | |
614 | |
615 value = cf->args->elts; | |
616 | |
617 pscf->ssl_passwords = ngx_ssl_read_password_file(cf, &value[1]); | |
618 | |
619 if (pscf->ssl_passwords == NULL) { | |
620 return NGX_CONF_ERROR; | |
621 } | |
622 | |
623 return NGX_CONF_OK; | |
624 } | |
625 | |
626 | |
627 static void | |
628 ngx_stream_proxy_ssl_init_connection(ngx_stream_session_t *s) | |
629 { | |
630 ngx_int_t rc; | |
631 ngx_connection_t *pc; | |
632 ngx_stream_upstream_t *u; | |
633 ngx_stream_proxy_srv_conf_t *pscf; | |
634 | |
635 u = s->upstream; | |
636 | |
637 pc = u->peer.connection; | |
638 | |
639 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
640 | |
641 if (ngx_ssl_create_connection(pscf->ssl, pc, NGX_SSL_BUFFER|NGX_SSL_CLIENT) | |
642 != NGX_OK) | |
643 { | |
644 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
645 return; | |
646 } | |
647 | |
648 if (pscf->ssl_server_name || pscf->ssl_verify) { | |
649 if (ngx_stream_proxy_ssl_name(s) != NGX_OK) { | |
650 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
651 return; | |
652 } | |
653 } | |
654 | |
655 if (pscf->ssl_session_reuse) { | |
656 if (u->peer.set_session(&u->peer, u->peer.data) != NGX_OK) { | |
657 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
658 return; | |
659 } | |
660 } | |
661 | |
662 s->connection->log->action = "SSL handshaking to upstream"; | |
663 | |
664 rc = ngx_ssl_handshake(pc); | |
665 | |
666 if (rc == NGX_AGAIN) { | |
667 | |
668 if (!pc->write->timer_set) { | |
669 ngx_add_timer(pc->write, pscf->connect_timeout); | |
670 } | |
671 | |
672 pc->ssl->handler = ngx_stream_proxy_ssl_handshake; | |
673 return; | |
674 } | |
675 | |
676 ngx_stream_proxy_ssl_handshake(pc); | |
677 } | |
678 | |
679 | |
680 static void | |
681 ngx_stream_proxy_ssl_handshake(ngx_connection_t *pc) | |
682 { | |
683 long rc; | |
684 ngx_stream_session_t *s; | |
685 ngx_stream_upstream_t *u; | |
686 ngx_stream_proxy_srv_conf_t *pscf; | |
687 | |
688 s = pc->data; | |
689 | |
690 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
691 | |
692 if (pc->ssl->handshaked) { | |
693 | |
694 if (pscf->ssl_verify) { | |
695 rc = SSL_get_verify_result(pc->ssl->connection); | |
696 | |
697 if (rc != X509_V_OK) { | |
698 ngx_log_error(NGX_LOG_ERR, pc->log, 0, | |
699 "upstream SSL certificate verify error: (%l:%s)", | |
700 rc, X509_verify_cert_error_string(rc)); | |
701 goto failed; | |
702 } | |
703 | |
704 u = s->upstream; | |
705 | |
706 if (ngx_ssl_check_host(pc, &u->ssl_name) != NGX_OK) { | |
707 ngx_log_error(NGX_LOG_ERR, pc->log, 0, | |
708 "upstream SSL certificate does not match \"%V\"", | |
709 &u->ssl_name); | |
710 goto failed; | |
711 } | |
712 } | |
713 | |
714 if (pscf->ssl_session_reuse) { | |
715 u = s->upstream; | |
716 u->peer.save_session(&u->peer, u->peer.data); | |
717 } | |
718 | |
719 ngx_stream_proxy_init_upstream(s); | |
720 | |
721 return; | |
722 } | |
723 | |
724 failed: | |
725 | |
726 ngx_stream_proxy_next_upstream(s); | |
727 } | |
728 | |
729 | |
730 static ngx_int_t | |
731 ngx_stream_proxy_ssl_name(ngx_stream_session_t *s) | |
732 { | |
733 u_char *p, *last; | |
734 ngx_str_t name; | |
735 ngx_stream_upstream_t *u; | |
736 ngx_stream_proxy_srv_conf_t *pscf; | |
737 | |
738 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
739 | |
740 u = s->upstream; | |
741 | |
742 name = pscf->ssl_name; | |
743 | |
744 if (name.len == 0) { | |
745 name = pscf->upstream->host; | |
746 } | |
747 | |
748 if (name.len == 0) { | |
749 goto done; | |
750 } | |
751 | |
752 /* | |
753 * ssl name here may contain port, strip it for compatibility | |
754 * with the http module | |
755 */ | |
756 | |
757 p = name.data; | |
758 last = name.data + name.len; | |
759 | |
760 if (*p == '[') { | |
761 p = ngx_strlchr(p, last, ']'); | |
762 | |
763 if (p == NULL) { | |
764 p = name.data; | |
765 } | |
766 } | |
767 | |
768 p = ngx_strlchr(p, last, ':'); | |
769 | |
770 if (p != NULL) { | |
771 name.len = p - name.data; | |
772 } | |
773 | |
774 if (!pscf->ssl_server_name) { | |
775 goto done; | |
776 } | |
777 | |
778 #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME | |
779 | |
780 /* as per RFC 6066, literal IPv4 and IPv6 addresses are not permitted */ | |
781 | |
782 if (name.len == 0 || *name.data == '[') { | |
783 goto done; | |
784 } | |
785 | |
786 if (ngx_inet_addr(name.data, name.len) != INADDR_NONE) { | |
787 goto done; | |
788 } | |
789 | |
790 /* | |
791 * SSL_set_tlsext_host_name() needs a null-terminated string, | |
792 * hence we explicitly null-terminate name here | |
793 */ | |
794 | |
795 p = ngx_pnalloc(s->connection->pool, name.len + 1); | |
796 if (p == NULL) { | |
797 return NGX_ERROR; | |
798 } | |
799 | |
800 (void) ngx_cpystrn(p, name.data, name.len + 1); | |
801 | |
802 name.data = p; | |
803 | |
804 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
805 "upstream SSL server name: \"%s\"", name.data); | |
806 | |
807 if (SSL_set_tlsext_host_name(u->peer.connection->ssl->connection, name.data) | |
808 == 0) | |
809 { | |
810 ngx_ssl_error(NGX_LOG_ERR, s->connection->log, 0, | |
811 "SSL_set_tlsext_host_name(\"%s\") failed", name.data); | |
812 return NGX_ERROR; | |
813 } | |
814 | |
815 #endif | |
816 | |
817 done: | |
818 | |
819 u->ssl_name = name; | |
820 | |
821 return NGX_OK; | |
822 } | |
823 | |
824 #endif | |
825 | |
826 | |
827 static void | |
828 ngx_stream_proxy_downstream_handler(ngx_event_t *ev) | |
829 { | |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
830 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
|
831 } |
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 static void |
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
835 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
|
836 { |
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_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
|
843 { |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
844 ngx_connection_t *c, *pc; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
845 ngx_stream_session_t *s; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
846 ngx_stream_upstream_t *u; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
847 ngx_stream_proxy_srv_conf_t *pscf; |
6115 | 848 |
849 c = ev->data; | |
850 s = c->data; | |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
851 u = s->upstream; |
6115 | 852 |
853 if (ev->timedout) { | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
854 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
855 if (ev->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
856 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
857 ev->timedout = 0; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
858 ev->delayed = 0; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
859 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
860 if (!ev->ready) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
861 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
|
862 ngx_stream_proxy_finalize(s, NGX_ERROR); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
863 return; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
864 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
865 |
6202
6345822f0abb
Stream: upstream "connected" flag.
Roman Arutyunyan <arut@nginx.com>
parents:
6201
diff
changeset
|
866 if (u->connected) { |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
867 pc = u->peer.connection; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
868 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
869 if (!c->read->delayed && !pc->read->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
870 pscf = ngx_stream_get_module_srv_conf(s, |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
871 ngx_stream_proxy_module); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
872 ngx_add_timer(c->write, pscf->timeout); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
873 } |
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 return; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
877 } |
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 } else { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
880 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
|
881 ngx_stream_proxy_finalize(s, NGX_DECLINED); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
882 return; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
883 } |
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 } else if (ev->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
886 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
887 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
|
888 "stream connection delayed"); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
889 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
890 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
|
891 ngx_stream_proxy_finalize(s, NGX_ERROR); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
892 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
893 |
6115 | 894 return; |
895 } | |
896 | |
6202
6345822f0abb
Stream: upstream "connected" flag.
Roman Arutyunyan <arut@nginx.com>
parents:
6201
diff
changeset
|
897 if (from_upstream && !u->connected) { |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
898 return; |
6115 | 899 } |
900 | |
6200
abee77018d3a
Stream: common handler for upstream and downstream.
Roman Arutyunyan <arut@nginx.com>
parents:
6184
diff
changeset
|
901 ngx_stream_proxy_process(s, from_upstream, ev->write); |
6115 | 902 } |
903 | |
904 | |
905 static void | |
906 ngx_stream_proxy_connect_handler(ngx_event_t *ev) | |
907 { | |
908 ngx_connection_t *c; | |
909 ngx_stream_session_t *s; | |
910 | |
911 c = ev->data; | |
912 s = c->data; | |
913 | |
914 if (ev->timedout) { | |
915 ngx_log_error(NGX_LOG_ERR, c->log, NGX_ETIMEDOUT, "upstream timed out"); | |
916 ngx_stream_proxy_next_upstream(s); | |
917 return; | |
918 } | |
919 | |
920 ngx_del_timer(c->write); | |
921 | |
922 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, c->log, 0, | |
923 "stream proxy connect upstream"); | |
924 | |
925 if (ngx_stream_proxy_test_connect(c) != NGX_OK) { | |
926 ngx_stream_proxy_next_upstream(s); | |
927 return; | |
928 } | |
929 | |
930 ngx_stream_proxy_init_upstream(s); | |
931 } | |
932 | |
933 | |
934 static ngx_int_t | |
935 ngx_stream_proxy_test_connect(ngx_connection_t *c) | |
936 { | |
937 int err; | |
938 socklen_t len; | |
939 | |
940 #if (NGX_HAVE_KQUEUE) | |
941 | |
942 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) { | |
943 err = c->write->kq_errno ? c->write->kq_errno : c->read->kq_errno; | |
944 | |
945 if (err) { | |
946 (void) ngx_connection_error(c, err, | |
947 "kevent() reported that connect() failed"); | |
948 return NGX_ERROR; | |
949 } | |
950 | |
951 } else | |
952 #endif | |
953 { | |
954 err = 0; | |
955 len = sizeof(int); | |
956 | |
957 /* | |
958 * BSDs and Linux return 0 and set a pending error in err | |
959 * Solaris returns -1 and sets errno | |
960 */ | |
961 | |
962 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, (void *) &err, &len) | |
963 == -1) | |
964 { | |
965 err = ngx_socket_errno; | |
966 } | |
967 | |
968 if (err) { | |
969 (void) ngx_connection_error(c, err, "connect() failed"); | |
970 return NGX_ERROR; | |
971 } | |
972 } | |
973 | |
974 return NGX_OK; | |
975 } | |
976 | |
977 | |
978 static ngx_int_t | |
979 ngx_stream_proxy_process(ngx_stream_session_t *s, ngx_uint_t from_upstream, | |
980 ngx_uint_t do_write) | |
981 { | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
982 off_t *received, limit; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
983 size_t size, limit_rate; |
6115 | 984 ssize_t n; |
985 ngx_buf_t *b; | |
6124
f1f222db290b
Stream: prevent repeated event notifications after eof.
Roman Arutyunyan <arut@nginx.com>
parents:
6115
diff
changeset
|
986 ngx_uint_t flags; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
987 ngx_msec_t delay; |
6115 | 988 ngx_connection_t *c, *pc, *src, *dst; |
989 ngx_log_handler_pt handler; | |
990 ngx_stream_upstream_t *u; | |
991 ngx_stream_proxy_srv_conf_t *pscf; | |
992 | |
993 u = s->upstream; | |
994 | |
995 c = s->connection; | |
6202
6345822f0abb
Stream: upstream "connected" flag.
Roman Arutyunyan <arut@nginx.com>
parents:
6201
diff
changeset
|
996 pc = u->connected ? u->peer.connection : NULL; |
6115 | 997 |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
998 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
|
999 |
6115 | 1000 if (from_upstream) { |
1001 src = pc; | |
1002 dst = c; | |
1003 b = &u->upstream_buf; | |
6208
7a14a0d754ad
Stream: renamed rate limiting directives.
Roman Arutyunyan <arut@nginx.com>
parents:
6204
diff
changeset
|
1004 limit_rate = pscf->download_rate; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1005 received = &u->received; |
6115 | 1006 |
1007 } else { | |
1008 src = c; | |
1009 dst = pc; | |
1010 b = &u->downstream_buf; | |
6208
7a14a0d754ad
Stream: renamed rate limiting directives.
Roman Arutyunyan <arut@nginx.com>
parents:
6204
diff
changeset
|
1011 limit_rate = pscf->upload_rate; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1012 received = &s->received; |
6115 | 1013 } |
1014 | |
1015 for ( ;; ) { | |
1016 | |
1017 if (do_write) { | |
1018 | |
1019 size = b->last - b->pos; | |
1020 | |
1021 if (size && dst && dst->write->ready) { | |
1022 | |
1023 n = dst->send(dst, b->pos, size); | |
1024 | |
1025 if (n == NGX_ERROR) { | |
1026 ngx_stream_proxy_finalize(s, NGX_DECLINED); | |
1027 return NGX_ERROR; | |
1028 } | |
1029 | |
1030 if (n > 0) { | |
1031 b->pos += n; | |
1032 | |
1033 if (b->pos == b->last) { | |
1034 b->pos = b->start; | |
1035 b->last = b->start; | |
1036 } | |
1037 } | |
1038 } | |
1039 } | |
1040 | |
1041 size = b->end - b->last; | |
1042 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1043 if (size && src->read->ready && !src->read->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1044 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1045 if (limit_rate) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1046 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
|
1047 - *received; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1048 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1049 if (limit <= 0) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1050 src->read->delayed = 1; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1051 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
|
1052 ngx_add_timer(src->read, delay); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1053 break; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1054 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1055 |
6204
114d1f8cdcab
Stream: fixed possible integer overflow in rate limiting.
Valentin Bartenev <vbart@nginx.com>
parents:
6203
diff
changeset
|
1056 if ((off_t) size > limit) { |
6203
fdfdcad62875
Stream: fixed MSVC compilation warning.
Roman Arutyunyan <arut@nginx.com>
parents:
6202
diff
changeset
|
1057 size = (size_t) limit; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1058 } |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1059 } |
6115 | 1060 |
1061 n = src->recv(src, b->last, size); | |
1062 | |
1063 if (n == NGX_AGAIN || n == 0) { | |
1064 break; | |
1065 } | |
1066 | |
1067 if (n > 0) { | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1068 if (limit_rate) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1069 delay = (ngx_msec_t) (n * 1000 / limit_rate); |
6115 | 1070 |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1071 if (delay > 0) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1072 src->read->delayed = 1; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1073 ngx_add_timer(src->read, delay); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1074 } |
6115 | 1075 } |
1076 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1077 *received += n; |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1078 b->last += n; |
6115 | 1079 do_write = 1; |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1080 |
6115 | 1081 continue; |
1082 } | |
1083 | |
1084 if (n == NGX_ERROR) { | |
1085 src->read->eof = 1; | |
1086 } | |
1087 } | |
1088 | |
1089 break; | |
1090 } | |
1091 | |
1092 if (src->read->eof && (b->pos == b->last || (dst && dst->read->eof))) { | |
1093 handler = c->log->handler; | |
1094 c->log->handler = NULL; | |
1095 | |
1096 ngx_log_error(NGX_LOG_INFO, c->log, 0, | |
1097 "%s disconnected" | |
1098 ", bytes from/to client:%O/%O" | |
1099 ", bytes from/to upstream:%O/%O", | |
1100 from_upstream ? "upstream" : "client", | |
1101 s->received, c->sent, u->received, pc ? pc->sent : 0); | |
1102 | |
1103 c->log->handler = handler; | |
1104 | |
1105 ngx_stream_proxy_finalize(s, NGX_OK); | |
1106 return NGX_DONE; | |
1107 } | |
1108 | |
6124
f1f222db290b
Stream: prevent repeated event notifications after eof.
Roman Arutyunyan <arut@nginx.com>
parents:
6115
diff
changeset
|
1109 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
|
1110 |
f1f222db290b
Stream: prevent repeated event notifications after eof.
Roman Arutyunyan <arut@nginx.com>
parents:
6115
diff
changeset
|
1111 if (ngx_handle_read_event(src->read, flags) != NGX_OK) { |
6115 | 1112 ngx_stream_proxy_finalize(s, NGX_ERROR); |
1113 return NGX_ERROR; | |
1114 } | |
1115 | |
1116 if (dst) { | |
1117 if (ngx_handle_write_event(dst->write, 0) != NGX_OK) { | |
1118 ngx_stream_proxy_finalize(s, NGX_ERROR); | |
1119 return NGX_ERROR; | |
1120 } | |
1121 | |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1122 if (!c->read->delayed && !pc->read->delayed) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1123 ngx_add_timer(c->write, pscf->timeout); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1124 |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1125 } else if (c->write->timer_set) { |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1126 ngx_del_timer(c->write); |
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1127 } |
6115 | 1128 } |
1129 | |
1130 return NGX_OK; | |
1131 } | |
1132 | |
1133 | |
1134 static void | |
1135 ngx_stream_proxy_next_upstream(ngx_stream_session_t *s) | |
1136 { | |
1137 ngx_msec_t timeout; | |
1138 ngx_connection_t *pc; | |
1139 ngx_stream_upstream_t *u; | |
1140 ngx_stream_proxy_srv_conf_t *pscf; | |
1141 | |
1142 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
1143 "stream proxy next upstream"); | |
1144 | |
1145 u = s->upstream; | |
1146 | |
1147 if (u->peer.sockaddr) { | |
1148 u->peer.free(&u->peer, u->peer.data, NGX_PEER_FAILED); | |
1149 u->peer.sockaddr = NULL; | |
1150 } | |
1151 | |
1152 pscf = ngx_stream_get_module_srv_conf(s, ngx_stream_proxy_module); | |
1153 | |
1154 timeout = pscf->next_upstream_timeout; | |
1155 | |
1156 if (u->peer.tries == 0 | |
1157 || !pscf->next_upstream | |
1158 || (timeout && ngx_current_msec - u->peer.start_time >= timeout)) | |
1159 { | |
1160 ngx_stream_proxy_finalize(s, NGX_DECLINED); | |
1161 return; | |
1162 } | |
1163 | |
1164 pc = u->peer.connection; | |
1165 | |
1166 if (pc) { | |
1167 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
1168 "close proxy upstream connection: %d", pc->fd); | |
1169 | |
1170 #if (NGX_STREAM_SSL) | |
1171 if (pc->ssl) { | |
1172 pc->ssl->no_wait_shutdown = 1; | |
1173 pc->ssl->no_send_shutdown = 1; | |
1174 | |
1175 (void) ngx_ssl_shutdown(pc); | |
1176 } | |
1177 #endif | |
1178 | |
1179 ngx_close_connection(pc); | |
1180 u->peer.connection = NULL; | |
1181 } | |
1182 | |
1183 ngx_stream_proxy_connect(s); | |
1184 } | |
1185 | |
1186 | |
1187 static void | |
1188 ngx_stream_proxy_finalize(ngx_stream_session_t *s, ngx_int_t rc) | |
1189 { | |
1190 ngx_connection_t *pc; | |
1191 ngx_stream_upstream_t *u; | |
1192 | |
1193 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
1194 "finalize stream proxy: %i", rc); | |
1195 | |
1196 u = s->upstream; | |
1197 | |
1198 if (u == NULL) { | |
1199 goto noupstream; | |
1200 } | |
1201 | |
1202 if (u->peer.free && u->peer.sockaddr) { | |
1203 u->peer.free(&u->peer, u->peer.data, 0); | |
1204 u->peer.sockaddr = NULL; | |
1205 } | |
1206 | |
1207 pc = u->peer.connection; | |
1208 | |
1209 if (pc) { | |
1210 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, s->connection->log, 0, | |
1211 "close stream proxy upstream connection: %d", pc->fd); | |
1212 | |
1213 #if (NGX_STREAM_SSL) | |
1214 if (pc->ssl) { | |
1215 pc->ssl->no_wait_shutdown = 1; | |
1216 (void) ngx_ssl_shutdown(pc); | |
1217 } | |
1218 #endif | |
1219 | |
1220 ngx_close_connection(pc); | |
1221 u->peer.connection = NULL; | |
1222 } | |
1223 | |
1224 noupstream: | |
1225 | |
1226 ngx_stream_close_connection(s->connection); | |
1227 } | |
1228 | |
1229 | |
1230 static u_char * | |
1231 ngx_stream_proxy_log_error(ngx_log_t *log, u_char *buf, size_t len) | |
1232 { | |
1233 u_char *p; | |
1234 ngx_connection_t *pc; | |
1235 ngx_stream_session_t *s; | |
1236 ngx_stream_upstream_t *u; | |
1237 | |
1238 s = log->data; | |
1239 | |
1240 u = s->upstream; | |
1241 | |
1242 p = buf; | |
1243 | |
1244 if (u->peer.name) { | |
1245 p = ngx_snprintf(p, len, ", upstream: \"%V\"", u->peer.name); | |
1246 len -= p - buf; | |
1247 } | |
1248 | |
1249 pc = u->peer.connection; | |
1250 | |
1251 p = ngx_snprintf(p, len, | |
1252 ", bytes from/to client:%O/%O" | |
1253 ", bytes from/to upstream:%O/%O", | |
1254 s->received, s->connection->sent, | |
1255 u->received, pc ? pc->sent : 0); | |
1256 | |
1257 return p; | |
1258 } | |
1259 | |
1260 | |
1261 static void * | |
1262 ngx_stream_proxy_create_srv_conf(ngx_conf_t *cf) | |
1263 { | |
1264 ngx_stream_proxy_srv_conf_t *conf; | |
1265 | |
1266 conf = ngx_pcalloc(cf->pool, sizeof(ngx_stream_proxy_srv_conf_t)); | |
1267 if (conf == NULL) { | |
1268 return NULL; | |
1269 } | |
1270 | |
1271 /* | |
1272 * set by ngx_pcalloc(): | |
1273 * | |
1274 * conf->ssl_protocols = 0; | |
1275 * conf->ssl_ciphers = { 0, NULL }; | |
1276 * conf->ssl_name = { 0, NULL }; | |
1277 * conf->ssl_trusted_certificate = { 0, NULL }; | |
1278 * conf->ssl_crl = { 0, NULL }; | |
1279 * conf->ssl_certificate = { 0, NULL }; | |
1280 * conf->ssl_certificate_key = { 0, NULL }; | |
1281 * | |
1282 * conf->ssl = NULL; | |
1283 * conf->upstream = NULL; | |
1284 */ | |
1285 | |
1286 conf->connect_timeout = NGX_CONF_UNSET_MSEC; | |
1287 conf->timeout = NGX_CONF_UNSET_MSEC; | |
1288 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
|
1289 conf->buffer_size = NGX_CONF_UNSET_SIZE; |
6208
7a14a0d754ad
Stream: renamed rate limiting directives.
Roman Arutyunyan <arut@nginx.com>
parents:
6204
diff
changeset
|
1290 conf->upload_rate = NGX_CONF_UNSET_SIZE; |
7a14a0d754ad
Stream: renamed rate limiting directives.
Roman Arutyunyan <arut@nginx.com>
parents:
6204
diff
changeset
|
1291 conf->download_rate = NGX_CONF_UNSET_SIZE; |
6115 | 1292 conf->next_upstream_tries = NGX_CONF_UNSET_UINT; |
1293 conf->next_upstream = NGX_CONF_UNSET; | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
1294 conf->proxy_protocol = NGX_CONF_UNSET; |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1295 conf->local = NGX_CONF_UNSET_PTR; |
6115 | 1296 |
1297 #if (NGX_STREAM_SSL) | |
1298 conf->ssl_enable = NGX_CONF_UNSET; | |
1299 conf->ssl_session_reuse = NGX_CONF_UNSET; | |
1300 conf->ssl_server_name = NGX_CONF_UNSET; | |
1301 conf->ssl_verify = NGX_CONF_UNSET; | |
1302 conf->ssl_verify_depth = NGX_CONF_UNSET_UINT; | |
1303 conf->ssl_passwords = NGX_CONF_UNSET_PTR; | |
1304 #endif | |
1305 | |
1306 return conf; | |
1307 } | |
1308 | |
1309 | |
1310 static char * | |
1311 ngx_stream_proxy_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child) | |
1312 { | |
1313 ngx_stream_proxy_srv_conf_t *prev = parent; | |
1314 ngx_stream_proxy_srv_conf_t *conf = child; | |
1315 | |
1316 ngx_conf_merge_msec_value(conf->connect_timeout, | |
1317 prev->connect_timeout, 60000); | |
1318 | |
1319 ngx_conf_merge_msec_value(conf->timeout, | |
1320 prev->timeout, 10 * 60000); | |
1321 | |
1322 ngx_conf_merge_msec_value(conf->next_upstream_timeout, | |
1323 prev->next_upstream_timeout, 0); | |
1324 | |
6215
8ee6a08ea3eb
Stream: added proxy_buffer_size to set the size of data buffers.
Roman Arutyunyan <arut@nginx.com>
parents:
6208
diff
changeset
|
1325 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
|
1326 prev->buffer_size, 16384); |
6115 | 1327 |
6208
7a14a0d754ad
Stream: renamed rate limiting directives.
Roman Arutyunyan <arut@nginx.com>
parents:
6204
diff
changeset
|
1328 ngx_conf_merge_size_value(conf->upload_rate, |
7a14a0d754ad
Stream: renamed rate limiting directives.
Roman Arutyunyan <arut@nginx.com>
parents:
6204
diff
changeset
|
1329 prev->upload_rate, 0); |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1330 |
6208
7a14a0d754ad
Stream: renamed rate limiting directives.
Roman Arutyunyan <arut@nginx.com>
parents:
6204
diff
changeset
|
1331 ngx_conf_merge_size_value(conf->download_rate, |
7a14a0d754ad
Stream: renamed rate limiting directives.
Roman Arutyunyan <arut@nginx.com>
parents:
6204
diff
changeset
|
1332 prev->download_rate, 0); |
6201
24488e6db782
Stream: upstream and downstream limit rates.
Roman Arutyunyan <arut@nginx.com>
parents:
6200
diff
changeset
|
1333 |
6115 | 1334 ngx_conf_merge_uint_value(conf->next_upstream_tries, |
1335 prev->next_upstream_tries, 0); | |
1336 | |
1337 ngx_conf_merge_value(conf->next_upstream, prev->next_upstream, 1); | |
1338 | |
6184
fa663739e115
Stream: client-side PROXY protocol.
Roman Arutyunyan <arut@nginx.com>
parents:
6183
diff
changeset
|
1339 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
|
1340 |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1341 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
|
1342 |
6115 | 1343 #if (NGX_STREAM_SSL) |
1344 | |
1345 ngx_conf_merge_value(conf->ssl_enable, prev->ssl_enable, 0); | |
1346 | |
1347 ngx_conf_merge_value(conf->ssl_session_reuse, | |
1348 prev->ssl_session_reuse, 1); | |
1349 | |
1350 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
|
1351 (NGX_CONF_BITMASK_SET|NGX_SSL_TLSv1 |
b2899e7d0ef8
Disabled SSLv3 by default (ticket #653).
Maxim Dounin <mdounin@mdounin.ru>
parents:
6124
diff
changeset
|
1352 |NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2)); |
6115 | 1353 |
1354 ngx_conf_merge_str_value(conf->ssl_ciphers, prev->ssl_ciphers, "DEFAULT"); | |
1355 | |
1356 ngx_conf_merge_str_value(conf->ssl_name, prev->ssl_name, ""); | |
1357 | |
1358 ngx_conf_merge_value(conf->ssl_server_name, prev->ssl_server_name, 0); | |
1359 | |
1360 ngx_conf_merge_value(conf->ssl_verify, prev->ssl_verify, 0); | |
1361 | |
1362 ngx_conf_merge_uint_value(conf->ssl_verify_depth, | |
1363 prev->ssl_verify_depth, 1); | |
1364 | |
1365 ngx_conf_merge_str_value(conf->ssl_trusted_certificate, | |
1366 prev->ssl_trusted_certificate, ""); | |
1367 | |
1368 ngx_conf_merge_str_value(conf->ssl_crl, prev->ssl_crl, ""); | |
1369 | |
1370 ngx_conf_merge_str_value(conf->ssl_certificate, | |
1371 prev->ssl_certificate, ""); | |
1372 | |
1373 ngx_conf_merge_str_value(conf->ssl_certificate_key, | |
1374 prev->ssl_certificate_key, ""); | |
1375 | |
1376 ngx_conf_merge_ptr_value(conf->ssl_passwords, prev->ssl_passwords, NULL); | |
1377 | |
1378 if (conf->ssl_enable && ngx_stream_proxy_set_ssl(cf, conf) != NGX_OK) { | |
1379 return NGX_CONF_ERROR; | |
1380 } | |
1381 | |
1382 #endif | |
1383 | |
1384 return NGX_CONF_OK; | |
1385 } | |
1386 | |
1387 | |
1388 #if (NGX_STREAM_SSL) | |
1389 | |
1390 static ngx_int_t | |
1391 ngx_stream_proxy_set_ssl(ngx_conf_t *cf, ngx_stream_proxy_srv_conf_t *pscf) | |
1392 { | |
1393 ngx_pool_cleanup_t *cln; | |
1394 | |
1395 pscf->ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t)); | |
1396 if (pscf->ssl == NULL) { | |
1397 return NGX_ERROR; | |
1398 } | |
1399 | |
1400 pscf->ssl->log = cf->log; | |
1401 | |
1402 if (ngx_ssl_create(pscf->ssl, pscf->ssl_protocols, NULL) != NGX_OK) { | |
1403 return NGX_ERROR; | |
1404 } | |
1405 | |
1406 cln = ngx_pool_cleanup_add(cf->pool, 0); | |
1407 if (cln == NULL) { | |
1408 return NGX_ERROR; | |
1409 } | |
1410 | |
1411 cln->handler = ngx_ssl_cleanup_ctx; | |
1412 cln->data = pscf->ssl; | |
1413 | |
1414 if (pscf->ssl_certificate.len) { | |
1415 | |
1416 if (pscf->ssl_certificate_key.len == 0) { | |
1417 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
1418 "no \"proxy_ssl_certificate_key\" is defined " | |
1419 "for certificate \"%V\"", &pscf->ssl_certificate); | |
1420 return NGX_ERROR; | |
1421 } | |
1422 | |
1423 if (ngx_ssl_certificate(cf, pscf->ssl, &pscf->ssl_certificate, | |
1424 &pscf->ssl_certificate_key, pscf->ssl_passwords) | |
1425 != NGX_OK) | |
1426 { | |
1427 return NGX_ERROR; | |
1428 } | |
1429 } | |
1430 | |
1431 if (SSL_CTX_set_cipher_list(pscf->ssl->ctx, | |
1432 (const char *) pscf->ssl_ciphers.data) | |
1433 == 0) | |
1434 { | |
1435 ngx_ssl_error(NGX_LOG_EMERG, cf->log, 0, | |
1436 "SSL_CTX_set_cipher_list(\"%V\") failed", | |
1437 &pscf->ssl_ciphers); | |
1438 return NGX_ERROR; | |
1439 } | |
1440 | |
1441 if (pscf->ssl_verify) { | |
1442 if (pscf->ssl_trusted_certificate.len == 0) { | |
1443 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
1444 "no proxy_ssl_trusted_certificate for proxy_ssl_verify"); | |
1445 return NGX_ERROR; | |
1446 } | |
1447 | |
1448 if (ngx_ssl_trusted_certificate(cf, pscf->ssl, | |
1449 &pscf->ssl_trusted_certificate, | |
1450 pscf->ssl_verify_depth) | |
1451 != NGX_OK) | |
1452 { | |
1453 return NGX_ERROR; | |
1454 } | |
1455 | |
1456 if (ngx_ssl_crl(cf, pscf->ssl, &pscf->ssl_crl) != NGX_OK) { | |
1457 return NGX_ERROR; | |
1458 } | |
1459 } | |
1460 | |
1461 return NGX_OK; | |
1462 } | |
1463 | |
1464 #endif | |
1465 | |
1466 | |
1467 static char * | |
1468 ngx_stream_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
1469 { | |
1470 ngx_stream_proxy_srv_conf_t *pscf = conf; | |
1471 | |
1472 ngx_url_t u; | |
1473 ngx_str_t *value, *url; | |
1474 ngx_stream_core_srv_conf_t *cscf; | |
1475 | |
1476 if (pscf->upstream) { | |
1477 return "is duplicate"; | |
1478 } | |
1479 | |
1480 cscf = ngx_stream_conf_get_module_srv_conf(cf, ngx_stream_core_module); | |
1481 | |
1482 cscf->handler = ngx_stream_proxy_handler; | |
1483 | |
1484 value = cf->args->elts; | |
1485 | |
1486 url = &value[1]; | |
1487 | |
1488 ngx_memzero(&u, sizeof(ngx_url_t)); | |
1489 | |
1490 u.url = *url; | |
1491 u.no_resolve = 1; | |
1492 | |
1493 pscf->upstream = ngx_stream_upstream_add(cf, &u, 0); | |
1494 if (pscf->upstream == NULL) { | |
1495 return NGX_CONF_ERROR; | |
1496 } | |
1497 | |
1498 return NGX_CONF_OK; | |
1499 } | |
6183
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1500 |
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 static char * |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1503 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
|
1504 { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1505 ngx_stream_proxy_srv_conf_t *pscf = conf; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1506 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1507 ngx_int_t rc; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1508 ngx_str_t *value; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1509 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1510 if (pscf->local != NGX_CONF_UNSET_PTR) { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1511 return "is duplicate"; |
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 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1514 value = cf->args->elts; |
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 if (ngx_strcmp(value[1].data, "off") == 0) { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1517 pscf->local = NULL; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1518 return NGX_CONF_OK; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1519 } |
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 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
|
1522 if (pscf->local == NULL) { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1523 return NGX_CONF_ERROR; |
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 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1526 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
|
1527 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1528 switch (rc) { |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1529 case NGX_OK: |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1530 pscf->local->name = value[1]; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1531 return NGX_CONF_OK; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1532 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1533 case NGX_DECLINED: |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1534 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1535 "invalid address \"%V\"", &value[1]); |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1536 /* fall through */ |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1537 |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1538 default: |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1539 return NGX_CONF_ERROR; |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1540 } |
4dcffe43a7ea
Stream: the "proxy_bind" directive.
Vladimir Homutov <vl@nginx.com>
parents:
6174
diff
changeset
|
1541 } |