0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10 #include <ngx_http_proxy_handler.h>
|
|
11
|
|
12
|
|
13 static ngx_int_t ngx_http_proxy_handler(ngx_http_request_t *r);
|
50
|
14 #if 0
|
26
|
15 static ngx_int_t ngx_http_proxy_cache_get(ngx_http_proxy_ctx_t *p);
|
50
|
16 #endif
|
0
|
17
|
28
|
18 static size_t ngx_http_proxy_log_proxy_state_getlen(ngx_http_request_t *r,
|
|
19 uintptr_t data);
|
0
|
20 static u_char *ngx_http_proxy_log_proxy_state(ngx_http_request_t *r,
|
28
|
21 u_char *buf,
|
|
22 ngx_http_log_op_t *op);
|
|
23
|
|
24 #if 0
|
0
|
25 static u_char *ngx_http_proxy_log_cache_state(ngx_http_request_t *r,
|
|
26 u_char *buf, uintptr_t data);
|
|
27 static u_char *ngx_http_proxy_log_reason(ngx_http_request_t *r, u_char *buf,
|
|
28 uintptr_t data);
|
28
|
29 #endif
|
0
|
30
|
28
|
31 static ngx_int_t ngx_http_proxy_add_log_formats(ngx_conf_t *cf);
|
0
|
32 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
|
|
33 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
|
|
34 void *parent, void *child);
|
|
35
|
|
36 static char *ngx_http_proxy_set_pass(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
37 void *conf);
|
|
38
|
34
|
39 static char *ngx_http_proxy_set_x_var(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
40 void *conf);
|
2
|
41 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
|
|
42
|
|
43 static ngx_conf_post_t ngx_http_proxy_lowat_post =
|
|
44 { ngx_http_proxy_lowat_check } ;
|
|
45
|
0
|
46
|
|
47 static ngx_conf_bitmask_t next_upstream_masks[] = {
|
|
48 { ngx_string("error"), NGX_HTTP_PROXY_FT_ERROR },
|
|
49 { ngx_string("timeout"), NGX_HTTP_PROXY_FT_TIMEOUT },
|
|
50 { ngx_string("invalid_header"), NGX_HTTP_PROXY_FT_INVALID_HEADER },
|
|
51 { ngx_string("http_500"), NGX_HTTP_PROXY_FT_HTTP_500 },
|
|
52 { ngx_string("http_404"), NGX_HTTP_PROXY_FT_HTTP_404 },
|
|
53 { ngx_null_string, 0 }
|
|
54 };
|
|
55
|
|
56
|
|
57 static ngx_conf_bitmask_t use_stale_masks[] = {
|
|
58 { ngx_string("error"), NGX_HTTP_PROXY_FT_ERROR },
|
|
59 { ngx_string("timeout"), NGX_HTTP_PROXY_FT_TIMEOUT },
|
|
60 { ngx_string("invalid_header"), NGX_HTTP_PROXY_FT_INVALID_HEADER },
|
|
61 { ngx_string("http_500"), NGX_HTTP_PROXY_FT_HTTP_500 },
|
|
62 { ngx_string("busy_lock"), NGX_HTTP_PROXY_FT_BUSY_LOCK },
|
|
63 { ngx_string("max_waiting"), NGX_HTTP_PROXY_FT_MAX_WAITING },
|
|
64 { ngx_null_string, 0 }
|
|
65 };
|
|
66
|
|
67
|
|
68 static ngx_conf_num_bounds_t ngx_http_proxy_lm_factor_bounds = {
|
|
69 ngx_conf_check_num_bounds, 0, 100
|
|
70 };
|
|
71
|
|
72
|
|
73 static ngx_command_t ngx_http_proxy_commands[] = {
|
|
74
|
|
75 { ngx_string("proxy_pass"),
|
|
76 NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
77 ngx_http_proxy_set_pass,
|
|
78 NGX_HTTP_LOC_CONF_OFFSET,
|
|
79 0,
|
|
80 NULL },
|
|
81
|
|
82 { ngx_string("proxy_connect_timeout"),
|
|
83 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
84 ngx_conf_set_msec_slot,
|
|
85 NGX_HTTP_LOC_CONF_OFFSET,
|
|
86 offsetof(ngx_http_proxy_loc_conf_t, connect_timeout),
|
|
87 NULL },
|
|
88
|
|
89 { ngx_string("proxy_send_timeout"),
|
|
90 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
91 ngx_conf_set_msec_slot,
|
|
92 NGX_HTTP_LOC_CONF_OFFSET,
|
|
93 offsetof(ngx_http_proxy_loc_conf_t, send_timeout),
|
|
94 NULL },
|
|
95
|
2
|
96 { ngx_string("proxy_send_lowat"),
|
|
97 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
98 ngx_conf_set_size_slot,
|
|
99 NGX_HTTP_LOC_CONF_OFFSET,
|
|
100 offsetof(ngx_http_proxy_loc_conf_t, send_lowat),
|
|
101 &ngx_http_proxy_lowat_post },
|
|
102
|
0
|
103 { ngx_string("proxy_preserve_host"),
|
|
104 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
105 ngx_conf_set_flag_slot,
|
|
106 NGX_HTTP_LOC_CONF_OFFSET,
|
|
107 offsetof(ngx_http_proxy_loc_conf_t, preserve_host),
|
|
108 NULL },
|
|
109
|
6
|
110 { ngx_string("proxy_set_x_url"),
|
|
111 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
112 ngx_conf_set_flag_slot,
|
|
113 NGX_HTTP_LOC_CONF_OFFSET,
|
|
114 offsetof(ngx_http_proxy_loc_conf_t, set_x_url),
|
|
115 NULL },
|
|
116
|
0
|
117 { ngx_string("proxy_set_x_real_ip"),
|
|
118 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
119 ngx_conf_set_flag_slot,
|
|
120 NGX_HTTP_LOC_CONF_OFFSET,
|
|
121 offsetof(ngx_http_proxy_loc_conf_t, set_x_real_ip),
|
|
122 NULL },
|
|
123
|
34
|
124 { ngx_string("proxy_set_x_var"),
|
|
125 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
126 ngx_http_proxy_set_x_var,
|
|
127 NGX_HTTP_LOC_CONF_OFFSET,
|
|
128 0,
|
|
129 NULL },
|
|
130
|
0
|
131 { ngx_string("proxy_add_x_forwarded_for"),
|
|
132 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
133 ngx_conf_set_flag_slot,
|
|
134 NGX_HTTP_LOC_CONF_OFFSET,
|
|
135 offsetof(ngx_http_proxy_loc_conf_t, add_x_forwarded_for),
|
|
136 NULL },
|
|
137
|
|
138 { ngx_string("proxy_header_buffer_size"),
|
|
139 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
140 ngx_conf_set_size_slot,
|
|
141 NGX_HTTP_LOC_CONF_OFFSET,
|
|
142 offsetof(ngx_http_proxy_loc_conf_t, header_buffer_size),
|
|
143 NULL },
|
|
144
|
|
145 { ngx_string("proxy_read_timeout"),
|
|
146 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
147 ngx_conf_set_msec_slot,
|
|
148 NGX_HTTP_LOC_CONF_OFFSET,
|
|
149 offsetof(ngx_http_proxy_loc_conf_t, read_timeout),
|
|
150 NULL },
|
|
151
|
|
152 { ngx_string("proxy_buffers"),
|
|
153 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
154 ngx_conf_set_bufs_slot,
|
|
155 NGX_HTTP_LOC_CONF_OFFSET,
|
|
156 offsetof(ngx_http_proxy_loc_conf_t, bufs),
|
|
157 NULL },
|
|
158
|
|
159 { ngx_string("proxy_busy_buffers_size"),
|
|
160 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
161 ngx_conf_set_size_slot,
|
|
162 NGX_HTTP_LOC_CONF_OFFSET,
|
|
163 offsetof(ngx_http_proxy_loc_conf_t, busy_buffers_size),
|
|
164 NULL },
|
|
165
|
26
|
166 #if 0
|
0
|
167
|
|
168 { ngx_string("proxy_cache_path"),
|
|
169 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
170 ngx_conf_set_path_slot,
|
|
171 NGX_HTTP_LOC_CONF_OFFSET,
|
|
172 offsetof(ngx_http_proxy_loc_conf_t, cache_path),
|
26
|
173 (void *) ngx_http_cache_cleaner_handler },
|
0
|
174
|
|
175 #endif
|
|
176
|
|
177 { ngx_string("proxy_temp_path"),
|
|
178 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
179 ngx_conf_set_path_slot,
|
|
180 NGX_HTTP_LOC_CONF_OFFSET,
|
|
181 offsetof(ngx_http_proxy_loc_conf_t, temp_path),
|
|
182 (void *) ngx_garbage_collector_temp_handler },
|
|
183
|
16
|
184 { ngx_string("proxy_max_temp_file_size"),
|
|
185 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
186 ngx_conf_set_size_slot,
|
|
187 NGX_HTTP_LOC_CONF_OFFSET,
|
|
188 offsetof(ngx_http_proxy_loc_conf_t, max_temp_file_size),
|
|
189 NULL },
|
|
190
|
0
|
191 { ngx_string("proxy_temp_file_write_size"),
|
|
192 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
193 ngx_conf_set_size_slot,
|
|
194 NGX_HTTP_LOC_CONF_OFFSET,
|
|
195 offsetof(ngx_http_proxy_loc_conf_t, temp_file_write_size),
|
|
196 NULL },
|
|
197
|
|
198 { ngx_string("proxy_cache"),
|
|
199 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
200 ngx_conf_set_flag_slot,
|
|
201 NGX_HTTP_LOC_CONF_OFFSET,
|
|
202 offsetof(ngx_http_proxy_loc_conf_t, cache),
|
|
203 NULL },
|
|
204
|
|
205
|
|
206 { ngx_string("proxy_busy_lock"),
|
|
207 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE13,
|
|
208 ngx_http_set_busy_lock_slot,
|
|
209 NGX_HTTP_LOC_CONF_OFFSET,
|
|
210 offsetof(ngx_http_proxy_loc_conf_t, busy_lock),
|
|
211 NULL },
|
|
212
|
|
213
|
|
214 { ngx_string("proxy_pass_server"),
|
|
215 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
216 ngx_conf_set_flag_slot,
|
|
217 NGX_HTTP_LOC_CONF_OFFSET,
|
|
218 offsetof(ngx_http_proxy_loc_conf_t, pass_server),
|
|
219 NULL },
|
|
220
|
|
221 { ngx_string("proxy_pass_x_accel_expires"),
|
|
222 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
223 ngx_conf_set_flag_slot,
|
|
224 NGX_HTTP_LOC_CONF_OFFSET,
|
|
225 offsetof(ngx_http_proxy_loc_conf_t, pass_x_accel_expires),
|
|
226 NULL },
|
|
227
|
|
228 { ngx_string("proxy_ignore_expires"),
|
|
229 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
230 ngx_conf_set_flag_slot,
|
|
231 NGX_HTTP_LOC_CONF_OFFSET,
|
|
232 offsetof(ngx_http_proxy_loc_conf_t, ignore_expires),
|
|
233 NULL },
|
|
234
|
|
235 { ngx_string("proxy_lm_factor"),
|
|
236 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
237 ngx_conf_set_num_slot,
|
|
238 NGX_HTTP_LOC_CONF_OFFSET,
|
|
239 offsetof(ngx_http_proxy_loc_conf_t, lm_factor),
|
|
240 &ngx_http_proxy_lm_factor_bounds },
|
|
241
|
|
242 { ngx_string("proxy_default_expires"),
|
|
243 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
244 ngx_conf_set_sec_slot,
|
|
245 NGX_HTTP_LOC_CONF_OFFSET,
|
|
246 offsetof(ngx_http_proxy_loc_conf_t, default_expires),
|
|
247 NULL },
|
|
248
|
|
249 { ngx_string("proxy_next_upstream"),
|
|
250 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
|
|
251 ngx_conf_set_bitmask_slot,
|
|
252 NGX_HTTP_LOC_CONF_OFFSET,
|
|
253 offsetof(ngx_http_proxy_loc_conf_t, next_upstream),
|
|
254 &next_upstream_masks },
|
|
255
|
|
256 { ngx_string("proxy_use_stale"),
|
|
257 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_ANY,
|
|
258 ngx_conf_set_bitmask_slot,
|
|
259 NGX_HTTP_LOC_CONF_OFFSET,
|
|
260 offsetof(ngx_http_proxy_loc_conf_t, use_stale),
|
|
261 &use_stale_masks },
|
|
262
|
|
263 ngx_null_command
|
|
264 };
|
|
265
|
|
266
|
|
267 ngx_http_module_t ngx_http_proxy_module_ctx = {
|
28
|
268 ngx_http_proxy_add_log_formats, /* pre conf */
|
0
|
269
|
|
270 NULL, /* create main configuration */
|
|
271 NULL, /* init main configuration */
|
|
272
|
|
273 NULL, /* create server configuration */
|
|
274 NULL, /* merge server configuration */
|
|
275
|
|
276 ngx_http_proxy_create_loc_conf, /* create location configration */
|
|
277 ngx_http_proxy_merge_loc_conf /* merge location configration */
|
|
278 };
|
|
279
|
|
280
|
|
281 ngx_module_t ngx_http_proxy_module = {
|
|
282 NGX_MODULE,
|
|
283 &ngx_http_proxy_module_ctx, /* module context */
|
|
284 ngx_http_proxy_commands, /* module directives */
|
|
285 NGX_HTTP_MODULE, /* module type */
|
|
286 NULL, /* init module */
|
28
|
287 NULL /* init process */
|
0
|
288 };
|
|
289
|
|
290
|
|
291
|
|
292 static ngx_http_log_op_name_t ngx_http_proxy_log_fmt_ops[] = {
|
28
|
293 { ngx_string("proxy"), 0, NULL,
|
|
294 ngx_http_proxy_log_proxy_state_getlen,
|
|
295 ngx_http_proxy_log_proxy_state },
|
|
296
|
|
297 #if 0
|
22
|
298 { ngx_string("proxy_cache_state"), 0, ngx_http_proxy_log_cache_state },
|
|
299 { ngx_string("proxy_reason"), 0, ngx_http_proxy_log_reason },
|
28
|
300 #endif
|
|
301
|
|
302 { ngx_null_string, 0, NULL, NULL, NULL }
|
0
|
303 };
|
|
304
|
|
305
|
|
306
|
|
307 ngx_http_header_t ngx_http_proxy_headers_in[] = {
|
|
308 { ngx_string("Date"), offsetof(ngx_http_proxy_headers_in_t, date) },
|
|
309 { ngx_string("Server"), offsetof(ngx_http_proxy_headers_in_t, server) },
|
|
310
|
|
311 { ngx_string("Expires"), offsetof(ngx_http_proxy_headers_in_t, expires) },
|
|
312 { ngx_string("Cache-Control"),
|
|
313 offsetof(ngx_http_proxy_headers_in_t, cache_control) },
|
|
314 { ngx_string("ETag"), offsetof(ngx_http_proxy_headers_in_t, etag) },
|
|
315 { ngx_string("X-Accel-Expires"),
|
|
316 offsetof(ngx_http_proxy_headers_in_t, x_accel_expires) },
|
|
317
|
|
318 { ngx_string("Connection"),
|
|
319 offsetof(ngx_http_proxy_headers_in_t, connection) },
|
|
320 { ngx_string("Content-Type"),
|
|
321 offsetof(ngx_http_proxy_headers_in_t, content_type) },
|
|
322 { ngx_string("Content-Length"),
|
|
323 offsetof(ngx_http_proxy_headers_in_t, content_length) },
|
28
|
324
|
|
325 #if (NGX_HTTP_GZIP)
|
4
|
326 { ngx_string("Content-Encoding"),
|
|
327 offsetof(ngx_http_proxy_headers_in_t, content_encoding) },
|
28
|
328 #endif
|
|
329
|
0
|
330 { ngx_string("Last-Modified"),
|
|
331 offsetof(ngx_http_proxy_headers_in_t, last_modified) },
|
|
332 { ngx_string("Location"),
|
|
333 offsetof(ngx_http_proxy_headers_in_t, location) },
|
|
334 { ngx_string("Accept-Ranges"),
|
|
335 offsetof(ngx_http_proxy_headers_in_t, accept_ranges) },
|
|
336 { ngx_string("X-Pad"), offsetof(ngx_http_proxy_headers_in_t, x_pad) },
|
|
337
|
|
338 { ngx_null_string, 0 }
|
|
339 };
|
|
340
|
|
341
|
|
342 static ngx_str_t cache_states[] = {
|
|
343 ngx_string("PASS"),
|
|
344 ngx_string("BYPASS"),
|
|
345 ngx_string("AUTH"),
|
|
346 ngx_string("PGNC"),
|
|
347 ngx_string("MISS"),
|
|
348 ngx_string("EXPR"),
|
|
349 ngx_string("AGED"),
|
|
350 ngx_string("HIT")
|
|
351 };
|
|
352
|
|
353
|
|
354 static ngx_str_t cache_reasons[] = {
|
|
355 ngx_string("BPS"),
|
|
356 ngx_string("XAE"),
|
|
357 ngx_string("CTL"),
|
|
358 ngx_string("EXP"),
|
|
359 ngx_string("MVD"),
|
|
360 ngx_string("LMF"),
|
|
361 ngx_string("PDE")
|
|
362 };
|
|
363
|
|
364
|
52
|
365 #if (NGX_PCRE)
|
50
|
366 static ngx_str_t ngx_http_proxy_uri = ngx_string("/");
|
52
|
367 #endif
|
50
|
368
|
|
369
|
0
|
370 static ngx_int_t ngx_http_proxy_handler(ngx_http_request_t *r)
|
|
371 {
|
|
372 ngx_http_proxy_ctx_t *p;
|
|
373
|
50
|
374 p = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
|
|
375 if (p == NULL) {
|
|
376 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
377 }
|
|
378
|
|
379 ngx_http_set_ctx(r, p, ngx_http_proxy_module);
|
|
380
|
0
|
381
|
|
382 p->lcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
383 p->request = r;
|
|
384
|
|
385 /* TODO: we currently support reverse proxy only */
|
|
386 p->accel = 1;
|
|
387
|
26
|
388 if (ngx_array_init(&p->states, r->pool, p->lcf->peers->number,
|
|
389 sizeof(ngx_http_proxy_state_t)) == NGX_ERROR)
|
|
390 {
|
|
391 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
392 }
|
0
|
393
|
50
|
394 p->state = ngx_array_push(&p->states);
|
|
395 if (p->state == NULL) {
|
0
|
396 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
397 }
|
|
398
|
|
399 ngx_memzero(p->state, sizeof(ngx_http_proxy_state_t));
|
|
400
|
26
|
401 #if 0
|
0
|
402
|
|
403 if (!p->lcf->cache
|
|
404 || (r->method != NGX_HTTP_GET && r->method != NGX_HTTP_HEAD))
|
|
405 {
|
|
406 p->state->cache_state = NGX_HTTP_PROXY_CACHE_PASS;
|
|
407
|
|
408 } else if (r->bypass_cache) {
|
|
409 p->state->cache_state = NGX_HTTP_PROXY_CACHE_BYPASS;
|
|
410
|
|
411 } else if (r->headers_in.authorization) {
|
|
412 p->state->cache_state = NGX_HTTP_PROXY_CACHE_AUTH;
|
|
413
|
|
414 } else if (r->no_cache) {
|
|
415 p->state->cache_state = NGX_HTTP_PROXY_CACHE_PGNC;
|
|
416 p->cachable = 1;
|
|
417
|
|
418 } else {
|
|
419 p->cachable = 1;
|
|
420 }
|
|
421
|
|
422
|
|
423 if (p->state->cache_state != 0) {
|
|
424 return ngx_http_proxy_request_upstream(p);
|
|
425 }
|
|
426
|
26
|
427 return ngx_http_proxy_cache_get(p);
|
0
|
428
|
|
429 #else
|
|
430
|
|
431 p->state->cache_state = NGX_HTTP_PROXY_CACHE_PASS;
|
|
432
|
|
433 return ngx_http_proxy_request_upstream(p);
|
|
434
|
|
435 #endif
|
|
436 }
|
|
437
|
|
438
|
26
|
439 #if 0
|
|
440
|
|
441 static ngx_int_t ngx_http_proxy_cache_get(ngx_http_proxy_ctx_t *p)
|
|
442 {
|
|
443 u_char *last;
|
|
444 ngx_http_request_t *r;
|
|
445 ngx_http_cache_ctx_t ctx;
|
|
446 ngx_http_proxy_upstream_conf_t *u;
|
|
447
|
|
448 r = p->request;
|
|
449 u = p->lcf->upstream;
|
|
450
|
|
451 ctx.key.len = u->url.len + r->uri.len - u->location->len + r->args.len;
|
50
|
452 ctx.key.data = ngx_palloc(r->pool, ctx.key.len);
|
|
453 if (ctx.key.data == NULL) {
|
26
|
454 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
455 }
|
|
456
|
|
457 last = ngx_cpymem(ctx.key.data, u->url.data, u->url.len);
|
|
458
|
|
459 last = ngx_cpymem(last, r->uri.data + u->location->len,
|
|
460 r->uri.len - u->location->len);
|
|
461
|
|
462 if (r->args.len > 0) {
|
|
463 *(last++) = '?';
|
|
464 last = ngx_cpymem(last, r->args.data, r->args.len);
|
|
465 }
|
|
466
|
|
467 p->header_in = ngx_create_temp_buf(r->pool, p->lcf->header_buffer_size);
|
|
468 if (p->header_in == NULL) {
|
|
469 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
470 }
|
|
471 p->header_in->tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
472
|
|
473 ctx.buf = p->header_in;
|
|
474 ctx.path = p->lcf->cache_path;
|
|
475 ctx.file = 1;
|
|
476 ctx.primary = 1;
|
|
477
|
|
478 ngx_http_cache_get(r, &ctx);
|
|
479
|
|
480 return ngx_http_proxy_request_upstream(p);
|
|
481 }
|
|
482
|
|
483 #endif
|
|
484
|
|
485
|
0
|
486 void ngx_http_proxy_check_broken_connection(ngx_event_t *ev)
|
|
487 {
|
|
488 int n;
|
|
489 char buf[1];
|
|
490 ngx_err_t err;
|
|
491 ngx_connection_t *c;
|
|
492 ngx_http_request_t *r;
|
|
493 ngx_http_proxy_ctx_t *p;
|
|
494
|
|
495 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ev->log, 0,
|
|
496 "http proxy check client, write event:%d", ev->write);
|
|
497
|
18
|
498 #if (NGX_HAVE_KQUEUE)
|
0
|
499
|
4
|
500 if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
|
0
|
501
|
|
502 if (!ev->pending_eof) {
|
|
503 return;
|
|
504 }
|
|
505
|
|
506 c = ev->data;
|
|
507 r = c->data;
|
|
508 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
509
|
|
510 ev->eof = 1;
|
|
511
|
|
512 if (ev->kq_errno) {
|
|
513 ev->error = 1;
|
|
514 }
|
|
515
|
|
516 if (!p->cachable && p->upstream->peer.connection) {
|
|
517 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
|
|
518 "kevent() reported that client closed "
|
|
519 "prematurely connection, "
|
|
520 "so upstream connection is closed too");
|
|
521 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
522 return;
|
|
523 }
|
|
524
|
|
525 ngx_log_error(NGX_LOG_INFO, ev->log, ev->kq_errno,
|
|
526 "kevent() reported that client closed "
|
|
527 "prematurely connection");
|
|
528
|
|
529 if (p->upstream == NULL || p->upstream->peer.connection == NULL) {
|
|
530 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
531 }
|
|
532
|
|
533 return;
|
|
534 }
|
|
535
|
|
536 #endif
|
|
537
|
|
538 c = ev->data;
|
|
539 r = c->data;
|
|
540 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
541
|
|
542 n = recv(c->fd, buf, 1, MSG_PEEK);
|
|
543
|
|
544 err = ngx_socket_errno;
|
|
545
|
|
546 /*
|
|
547 * we do not need to disable the write event because
|
|
548 * that event has NGX_USE_CLEAR_EVENT type
|
|
549 */
|
|
550
|
|
551 if (ev->write && (n >= 0 || err == NGX_EAGAIN)) {
|
|
552 return;
|
|
553 }
|
|
554
|
|
555 if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && ev->active) {
|
|
556 if (ngx_del_event(ev, NGX_READ_EVENT, 0) == NGX_ERROR) {
|
|
557 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
558 }
|
|
559 }
|
|
560
|
|
561 if (n > 0) {
|
|
562 return;
|
|
563 }
|
|
564
|
|
565 ev->eof = 1;
|
|
566
|
|
567 if (n == -1) {
|
|
568 if (err == NGX_EAGAIN) {
|
|
569 return;
|
|
570 }
|
|
571
|
|
572 ev->error = 1;
|
|
573
|
|
574 } else {
|
|
575 /* n == 0 */
|
|
576 err = 0;
|
|
577 }
|
|
578
|
|
579 if (!p->cachable && p->upstream->peer.connection) {
|
|
580 ngx_log_error(NGX_LOG_INFO, ev->log, err,
|
|
581 "client closed prematurely connection, "
|
|
582 "so upstream connection is closed too");
|
|
583 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
584 return;
|
|
585 }
|
|
586
|
|
587 ngx_log_error(NGX_LOG_INFO, ev->log, err,
|
|
588 "client closed prematurely connection");
|
|
589
|
|
590 if (p->upstream == NULL || p->upstream->peer.connection == NULL) {
|
|
591 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
592 }
|
|
593 }
|
|
594
|
|
595
|
|
596 void ngx_http_proxy_busy_lock_handler(ngx_event_t *rev)
|
|
597 {
|
|
598 ngx_connection_t *c;
|
|
599 ngx_http_request_t *r;
|
|
600 ngx_http_proxy_ctx_t *p;
|
|
601
|
|
602 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0, "http proxy busy lock");
|
|
603
|
|
604 c = rev->data;
|
|
605 r = c->data;
|
|
606 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
607 p->action = "waiting upstream in busy lock";
|
|
608
|
|
609 if (p->request->connection->write->eof) {
|
|
610 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
611 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
612 return;
|
|
613 }
|
|
614
|
|
615 if (rev->timedout) {
|
|
616 rev->timedout = 0;
|
|
617 p->busy_lock.time++;
|
|
618 p->state->bl_time = p->busy_lock.time;
|
|
619
|
|
620 #if (NGX_HTTP_FILE_CACHE)
|
|
621
|
|
622 if (p->state->cache_state < NGX_HTTP_PROXY_CACHE_MISS) {
|
|
623 ngx_http_proxy_upstream_busy_lock(p);
|
|
624
|
|
625 } else {
|
|
626 ngx_http_proxy_cache_busy_lock(p);
|
|
627 }
|
|
628 #else
|
|
629
|
|
630 ngx_http_proxy_upstream_busy_lock(p);
|
|
631
|
|
632 #endif
|
|
633
|
|
634 return;
|
|
635 }
|
|
636
|
|
637 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
|
|
638 "http proxy: client sent while busy lock");
|
|
639
|
|
640 /*
|
|
641 * TODO: kevent() notify about error, otherwise we need to
|
|
642 * call ngx_peek(): recv(MSG_PEEK) to get errno. THINK about aio.
|
|
643 * if there's no error we need to disable event.
|
|
644 */
|
|
645
|
|
646 #if 0
|
18
|
647 #if (NGX_HAVE_KQUEUE)
|
0
|
648
|
18
|
649 if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && rev->kq_eof) {
|
0
|
650 ngx_http_busy_unlock(p->lcf->busy_lock, &p->busy_lock);
|
|
651
|
|
652 ngx_del_timer(rev);
|
|
653
|
|
654 ngx_log_error(NGX_LOG_ERR, c->log, rev->kq_errno,
|
|
655 "client() closed connection");
|
|
656
|
|
657 if (ngx_del_event(rev, NGX_READ_EVENT, NGX_CLOSE_EVENT) == NGX_ERROR) {
|
|
658 ngx_http_proxy_finalize_request(p, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
659 return;
|
|
660 }
|
|
661
|
|
662 ngx_http_proxy_finalize_request(p, NGX_HTTP_CLIENT_CLOSED_REQUEST);
|
|
663 return;
|
|
664 }
|
|
665
|
|
666 #endif
|
|
667 #endif
|
|
668
|
|
669 }
|
|
670
|
|
671
|
|
672 void ngx_http_proxy_finalize_request(ngx_http_proxy_ctx_t *p, int rc)
|
|
673 {
|
|
674 ngx_http_request_t *r;
|
|
675
|
|
676 r = p->request;
|
|
677
|
|
678 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
679 "finalize http proxy request");
|
|
680
|
|
681 if (p->upstream && p->upstream->peer.connection) {
|
|
682 ngx_http_proxy_close_connection(p);
|
|
683 }
|
|
684
|
|
685 if (p->header_sent
|
|
686 && (rc == NGX_ERROR || rc >= NGX_HTTP_SPECIAL_RESPONSE))
|
|
687 {
|
|
688 rc = 0;
|
|
689 }
|
|
690
|
|
691 if (p->saved_ctx) {
|
|
692 r->connection->log->data = p->saved_ctx;
|
|
693 r->connection->log->handler = p->saved_handler;
|
|
694 }
|
|
695
|
|
696 if (p->upstream && p->upstream->event_pipe) {
|
|
697 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
698 "http proxy temp fd: %d",
|
|
699 p->upstream->event_pipe->temp_file->file.fd);
|
|
700 }
|
|
701
|
|
702 if (p->cache) {
|
|
703 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
704 "http proxy cache fd: %d",
|
|
705 p->cache->ctx.file.fd);
|
|
706 }
|
|
707
|
|
708 if (p->upstream && p->upstream->event_pipe) {
|
|
709 r->file.fd = p->upstream->event_pipe->temp_file->file.fd;
|
|
710
|
|
711 } else if (p->cache) {
|
|
712 r->file.fd = p->cache->ctx.file.fd;
|
|
713 }
|
|
714
|
|
715 if (rc == 0 && r->main == NULL) {
|
|
716 rc = ngx_http_send_last(r);
|
|
717 }
|
|
718
|
|
719 ngx_http_finalize_request(r, rc);
|
|
720 }
|
|
721
|
|
722
|
|
723 void ngx_http_proxy_close_connection(ngx_http_proxy_ctx_t *p)
|
|
724 {
|
|
725 ngx_socket_t fd;
|
|
726 ngx_connection_t *c;
|
|
727
|
|
728 c = p->upstream->peer.connection;
|
|
729 p->upstream->peer.connection = NULL;
|
|
730
|
|
731 if (p->lcf->busy_lock) {
|
|
732 p->lcf->busy_lock->busy--;
|
|
733 }
|
|
734
|
|
735 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
736 "http proxy close connection: %d", c->fd);
|
|
737
|
|
738 if (c->fd == -1) {
|
|
739 #if 0
|
|
740 ngx_log_error(NGX_LOG_ALERT, c->log, 0, "connection already closed");
|
|
741 #endif
|
|
742 return;
|
|
743 }
|
|
744
|
|
745 if (c->read->timer_set) {
|
|
746 ngx_del_timer(c->read);
|
|
747 }
|
|
748
|
|
749 if (c->write->timer_set) {
|
|
750 ngx_del_timer(c->write);
|
|
751 }
|
|
752
|
|
753 /* TODO: move connection to the connection pool */
|
|
754
|
|
755 if (ngx_del_conn) {
|
|
756 ngx_del_conn(c, NGX_CLOSE_EVENT);
|
|
757
|
|
758 } else {
|
|
759 if (c->read->active || c->read->disabled) {
|
|
760 ngx_del_event(c->read, NGX_READ_EVENT, NGX_CLOSE_EVENT);
|
|
761 }
|
|
762
|
|
763 if (c->write->active || c->read->disabled) {
|
|
764 ngx_del_event(c->write, NGX_WRITE_EVENT, NGX_CLOSE_EVENT);
|
|
765 }
|
|
766 }
|
|
767
|
|
768 /*
|
|
769 * we have to clean the connection information before the closing
|
|
770 * because another thread may reopen the same file descriptor
|
|
771 * before we clean the connection
|
|
772 */
|
|
773
|
|
774 if (ngx_mutex_lock(ngx_posted_events_mutex) == NGX_OK) {
|
|
775
|
|
776 if (c->read->prev) {
|
|
777 ngx_delete_posted_event(c->read);
|
|
778 }
|
|
779
|
|
780 if (c->write->prev) {
|
|
781 ngx_delete_posted_event(c->write);
|
|
782 }
|
|
783
|
|
784 c->read->closed = 1;
|
|
785 c->write->closed = 1;
|
|
786
|
|
787 ngx_mutex_unlock(ngx_posted_events_mutex);
|
|
788 }
|
|
789
|
|
790 fd = c->fd;
|
|
791 c->fd = (ngx_socket_t) -1;
|
|
792 c->data = NULL;
|
|
793
|
|
794 if (ngx_close_socket(fd) == -1) {
|
|
795 ngx_log_error(NGX_LOG_ALERT, c->log, ngx_socket_errno,
|
|
796 ngx_close_socket_n " failed");
|
|
797 }
|
|
798 }
|
|
799
|
|
800
|
32
|
801 u_char *ngx_http_proxy_log_error(ngx_log_t *log, u_char *buf, size_t len)
|
0
|
802 {
|
10
|
803 u_char *p;
|
|
804 ngx_int_t escape;
|
|
805 ngx_str_t uri;
|
|
806 ngx_http_request_t *r;
|
|
807 ngx_peer_connection_t *peer;
|
32
|
808 ngx_http_proxy_log_ctx_t *ctx;
|
10
|
809 ngx_http_proxy_upstream_conf_t *uc;
|
0
|
810
|
32
|
811 ctx = log->data;
|
0
|
812 r = ctx->proxy->request;
|
10
|
813 uc = ctx->proxy->lcf->upstream;
|
0
|
814 peer = &ctx->proxy->upstream->peer;
|
|
815
|
10
|
816 p = ngx_snprintf(buf, len,
|
52
|
817 " while %s, client: %V, host: %V, URL: \"%V\","
|
|
818 " upstream: http://%V%s%V",
|
10
|
819 ctx->proxy->action,
|
|
820 &r->connection->addr_text,
|
52
|
821 &r->server_name,
|
10
|
822 &r->unparsed_uri,
|
28
|
823 &peer->peers->peer[peer->cur_peer].name,
|
|
824 ctx->proxy->lcf->upstream->uri_separator,
|
10
|
825 &ctx->proxy->lcf->upstream->uri);
|
|
826 len -= p - buf;
|
|
827 buf = p;
|
|
828
|
|
829 if (r->quoted_uri) {
|
|
830 escape = 2 * ngx_escape_uri(NULL, r->uri.data + uc->location->len,
|
|
831 r->uri.len - uc->location->len,
|
|
832 NGX_ESCAPE_URI);
|
|
833 } else {
|
|
834 escape = 0;
|
|
835 }
|
|
836
|
|
837 if (escape) {
|
|
838 if (len >= r->uri.len - uc->location->len + escape) {
|
|
839
|
|
840 ngx_escape_uri(buf, r->uri.data + uc->location->len,
|
|
841 r->uri.len - uc->location->len, NGX_ESCAPE_URI);
|
|
842
|
|
843 buf += r->uri.len - uc->location->len + escape;
|
|
844
|
|
845 if (r->args.len == 0) {
|
|
846 return buf;
|
|
847 }
|
|
848
|
|
849 len -= r->uri.len - uc->location->len + escape;
|
|
850
|
|
851 return ngx_snprintf(buf, len, "?%V", &r->args);
|
|
852 }
|
|
853
|
|
854 p = ngx_palloc(r->pool, r->uri.len - uc->location->len + escape);
|
|
855 if (p == NULL) {
|
|
856 return buf;
|
|
857 }
|
|
858
|
|
859 ngx_escape_uri(p, r->uri.data + uc->location->len,
|
|
860 r->uri.len - uc->location->len, NGX_ESCAPE_URI);
|
|
861
|
|
862 uri.len = r->uri.len - uc->location->len + escape;
|
|
863 uri.data = p;
|
|
864
|
|
865 } else {
|
|
866 uri.len = r->uri.len - uc->location->len;
|
|
867 uri.data = r->uri.data + uc->location->len;
|
|
868
|
|
869 }
|
|
870
|
|
871 return ngx_snprintf(buf, len, "%V%s%V",
|
|
872 &uri, r->args.len ? "?" : "", &r->args);
|
0
|
873 }
|
|
874
|
|
875
|
28
|
876 static size_t ngx_http_proxy_log_proxy_state_getlen(ngx_http_request_t *r,
|
|
877 uintptr_t data)
|
|
878 {
|
|
879 ngx_http_proxy_ctx_t *p;
|
|
880
|
|
881 p = ngx_http_get_module_err_ctx(r, ngx_http_proxy_module);
|
|
882
|
|
883 if (p == NULL) {
|
|
884 return 1;
|
|
885 }
|
|
886
|
|
887 return p->states.nelts * /* STUB */ 100;
|
|
888 }
|
|
889
|
|
890
|
0
|
891 static u_char *ngx_http_proxy_log_proxy_state(ngx_http_request_t *r,
|
28
|
892 u_char *buf,
|
|
893 ngx_http_log_op_t *op)
|
0
|
894 {
|
22
|
895 ngx_uint_t i;
|
|
896 ngx_http_proxy_ctx_t *p;
|
|
897 ngx_http_proxy_state_t *state;
|
0
|
898
|
|
899 p = ngx_http_get_module_err_ctx(r, ngx_http_proxy_module);
|
|
900
|
|
901 if (p == NULL) {
|
|
902 *buf = '-';
|
|
903 return buf + 1;
|
|
904 }
|
|
905
|
22
|
906 i = 0;
|
|
907 state = p->states.elts;
|
0
|
908
|
22
|
909 for ( ;; ) {
|
|
910 if (state[i].cache_state == 0) {
|
|
911 *buf++ = '-';
|
0
|
912
|
22
|
913 } else {
|
|
914 buf = ngx_cpymem(buf, cache_states[state[i].cache_state - 1].data,
|
|
915 cache_states[state[i].cache_state - 1].len);
|
|
916 }
|
0
|
917
|
22
|
918 *buf++ = '/';
|
|
919
|
|
920 if (state[i].expired == 0) {
|
|
921 *buf++ = '-';
|
0
|
922
|
22
|
923 } else {
|
|
924 buf = ngx_sprintf(buf, "%T", state[i].expired);
|
|
925 }
|
|
926
|
|
927 *buf++ = '/';
|
|
928
|
|
929 if (state[i].bl_time == 0) {
|
|
930 *buf++ = '-';
|
0
|
931
|
22
|
932 } else {
|
|
933 buf = ngx_sprintf(buf, "%T", state[i].bl_time);
|
|
934 }
|
0
|
935
|
22
|
936 *buf++ = '/';
|
|
937
|
|
938 *buf++ = '*';
|
|
939
|
|
940 *buf++ = ' ';
|
0
|
941
|
22
|
942 if (state[i].status == 0) {
|
|
943 *buf++ = '-';
|
|
944
|
|
945 } else {
|
|
946 buf = ngx_sprintf(buf, "%ui", state[i].status);
|
|
947 }
|
0
|
948
|
22
|
949 *buf++ = '/';
|
|
950
|
|
951 if (state[i].reason == 0) {
|
|
952 *buf++ = '-';
|
|
953
|
|
954 } else {
|
|
955 buf = ngx_cpymem(buf, cache_reasons[state[i].reason - 1].data,
|
|
956 cache_reasons[state[i].reason - 1].len);
|
|
957 }
|
0
|
958
|
22
|
959 *buf++ = '/';
|
|
960
|
|
961 if (state[i].reason < NGX_HTTP_PROXY_CACHE_XAE) {
|
|
962 *buf++ = '-';
|
|
963
|
|
964 } else {
|
|
965 buf = ngx_sprintf(buf, "%T", state[i].expires);
|
|
966 }
|
0
|
967
|
22
|
968 *buf++ = ' ';
|
|
969 *buf++ = '*';
|
|
970
|
|
971 if (++i == p->states.nelts) {
|
|
972 return buf;
|
|
973 }
|
|
974
|
|
975 *buf++ = ',';
|
|
976 *buf++ = ' ';
|
0
|
977 }
|
|
978 }
|
|
979
|
|
980
|
28
|
981 #if 0
|
|
982
|
0
|
983 static u_char *ngx_http_proxy_log_cache_state(ngx_http_request_t *r,
|
|
984 u_char *buf, uintptr_t data)
|
|
985 {
|
22
|
986 ngx_uint_t i;
|
|
987 ngx_http_proxy_ctx_t *p;
|
|
988 ngx_http_proxy_state_t *state;
|
0
|
989
|
|
990 p = ngx_http_get_module_err_ctx(r, ngx_http_proxy_module);
|
|
991
|
|
992 if (p == NULL || p->state->cache_state == 0) {
|
22
|
993 if (buf == NULL) {
|
|
994 return (u_char *) 1;
|
|
995 }
|
|
996
|
0
|
997 *buf = '-';
|
|
998 return buf + 1;
|
|
999 }
|
|
1000
|
22
|
1001 if (buf == NULL) {
|
|
1002 /* find the request line length */
|
|
1003 return (u_char *) (p->states.nelts * sizeof("BYPASS") - 1);
|
|
1004 }
|
|
1005
|
|
1006 i = 0;
|
|
1007 state = p->states.elts;
|
|
1008
|
|
1009 for ( ;; ) {
|
|
1010 buf = ngx_cpymem(buf, cache_states[state[i].cache_state - 1].data,
|
|
1011 cache_states[state[i].cache_state - 1].len);
|
|
1012
|
|
1013 if (++i == p->states.nelts) {
|
|
1014 return buf;
|
|
1015 }
|
|
1016
|
|
1017 *buf++ = ',';
|
|
1018 *buf++ = ' ';
|
|
1019 }
|
0
|
1020 }
|
|
1021
|
|
1022
|
|
1023 static u_char *ngx_http_proxy_log_reason(ngx_http_request_t *r, u_char *buf,
|
|
1024 uintptr_t data)
|
|
1025 {
|
22
|
1026 ngx_uint_t i;
|
|
1027 ngx_http_proxy_ctx_t *p;
|
|
1028 ngx_http_proxy_state_t *state;
|
0
|
1029
|
|
1030 p = ngx_http_get_module_err_ctx(r, ngx_http_proxy_module);
|
|
1031
|
|
1032 if (p == NULL || p->state->reason == 0) {
|
22
|
1033 if (buf == NULL) {
|
|
1034 return (u_char *) 1;
|
|
1035 }
|
|
1036
|
0
|
1037 *buf = '-';
|
|
1038 return buf + 1;
|
|
1039 }
|
|
1040
|
22
|
1041 if (buf == NULL) {
|
|
1042 /* find the request line length */
|
|
1043 return (u_char *) (p->states.nelts * sizeof("BPS") - 1);
|
|
1044 }
|
|
1045
|
|
1046 i = 0;
|
|
1047 state = p->states.elts;
|
|
1048
|
|
1049 for ( ;; ) {
|
|
1050 buf = ngx_cpymem(buf, cache_reasons[state[i].reason - 1].data,
|
|
1051 cache_reasons[state[i].reason - 1].len);
|
|
1052
|
|
1053 if (++i == p->states.nelts) {
|
|
1054 return buf;
|
|
1055 }
|
|
1056
|
|
1057 *buf++ = ',';
|
|
1058 *buf++ = ' ';
|
|
1059 }
|
0
|
1060 }
|
|
1061
|
28
|
1062 #endif
|
0
|
1063
|
28
|
1064
|
|
1065 static ngx_int_t ngx_http_proxy_add_log_formats(ngx_conf_t *cf)
|
0
|
1066 {
|
|
1067 ngx_http_log_op_name_t *op;
|
|
1068
|
|
1069 for (op = ngx_http_proxy_log_fmt_ops; op->name.len; op++) { /* void */ }
|
28
|
1070 op->run = NULL;
|
0
|
1071
|
28
|
1072 for (op = ngx_http_log_fmt_ops; op->run; op++) {
|
0
|
1073 if (op->name.len == 0) {
|
28
|
1074 op = (ngx_http_log_op_name_t *) op->run;
|
0
|
1075 }
|
|
1076 }
|
|
1077
|
28
|
1078 op->run = (ngx_http_log_op_run_pt) ngx_http_proxy_log_fmt_ops;
|
0
|
1079
|
|
1080 return NGX_OK;
|
|
1081 }
|
|
1082
|
|
1083
|
|
1084 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
|
|
1085 {
|
|
1086 ngx_http_proxy_loc_conf_t *conf;
|
|
1087
|
50
|
1088 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
|
|
1089 if (conf == NULL) {
|
34
|
1090 return NGX_CONF_ERROR;
|
|
1091 }
|
0
|
1092
|
28
|
1093 /*
|
|
1094 * set by ngx_pcalloc():
|
|
1095 *
|
|
1096 * conf->bufs.num = 0;
|
|
1097 * conf->path = NULL;
|
|
1098 * conf->next_upstream = 0;
|
|
1099 * conf->use_stale = 0;
|
|
1100 * conf->upstreams = NULL;
|
|
1101 * conf->peers = NULL;
|
|
1102 * conf->cache_path = NULL;
|
|
1103 * conf->temp_path = NULL;
|
34
|
1104 * conf->x_vars;
|
28
|
1105 * conf->busy_lock = NULL;
|
|
1106 */
|
0
|
1107
|
|
1108 conf->connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
1109 conf->send_timeout = NGX_CONF_UNSET_MSEC;
|
2
|
1110 conf->send_lowat = NGX_CONF_UNSET_SIZE;
|
0
|
1111
|
|
1112 conf->preserve_host = NGX_CONF_UNSET;
|
6
|
1113 conf->set_x_url = NGX_CONF_UNSET;
|
0
|
1114 conf->set_x_real_ip = NGX_CONF_UNSET;
|
|
1115 conf->add_x_forwarded_for = NGX_CONF_UNSET;
|
|
1116
|
|
1117 conf->header_buffer_size = NGX_CONF_UNSET_SIZE;
|
|
1118 conf->read_timeout = NGX_CONF_UNSET_MSEC;
|
|
1119 conf->busy_buffers_size = NGX_CONF_UNSET_SIZE;
|
|
1120
|
16
|
1121 conf->max_temp_file_size = NGX_CONF_UNSET_SIZE;
|
0
|
1122 conf->temp_file_write_size = NGX_CONF_UNSET_SIZE;
|
|
1123
|
|
1124 /* "proxy_cyclic_temp_file" is disabled */
|
|
1125 conf->cyclic_temp_file = 0;
|
|
1126
|
|
1127 conf->cache = NGX_CONF_UNSET;
|
|
1128
|
|
1129 conf->pass_server = NGX_CONF_UNSET;
|
|
1130 conf->pass_x_accel_expires = NGX_CONF_UNSET;
|
|
1131 conf->ignore_expires = NGX_CONF_UNSET;
|
|
1132 conf->lm_factor = NGX_CONF_UNSET;
|
|
1133 conf->default_expires = NGX_CONF_UNSET;
|
|
1134
|
|
1135 return conf;
|
|
1136 }
|
|
1137
|
|
1138
|
|
1139 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
|
|
1140 void *parent, void *child)
|
|
1141 {
|
|
1142 ngx_http_proxy_loc_conf_t *prev = parent;
|
|
1143 ngx_http_proxy_loc_conf_t *conf = child;
|
|
1144
|
|
1145 size_t size;
|
|
1146
|
|
1147 ngx_conf_merge_msec_value(conf->connect_timeout,
|
|
1148 prev->connect_timeout, 60000);
|
|
1149 ngx_conf_merge_msec_value(conf->send_timeout, prev->send_timeout, 60000);
|
2
|
1150 ngx_conf_merge_size_value(conf->send_lowat, prev->send_lowat, 0);
|
0
|
1151
|
|
1152 ngx_conf_merge_value(conf->preserve_host, prev->preserve_host, 0);
|
6
|
1153 ngx_conf_merge_value(conf->set_x_url, prev->set_x_url, 0);
|
0
|
1154 ngx_conf_merge_value(conf->set_x_real_ip, prev->set_x_real_ip, 0);
|
|
1155 ngx_conf_merge_value(conf->add_x_forwarded_for,
|
|
1156 prev->add_x_forwarded_for, 0);
|
|
1157
|
|
1158 ngx_conf_merge_msec_value(conf->read_timeout, prev->read_timeout, 60000);
|
|
1159
|
|
1160 ngx_conf_merge_size_value(conf->header_buffer_size,
|
|
1161 prev->header_buffer_size, (size_t) ngx_pagesize);
|
|
1162
|
|
1163 ngx_conf_merge_bufs_value(conf->bufs, prev->bufs, 8, ngx_pagesize);
|
|
1164
|
|
1165 if (conf->bufs.num < 2) {
|
|
1166 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1167 "there must be at least 2 \"proxy_buffers\"");
|
|
1168 return NGX_CONF_ERROR;
|
|
1169 }
|
|
1170
|
|
1171 size = conf->header_buffer_size;
|
|
1172 if (size < conf->bufs.size) {
|
|
1173 size = conf->bufs.size;
|
|
1174 }
|
|
1175
|
|
1176
|
|
1177 ngx_conf_merge_size_value(conf->busy_buffers_size,
|
|
1178 prev->busy_buffers_size, NGX_CONF_UNSET_SIZE);
|
|
1179
|
|
1180 if (conf->busy_buffers_size == NGX_CONF_UNSET_SIZE) {
|
|
1181 conf->busy_buffers_size = 2 * size;
|
|
1182
|
|
1183 } else if (conf->busy_buffers_size < size) {
|
|
1184 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1185 "\"proxy_busy_buffers_size\" must be equal or bigger than "
|
|
1186 "maximum of the value of \"proxy_header_buffer_size\" and "
|
|
1187 "one of the \"proxy_buffers\"");
|
|
1188
|
|
1189 return NGX_CONF_ERROR;
|
|
1190
|
|
1191 } else if (conf->busy_buffers_size > (conf->bufs.num - 1) * conf->bufs.size)
|
|
1192 {
|
|
1193 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1194 "\"proxy_busy_buffers_size\" must be less than "
|
|
1195 "the size of all \"proxy_buffers\" minus one buffer");
|
|
1196
|
|
1197 return NGX_CONF_ERROR;
|
|
1198 }
|
|
1199
|
|
1200
|
|
1201 ngx_conf_merge_size_value(conf->temp_file_write_size,
|
|
1202 prev->temp_file_write_size, NGX_CONF_UNSET_SIZE);
|
|
1203
|
|
1204 if (conf->temp_file_write_size == NGX_CONF_UNSET_SIZE) {
|
|
1205 conf->temp_file_write_size = 2 * size;
|
|
1206
|
|
1207 } else if (conf->temp_file_write_size < size) {
|
|
1208 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1209 "\"proxy_temp_file_write_size\" must be equal or bigger than "
|
|
1210 "maximum of the value of \"proxy_header_buffer_size\" and "
|
|
1211 "one of the \"proxy_buffers\"");
|
|
1212
|
|
1213 return NGX_CONF_ERROR;
|
|
1214 }
|
|
1215
|
|
1216
|
|
1217 ngx_conf_merge_size_value(conf->max_temp_file_size,
|
|
1218 prev->max_temp_file_size, NGX_CONF_UNSET_SIZE);
|
|
1219
|
|
1220 if (conf->max_temp_file_size == NGX_CONF_UNSET_SIZE) {
|
16
|
1221
|
|
1222 /*
|
|
1223 * "proxy_max_temp_file_size" is set to 1G for reverse proxy,
|
|
1224 * it should be much less in the generic proxy
|
|
1225 */
|
|
1226
|
|
1227 conf->max_temp_file_size = 1024 * 1024 * 1024;
|
0
|
1228
|
16
|
1229 #if 0
|
|
1230 conf->max_temp_file_size = 2 * size;
|
|
1231 #endif
|
|
1232
|
|
1233
|
|
1234 } else if (conf->max_temp_file_size != 0
|
|
1235 && conf->max_temp_file_size < size)
|
|
1236 {
|
0
|
1237 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
16
|
1238 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
|
|
1239 "the temporary files usage or must be equal or bigger than "
|
0
|
1240 "maximum of the value of \"proxy_header_buffer_size\" and "
|
|
1241 "one of the \"proxy_buffers\"");
|
|
1242
|
|
1243 return NGX_CONF_ERROR;
|
|
1244 }
|
|
1245
|
|
1246
|
|
1247 ngx_conf_merge_bitmask_value(conf->next_upstream, prev->next_upstream,
|
|
1248 (NGX_CONF_BITMASK_SET
|
|
1249 |NGX_HTTP_PROXY_FT_ERROR
|
|
1250 |NGX_HTTP_PROXY_FT_TIMEOUT));
|
|
1251
|
|
1252 ngx_conf_merge_bitmask_value(conf->use_stale, prev->use_stale,
|
|
1253 NGX_CONF_BITMASK_SET);
|
|
1254
|
28
|
1255 #if 0
|
0
|
1256 ngx_conf_merge_path_value(conf->cache_path, prev->cache_path,
|
28
|
1257 NGX_HTTP_PROXY_CACHE_PATH, 1, 2, 0,
|
|
1258 ngx_garbage_collector_temp_handler, cf);
|
|
1259 #endif
|
0
|
1260
|
|
1261 ngx_conf_merge_path_value(conf->temp_path, prev->temp_path,
|
28
|
1262 NGX_HTTP_PROXY_TEMP_PATH, 1, 2, 0,
|
|
1263 ngx_garbage_collector_temp_handler, cf);
|
0
|
1264
|
|
1265 ngx_conf_merge_value(conf->cache, prev->cache, 0);
|
|
1266
|
|
1267
|
|
1268 /* conf->cache must be merged */
|
|
1269
|
|
1270 if (conf->busy_lock == NULL) {
|
|
1271 conf->busy_lock = prev->busy_lock;
|
|
1272 }
|
|
1273
|
|
1274 if (conf->busy_lock && conf->cache && conf->busy_lock->md5 == NULL) {
|
|
1275
|
|
1276 /* ngx_calloc_shared() */
|
|
1277 conf->busy_lock->md5_mask =
|
|
1278 ngx_pcalloc(cf->pool, (conf->busy_lock->max_busy + 7) / 8);
|
|
1279 if (conf->busy_lock->md5_mask == NULL) {
|
|
1280 return NGX_CONF_ERROR;
|
|
1281 }
|
|
1282
|
|
1283 /* 16 bytes are 128 bits of the md5 */
|
|
1284
|
|
1285 /* ngx_alloc_shared() */
|
|
1286 conf->busy_lock->md5 = ngx_palloc(cf->pool,
|
|
1287 16 * conf->busy_lock->max_busy);
|
|
1288 if (conf->busy_lock->md5 == NULL) {
|
|
1289 return NGX_CONF_ERROR;
|
|
1290 }
|
|
1291 }
|
|
1292
|
|
1293
|
|
1294 ngx_conf_merge_value(conf->pass_server, prev->pass_server, 0);
|
|
1295 ngx_conf_merge_value(conf->pass_x_accel_expires,
|
|
1296 prev->pass_x_accel_expires, 0);
|
|
1297 ngx_conf_merge_value(conf->ignore_expires, prev->ignore_expires, 0);
|
|
1298 ngx_conf_merge_value(conf->lm_factor, prev->lm_factor, 0);
|
|
1299 ngx_conf_merge_sec_value(conf->default_expires, prev->default_expires, 0);
|
|
1300
|
36
|
1301 if (conf->x_vars == NULL) {
|
|
1302 conf->x_vars = prev->x_vars;
|
|
1303 }
|
|
1304
|
0
|
1305 return NULL;
|
|
1306 }
|
|
1307
|
|
1308
|
|
1309 static char *ngx_http_proxy_set_pass(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1310 void *conf)
|
|
1311 {
|
|
1312 ngx_http_proxy_loc_conf_t *lcf = conf;
|
|
1313
|
28
|
1314 ngx_str_t *value, *url;
|
|
1315 ngx_inet_upstream_t inet_upstream;
|
|
1316 ngx_http_core_loc_conf_t *clcf;
|
|
1317 #if (NGX_HAVE_UNIX_DOMAIN)
|
|
1318 ngx_unix_domain_upstream_t unix_upstream;
|
|
1319 #endif
|
0
|
1320
|
|
1321 value = cf->args->elts;
|
|
1322
|
28
|
1323 url = &value[1];
|
|
1324
|
|
1325 if (ngx_strncasecmp(url->data, "http://", 7) != 0) {
|
2
|
1326 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
|
|
1327 return NGX_CONF_ERROR;
|
0
|
1328 }
|
|
1329
|
2
|
1330 lcf->upstream = ngx_pcalloc(cf->pool,
|
|
1331 sizeof(ngx_http_proxy_upstream_conf_t));
|
|
1332 if (lcf->upstream == NULL) {
|
|
1333 return NGX_CONF_ERROR;
|
|
1334 }
|
0
|
1335
|
34
|
1336 lcf->upstream->url = *url;
|
|
1337
|
28
|
1338 if (ngx_strncasecmp(url->data + 7, "unix:", 5) == 0) {
|
0
|
1339
|
28
|
1340 #if (NGX_HAVE_UNIX_DOMAIN)
|
0
|
1341
|
28
|
1342 ngx_memzero(&unix_upstream, sizeof(ngx_unix_domain_upstream_t));
|
2
|
1343
|
28
|
1344 unix_upstream.name = *url;
|
|
1345 unix_upstream.url.len = url->len - 7;
|
|
1346 unix_upstream.url.data = url->data + 7;
|
|
1347 unix_upstream.uri_part = 1;
|
0
|
1348
|
50
|
1349 lcf->peers = ngx_unix_upstream_parse(cf, &unix_upstream);
|
|
1350 if (lcf->peers == NULL) {
|
2
|
1351 return NGX_CONF_ERROR;
|
|
1352 }
|
0
|
1353
|
28
|
1354 lcf->upstream->host_header.len = sizeof("localhost") - 1;
|
|
1355 lcf->upstream->host_header.data = (u_char *) "localhost";
|
|
1356 lcf->upstream->uri = unix_upstream.uri;
|
|
1357 lcf->upstream->uri_separator = ":";
|
|
1358 lcf->upstream->default_port = 1;
|
2
|
1359
|
28
|
1360 #else
|
|
1361 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1362 "the unix domain sockets are not supported "
|
|
1363 "on this platform");
|
|
1364 return NGX_CONF_ERROR;
|
0
|
1365
|
28
|
1366 #endif
|
0
|
1367
|
|
1368 } else {
|
28
|
1369 ngx_memzero(&inet_upstream, sizeof(ngx_inet_upstream_t));
|
0
|
1370
|
28
|
1371 inet_upstream.name = *url;
|
|
1372 inet_upstream.url.len = url->len - 7;
|
|
1373 inet_upstream.url.data = url->data + 7;
|
|
1374 inet_upstream.default_port_value = 80;
|
|
1375 inet_upstream.uri_part = 1;
|
0
|
1376
|
50
|
1377 lcf->peers = ngx_inet_upstream_parse(cf, &inet_upstream);
|
|
1378 if (lcf->peers == NULL) {
|
2
|
1379 return NGX_CONF_ERROR;
|
|
1380 }
|
0
|
1381
|
28
|
1382 lcf->upstream->host_header = inet_upstream.host_header;
|
|
1383 lcf->upstream->port_text = inet_upstream.port_text;
|
|
1384 lcf->upstream->uri = inet_upstream.uri;
|
|
1385 lcf->upstream->uri_separator = "";
|
30
|
1386 lcf->upstream->default_port = inet_upstream.default_port;
|
0
|
1387 }
|
|
1388
|
|
1389 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
1390
|
50
|
1391 clcf->handler = ngx_http_proxy_handler;
|
|
1392
|
|
1393 #if (NGX_PCRE)
|
|
1394 lcf->upstream->location = clcf->regex ? &ngx_http_proxy_uri : &clcf->name;
|
|
1395 #else
|
0
|
1396 lcf->upstream->location = &clcf->name;
|
50
|
1397 #endif
|
0
|
1398
|
|
1399 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
1400 clcf->auto_redirect = 1;
|
|
1401 }
|
|
1402
|
28
|
1403 return NGX_CONF_OK;
|
0
|
1404 }
|
2
|
1405
|
|
1406
|
34
|
1407 static char *ngx_http_proxy_set_x_var(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
1408 void *conf)
|
|
1409 {
|
|
1410 ngx_http_proxy_loc_conf_t *lcf = conf;
|
|
1411
|
|
1412 ngx_uint_t i, *index;
|
|
1413 ngx_str_t *value;
|
|
1414 ngx_http_variable_t *var;
|
|
1415 ngx_http_core_main_conf_t *cmcf;
|
|
1416
|
36
|
1417 if (lcf->x_vars == NULL) {
|
|
1418 lcf->x_vars = ngx_array_create(cf->pool, 4,
|
|
1419 sizeof(ngx_http_variable_t *));
|
|
1420 if (lcf->x_vars == NULL) {
|
34
|
1421 return NGX_CONF_ERROR;
|
|
1422 }
|
|
1423 }
|
|
1424
|
|
1425 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
1426
|
|
1427 value = cf->args->elts;
|
|
1428
|
|
1429 var = cmcf->variables.elts;
|
|
1430 for (i = 0; i < cmcf->variables.nelts; i++) {
|
|
1431 if (ngx_strcasecmp(var[i].name.data, value[1].data) == 0) {
|
|
1432
|
50
|
1433 index = ngx_array_push(lcf->x_vars);
|
|
1434 if (index == NULL) {
|
34
|
1435 return NGX_CONF_ERROR;
|
|
1436 }
|
|
1437
|
|
1438 *index = var[i].index;
|
|
1439 return NGX_CONF_OK;
|
|
1440 }
|
|
1441 }
|
|
1442
|
|
1443 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1444 "unknown variable name \"%V\"", &value[1]);
|
|
1445 return NGX_CONF_ERROR;
|
|
1446 }
|
|
1447
|
|
1448
|
2
|
1449 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
|
1450 {
|
22
|
1451 #if (NGX_FREEBSD)
|
4
|
1452 ssize_t *np = data;
|
2
|
1453
|
|
1454 if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
|
1455 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1456 "\"proxy_send_lowat\" must be less than %d "
|
|
1457 "(sysctl net.inet.tcp.sendspace)",
|
|
1458 ngx_freebsd_net_inet_tcp_sendspace);
|
|
1459
|
|
1460 return NGX_CONF_ERROR;
|
|
1461 }
|
|
1462
|
18
|
1463 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
22
|
1464 ssize_t *np = data;
|
2
|
1465
|
|
1466 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
1467 "\"proxy_send_lowat\" is not supported, ignored");
|
|
1468
|
|
1469 *np = 0;
|
|
1470
|
|
1471 #endif
|
|
1472
|
|
1473 return NGX_CONF_OK;
|
|
1474 }
|