3
* Copyright (C) Igor Sysoev
4
* Copyright (C) Nginx, Inc.
8
#include <ngx_config.h>
13
typedef struct ngx_http_proxy_rewrite_s ngx_http_proxy_rewrite_t;
15
typedef ngx_int_t (*ngx_http_proxy_rewrite_pt)(ngx_http_request_t *r,
16
ngx_table_elt_t *h, size_t prefix, size_t len,
17
ngx_http_proxy_rewrite_t *pr);
19
struct ngx_http_proxy_rewrite_s {
20
ngx_http_proxy_rewrite_pt handler;
23
ngx_http_complex_value_t complex;
25
ngx_http_regex_t *regex;
29
ngx_http_complex_value_t replacement;
36
ngx_str_t host_header;
39
} ngx_http_proxy_vars_t;
43
ngx_http_upstream_conf_t upstream;
46
ngx_array_t *body_set_len;
47
ngx_array_t *body_set;
48
ngx_array_t *headers_set_len;
49
ngx_array_t *headers_set;
50
ngx_hash_t headers_set_hash;
52
ngx_array_t *headers_source;
54
ngx_array_t *proxy_lengths;
55
ngx_array_t *proxy_values;
57
ngx_array_t *redirects;
58
ngx_array_t *cookie_domains;
59
ngx_array_t *cookie_paths;
61
ngx_str_t body_source;
68
ngx_http_complex_value_t cache_key;
71
ngx_http_proxy_vars_t vars;
75
ngx_uint_t http_version;
77
ngx_uint_t headers_hash_max_size;
78
ngx_uint_t headers_hash_bucket_size;
79
} ngx_http_proxy_loc_conf_t;
83
ngx_http_status_t status;
84
ngx_http_proxy_vars_t vars;
85
size_t internal_body_length;
91
ngx_uint_t head; /* unsigned head:1 */
92
} ngx_http_proxy_ctx_t;
95
static ngx_int_t ngx_http_proxy_eval(ngx_http_request_t *r,
96
ngx_http_proxy_ctx_t *ctx, ngx_http_proxy_loc_conf_t *plcf);
98
static ngx_int_t ngx_http_proxy_create_key(ngx_http_request_t *r);
100
static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
101
static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r);
102
static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r);
103
static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r);
104
static ngx_int_t ngx_http_proxy_input_filter_init(void *data);
105
static ngx_int_t ngx_http_proxy_copy_filter(ngx_event_pipe_t *p,
107
static ngx_int_t ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p,
109
static ngx_int_t ngx_http_proxy_non_buffered_copy_filter(void *data,
111
static ngx_int_t ngx_http_proxy_non_buffered_chunked_filter(void *data,
113
static void ngx_http_proxy_abort_request(ngx_http_request_t *r);
114
static void ngx_http_proxy_finalize_request(ngx_http_request_t *r,
117
static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r,
118
ngx_http_variable_value_t *v, uintptr_t data);
119
static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r,
120
ngx_http_variable_value_t *v, uintptr_t data);
122
ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
123
ngx_http_variable_value_t *v, uintptr_t data);
125
ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
126
ngx_http_variable_value_t *v, uintptr_t data);
127
static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
128
ngx_table_elt_t *h, size_t prefix);
129
static ngx_int_t ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r,
131
static ngx_int_t ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r,
132
ngx_table_elt_t *h, u_char *value, ngx_array_t *rewrites);
133
static ngx_int_t ngx_http_proxy_rewrite(ngx_http_request_t *r,
134
ngx_table_elt_t *h, size_t prefix, size_t len, ngx_str_t *replacement);
136
static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
137
static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
138
static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
139
void *parent, void *child);
140
static ngx_int_t ngx_http_proxy_merge_headers(ngx_conf_t *cf,
141
ngx_http_proxy_loc_conf_t *conf, ngx_http_proxy_loc_conf_t *prev);
143
static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
145
static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
147
static char *ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd,
149
static char *ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd,
151
static char *ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd,
154
static char *ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd,
156
static char *ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd,
160
static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
162
static ngx_int_t ngx_http_proxy_rewrite_regex(ngx_conf_t *cf,
163
ngx_http_proxy_rewrite_t *pr, ngx_str_t *regex, ngx_uint_t caseless);
166
static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf,
167
ngx_http_proxy_loc_conf_t *plcf);
169
static void ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v);
172
static ngx_conf_post_t ngx_http_proxy_lowat_post =
173
{ ngx_http_proxy_lowat_check };
176
static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = {
177
{ ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
178
{ ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
179
{ ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
180
{ ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
181
{ ngx_string("http_502"), NGX_HTTP_UPSTREAM_FT_HTTP_502 },
182
{ ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
183
{ ngx_string("http_504"), NGX_HTTP_UPSTREAM_FT_HTTP_504 },
184
{ ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
185
{ ngx_string("updating"), NGX_HTTP_UPSTREAM_FT_UPDATING },
186
{ ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
187
{ ngx_null_string, 0 }
191
static ngx_conf_enum_t ngx_http_proxy_http_version[] = {
192
{ ngx_string("1.0"), NGX_HTTP_VERSION_10 },
193
{ ngx_string("1.1"), NGX_HTTP_VERSION_11 },
194
{ ngx_null_string, 0 }
198
ngx_module_t ngx_http_proxy_module;
201
static ngx_command_t ngx_http_proxy_commands[] = {
203
{ ngx_string("proxy_pass"),
204
NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
206
NGX_HTTP_LOC_CONF_OFFSET,
210
{ ngx_string("proxy_redirect"),
211
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
212
ngx_http_proxy_redirect,
213
NGX_HTTP_LOC_CONF_OFFSET,
217
{ ngx_string("proxy_cookie_domain"),
218
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
219
ngx_http_proxy_cookie_domain,
220
NGX_HTTP_LOC_CONF_OFFSET,
224
{ ngx_string("proxy_cookie_path"),
225
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
226
ngx_http_proxy_cookie_path,
227
NGX_HTTP_LOC_CONF_OFFSET,
231
{ ngx_string("proxy_store"),
232
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
233
ngx_http_proxy_store,
234
NGX_HTTP_LOC_CONF_OFFSET,
238
{ ngx_string("proxy_store_access"),
239
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
240
ngx_conf_set_access_slot,
241
NGX_HTTP_LOC_CONF_OFFSET,
242
offsetof(ngx_http_proxy_loc_conf_t, upstream.store_access),
245
{ ngx_string("proxy_buffering"),
246
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
247
ngx_conf_set_flag_slot,
248
NGX_HTTP_LOC_CONF_OFFSET,
249
offsetof(ngx_http_proxy_loc_conf_t, upstream.buffering),
252
{ ngx_string("proxy_ignore_client_abort"),
253
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
254
ngx_conf_set_flag_slot,
255
NGX_HTTP_LOC_CONF_OFFSET,
256
offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort),
259
{ ngx_string("proxy_bind"),
260
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
261
ngx_http_upstream_bind_set_slot,
262
NGX_HTTP_LOC_CONF_OFFSET,
263
offsetof(ngx_http_proxy_loc_conf_t, upstream.local),
266
{ ngx_string("proxy_connect_timeout"),
267
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
268
ngx_conf_set_msec_slot,
269
NGX_HTTP_LOC_CONF_OFFSET,
270
offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
273
{ ngx_string("proxy_send_timeout"),
274
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
275
ngx_conf_set_msec_slot,
276
NGX_HTTP_LOC_CONF_OFFSET,
277
offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
280
{ ngx_string("proxy_send_lowat"),
281
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
282
ngx_conf_set_size_slot,
283
NGX_HTTP_LOC_CONF_OFFSET,
284
offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
285
&ngx_http_proxy_lowat_post },
287
{ ngx_string("proxy_intercept_errors"),
288
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
289
ngx_conf_set_flag_slot,
290
NGX_HTTP_LOC_CONF_OFFSET,
291
offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors),
294
{ ngx_string("proxy_set_header"),
295
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
296
ngx_conf_set_keyval_slot,
297
NGX_HTTP_LOC_CONF_OFFSET,
298
offsetof(ngx_http_proxy_loc_conf_t, headers_source),
301
{ ngx_string("proxy_headers_hash_max_size"),
302
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
303
ngx_conf_set_num_slot,
304
NGX_HTTP_LOC_CONF_OFFSET,
305
offsetof(ngx_http_proxy_loc_conf_t, headers_hash_max_size),
308
{ ngx_string("proxy_headers_hash_bucket_size"),
309
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
310
ngx_conf_set_num_slot,
311
NGX_HTTP_LOC_CONF_OFFSET,
312
offsetof(ngx_http_proxy_loc_conf_t, headers_hash_bucket_size),
315
{ ngx_string("proxy_set_body"),
316
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
317
ngx_conf_set_str_slot,
318
NGX_HTTP_LOC_CONF_OFFSET,
319
offsetof(ngx_http_proxy_loc_conf_t, body_source),
322
{ ngx_string("proxy_method"),
323
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
324
ngx_conf_set_str_slot,
325
NGX_HTTP_LOC_CONF_OFFSET,
326
offsetof(ngx_http_proxy_loc_conf_t, method),
329
{ ngx_string("proxy_pass_request_headers"),
330
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
331
ngx_conf_set_flag_slot,
332
NGX_HTTP_LOC_CONF_OFFSET,
333
offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers),
336
{ ngx_string("proxy_pass_request_body"),
337
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
338
ngx_conf_set_flag_slot,
339
NGX_HTTP_LOC_CONF_OFFSET,
340
offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body),
343
{ ngx_string("proxy_buffer_size"),
344
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
345
ngx_conf_set_size_slot,
346
NGX_HTTP_LOC_CONF_OFFSET,
347
offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
350
{ ngx_string("proxy_read_timeout"),
351
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
352
ngx_conf_set_msec_slot,
353
NGX_HTTP_LOC_CONF_OFFSET,
354
offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout),
357
{ ngx_string("proxy_buffers"),
358
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
359
ngx_conf_set_bufs_slot,
360
NGX_HTTP_LOC_CONF_OFFSET,
361
offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
364
{ ngx_string("proxy_busy_buffers_size"),
365
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
366
ngx_conf_set_size_slot,
367
NGX_HTTP_LOC_CONF_OFFSET,
368
offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf),
373
{ ngx_string("proxy_cache"),
374
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
375
ngx_http_proxy_cache,
376
NGX_HTTP_LOC_CONF_OFFSET,
380
{ ngx_string("proxy_cache_key"),
381
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
382
ngx_http_proxy_cache_key,
383
NGX_HTTP_LOC_CONF_OFFSET,
387
{ ngx_string("proxy_cache_path"),
388
NGX_HTTP_MAIN_CONF|NGX_CONF_2MORE,
389
ngx_http_file_cache_set_slot,
392
&ngx_http_proxy_module },
394
{ ngx_string("proxy_cache_bypass"),
395
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
396
ngx_http_set_predicate_slot,
397
NGX_HTTP_LOC_CONF_OFFSET,
398
offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_bypass),
401
{ ngx_string("proxy_no_cache"),
402
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
403
ngx_http_set_predicate_slot,
404
NGX_HTTP_LOC_CONF_OFFSET,
405
offsetof(ngx_http_proxy_loc_conf_t, upstream.no_cache),
408
{ ngx_string("proxy_cache_valid"),
409
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
410
ngx_http_file_cache_valid_set_slot,
411
NGX_HTTP_LOC_CONF_OFFSET,
412
offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_valid),
415
{ ngx_string("proxy_cache_min_uses"),
416
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
417
ngx_conf_set_num_slot,
418
NGX_HTTP_LOC_CONF_OFFSET,
419
offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_min_uses),
422
{ ngx_string("proxy_cache_use_stale"),
423
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
424
ngx_conf_set_bitmask_slot,
425
NGX_HTTP_LOC_CONF_OFFSET,
426
offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_use_stale),
427
&ngx_http_proxy_next_upstream_masks },
429
{ ngx_string("proxy_cache_methods"),
430
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
431
ngx_conf_set_bitmask_slot,
432
NGX_HTTP_LOC_CONF_OFFSET,
433
offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_methods),
434
&ngx_http_upstream_cache_method_mask },
436
{ ngx_string("proxy_cache_lock"),
437
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
438
ngx_conf_set_flag_slot,
439
NGX_HTTP_LOC_CONF_OFFSET,
440
offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock),
443
{ ngx_string("proxy_cache_lock_timeout"),
444
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
445
ngx_conf_set_msec_slot,
446
NGX_HTTP_LOC_CONF_OFFSET,
447
offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_timeout),
452
{ ngx_string("proxy_temp_path"),
453
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
454
ngx_conf_set_path_slot,
455
NGX_HTTP_LOC_CONF_OFFSET,
456
offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
459
{ ngx_string("proxy_max_temp_file_size"),
460
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
461
ngx_conf_set_size_slot,
462
NGX_HTTP_LOC_CONF_OFFSET,
463
offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf),
466
{ ngx_string("proxy_temp_file_write_size"),
467
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
468
ngx_conf_set_size_slot,
469
NGX_HTTP_LOC_CONF_OFFSET,
470
offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf),
473
{ ngx_string("proxy_next_upstream"),
474
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
475
ngx_conf_set_bitmask_slot,
476
NGX_HTTP_LOC_CONF_OFFSET,
477
offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
478
&ngx_http_proxy_next_upstream_masks },
480
{ ngx_string("proxy_pass_header"),
481
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
482
ngx_conf_set_str_array_slot,
483
NGX_HTTP_LOC_CONF_OFFSET,
484
offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers),
487
{ ngx_string("proxy_hide_header"),
488
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
489
ngx_conf_set_str_array_slot,
490
NGX_HTTP_LOC_CONF_OFFSET,
491
offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers),
494
{ ngx_string("proxy_ignore_headers"),
495
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
496
ngx_conf_set_bitmask_slot,
497
NGX_HTTP_LOC_CONF_OFFSET,
498
offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_headers),
499
&ngx_http_upstream_ignore_headers_masks },
501
{ ngx_string("proxy_http_version"),
502
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
503
ngx_conf_set_enum_slot,
504
NGX_HTTP_LOC_CONF_OFFSET,
505
offsetof(ngx_http_proxy_loc_conf_t, http_version),
506
&ngx_http_proxy_http_version },
510
{ ngx_string("proxy_ssl_session_reuse"),
511
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
512
ngx_conf_set_flag_slot,
513
NGX_HTTP_LOC_CONF_OFFSET,
514
offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_session_reuse),
523
static ngx_http_module_t ngx_http_proxy_module_ctx = {
524
ngx_http_proxy_add_variables, /* preconfiguration */
525
NULL, /* postconfiguration */
527
NULL, /* create main configuration */
528
NULL, /* init main configuration */
530
NULL, /* create server configuration */
531
NULL, /* merge server configuration */
533
ngx_http_proxy_create_loc_conf, /* create location configuration */
534
ngx_http_proxy_merge_loc_conf /* merge location configuration */
538
ngx_module_t ngx_http_proxy_module = {
540
&ngx_http_proxy_module_ctx, /* module context */
541
ngx_http_proxy_commands, /* module directives */
542
NGX_HTTP_MODULE, /* module type */
543
NULL, /* init master */
544
NULL, /* init module */
545
NULL, /* init process */
546
NULL, /* init thread */
547
NULL, /* exit thread */
548
NULL, /* exit process */
549
NULL, /* exit master */
550
NGX_MODULE_V1_PADDING
554
static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
555
static char ngx_http_proxy_version_11[] = " HTTP/1.1" CRLF;
558
static ngx_keyval_t ngx_http_proxy_headers[] = {
559
{ ngx_string("Host"), ngx_string("$proxy_host") },
560
{ ngx_string("Connection"), ngx_string("close") },
561
{ ngx_string("Keep-Alive"), ngx_string("") },
562
{ ngx_string("Expect"), ngx_string("") },
563
{ ngx_string("Upgrade"), ngx_string("") },
564
{ ngx_null_string, ngx_null_string }
568
static ngx_str_t ngx_http_proxy_hide_headers[] = {
570
ngx_string("Server"),
572
ngx_string("X-Accel-Expires"),
573
ngx_string("X-Accel-Redirect"),
574
ngx_string("X-Accel-Limit-Rate"),
575
ngx_string("X-Accel-Buffering"),
576
ngx_string("X-Accel-Charset"),
583
static ngx_keyval_t ngx_http_proxy_cache_headers[] = {
584
{ ngx_string("Host"), ngx_string("$proxy_host") },
585
{ ngx_string("Connection"), ngx_string("close") },
586
{ ngx_string("Keep-Alive"), ngx_string("") },
587
{ ngx_string("Expect"), ngx_string("") },
588
{ ngx_string("Upgrade"), ngx_string("") },
589
{ ngx_string("If-Modified-Since"), ngx_string("") },
590
{ ngx_string("If-Unmodified-Since"), ngx_string("") },
591
{ ngx_string("If-None-Match"), ngx_string("") },
592
{ ngx_string("If-Match"), ngx_string("") },
593
{ ngx_string("Range"), ngx_string("") },
594
{ ngx_string("If-Range"), ngx_string("") },
595
{ ngx_null_string, ngx_null_string }
601
static ngx_http_variable_t ngx_http_proxy_vars[] = {
603
{ ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0,
604
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
606
{ ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0,
607
NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
609
{ ngx_string("proxy_add_x_forwarded_for"), NULL,
610
ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
613
{ ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
616
{ ngx_string("proxy_internal_body_length"), NULL,
617
ngx_http_proxy_internal_body_length_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
619
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
623
static ngx_path_init_t ngx_http_proxy_temp_path = {
624
ngx_string(NGX_HTTP_PROXY_TEMP_PATH), { 1, 2, 0 }
629
ngx_http_proxy_handler(ngx_http_request_t *r)
632
ngx_http_upstream_t *u;
633
ngx_http_proxy_ctx_t *ctx;
634
ngx_http_proxy_loc_conf_t *plcf;
636
if (ngx_http_upstream_create(r) != NGX_OK) {
637
return NGX_HTTP_INTERNAL_SERVER_ERROR;
640
ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
645
ngx_http_set_ctx(r, ctx, ngx_http_proxy_module);
647
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
651
if (plcf->proxy_lengths == NULL) {
652
ctx->vars = plcf->vars;
653
u->schema = plcf->vars.schema;
655
u->ssl = (plcf->upstream.ssl != NULL);
659
if (ngx_http_proxy_eval(r, ctx, plcf) != NGX_OK) {
660
return NGX_HTTP_INTERNAL_SERVER_ERROR;
664
u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
666
u->conf = &plcf->upstream;
669
u->create_key = ngx_http_proxy_create_key;
671
u->create_request = ngx_http_proxy_create_request;
672
u->reinit_request = ngx_http_proxy_reinit_request;
673
u->process_header = ngx_http_proxy_process_status_line;
674
u->abort_request = ngx_http_proxy_abort_request;
675
u->finalize_request = ngx_http_proxy_finalize_request;
678
if (plcf->redirects) {
679
u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
682
if (plcf->cookie_domains || plcf->cookie_paths) {
683
u->rewrite_cookie = ngx_http_proxy_rewrite_cookie;
686
u->buffering = plcf->upstream.buffering;
688
u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
689
if (u->pipe == NULL) {
690
return NGX_HTTP_INTERNAL_SERVER_ERROR;
693
u->pipe->input_filter = ngx_http_proxy_copy_filter;
694
u->pipe->input_ctx = r;
696
u->input_filter_init = ngx_http_proxy_input_filter_init;
697
u->input_filter = ngx_http_proxy_non_buffered_copy_filter;
698
u->input_filter_ctx = r;
702
rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
704
if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
713
ngx_http_proxy_eval(ngx_http_request_t *r, ngx_http_proxy_ctx_t *ctx,
714
ngx_http_proxy_loc_conf_t *plcf)
721
ngx_http_upstream_t *u;
723
if (ngx_http_script_run(r, &proxy, plcf->proxy_lengths->elts, 0,
724
plcf->proxy_values->elts)
731
&& ngx_strncasecmp(proxy.data, (u_char *) "http://", 7) == 0)
738
} else if (proxy.len > 8
739
&& ngx_strncasecmp(proxy.data, (u_char *) "https://", 8) == 0)
743
r->upstream->ssl = 1;
748
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
749
"invalid URL prefix in \"%V\"", &proxy);
756
u->schema.data = proxy.data;
758
ngx_memzero(&url, sizeof(ngx_url_t));
760
url.url.len = proxy.len - add;
761
url.url.data = proxy.data + add;
762
url.default_port = port;
766
if (ngx_parse_url(r->pool, &url) != NGX_OK) {
768
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
769
"%s in upstream \"%V\"", url.err, &url.url);
776
if (url.uri.data[0] == '?') {
777
p = ngx_pnalloc(r->pool, url.uri.len + 1);
783
ngx_memcpy(p, url.uri.data, url.uri.len);
786
url.uri.data = p - 1;
790
ctx->vars.key_start = u->schema;
792
ngx_http_proxy_set_vars(&url, &ctx->vars);
794
u->resolved = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_resolved_t));
795
if (u->resolved == NULL) {
799
if (url.addrs && url.addrs[0].sockaddr) {
800
u->resolved->sockaddr = url.addrs[0].sockaddr;
801
u->resolved->socklen = url.addrs[0].socklen;
802
u->resolved->naddrs = 1;
803
u->resolved->host = url.addrs[0].name;
806
u->resolved->host = url.host;
807
u->resolved->port = (in_port_t) (url.no_port ? port : url.port);
808
u->resolved->no_port = url.no_port;
818
ngx_http_proxy_create_key(ngx_http_request_t *r)
824
ngx_http_upstream_t *u;
825
ngx_http_proxy_ctx_t *ctx;
826
ngx_http_proxy_loc_conf_t *plcf;
830
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
832
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
834
key = ngx_array_push(&r->cache->keys);
839
if (plcf->cache_key.value.len) {
841
if (ngx_http_complex_value(r, &plcf->cache_key, key) != NGX_OK) {
848
*key = ctx->vars.key_start;
850
key = ngx_array_push(&r->cache->keys);
855
if (plcf->proxy_lengths && ctx->vars.uri.len) {
857
*key = ctx->vars.uri;
858
u->uri = ctx->vars.uri;
862
} else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)
864
*key = r->unparsed_uri;
865
u->uri = r->unparsed_uri;
870
loc_len = (r->valid_location && ctx->vars.uri.len) ? plcf->location.len : 0;
872
if (r->quoted_uri || r->internal) {
873
escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
874
r->uri.len - loc_len, NGX_ESCAPE_URI);
879
len = ctx->vars.uri.len + r->uri.len - loc_len + escape
880
+ sizeof("?") - 1 + r->args.len;
882
p = ngx_pnalloc(r->pool, len);
889
if (r->valid_location) {
890
p = ngx_copy(p, ctx->vars.uri.data, ctx->vars.uri.len);
894
ngx_escape_uri(p, r->uri.data + loc_len,
895
r->uri.len - loc_len, NGX_ESCAPE_URI);
896
p += r->uri.len - loc_len + escape;
899
p = ngx_copy(p, r->uri.data + loc_len, r->uri.len - loc_len);
902
if (r->args.len > 0) {
904
p = ngx_copy(p, r->args.data, r->args.len);
907
key->len = p - key->data;
917
ngx_http_proxy_create_request(ngx_http_request_t *r)
919
size_t len, uri_len, loc_len, body_len;
923
ngx_uint_t i, unparsed_uri;
924
ngx_chain_t *cl, *body;
925
ngx_list_part_t *part;
926
ngx_table_elt_t *header;
927
ngx_http_upstream_t *u;
928
ngx_http_proxy_ctx_t *ctx;
929
ngx_http_script_code_pt code;
930
ngx_http_script_engine_t e, le;
931
ngx_http_proxy_loc_conf_t *plcf;
932
ngx_http_script_len_code_pt lcode;
936
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
939
/* HEAD was changed to GET to cache response */
943
} else if (plcf->method.len) {
944
method = plcf->method;
947
method = r->method_name;
951
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
954
&& ngx_strncasecmp(method.data, (u_char *) "HEAD ", 5) == 0)
959
len = method.len + sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;
965
if (plcf->proxy_lengths && ctx->vars.uri.len) {
966
uri_len = ctx->vars.uri.len;
968
} else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)
971
uri_len = r->unparsed_uri.len;
974
loc_len = (r->valid_location && ctx->vars.uri.len) ?
975
plcf->location.len : 0;
977
if (r->quoted_uri || r->space_in_uri || r->internal) {
978
escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
979
r->uri.len - loc_len, NGX_ESCAPE_URI);
982
uri_len = ctx->vars.uri.len + r->uri.len - loc_len + escape
983
+ sizeof("?") - 1 + r->args.len;
987
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
988
"zero length URI to proxy");
994
ngx_http_script_flush_no_cacheable_variables(r, plcf->flushes);
996
if (plcf->body_set_len) {
997
le.ip = plcf->body_set_len->elts;
1002
while (*(uintptr_t *) le.ip) {
1003
lcode = *(ngx_http_script_len_code_pt *) le.ip;
1004
body_len += lcode(&le);
1007
ctx->internal_body_length = body_len;
1011
le.ip = plcf->headers_set_len->elts;
1015
while (*(uintptr_t *) le.ip) {
1016
while (*(uintptr_t *) le.ip) {
1017
lcode = *(ngx_http_script_len_code_pt *) le.ip;
1020
le.ip += sizeof(uintptr_t);
1024
if (plcf->upstream.pass_request_headers) {
1025
part = &r->headers_in.headers.part;
1026
header = part->elts;
1028
for (i = 0; /* void */; i++) {
1030
if (i >= part->nelts) {
1031
if (part->next == NULL) {
1036
header = part->elts;
1040
if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
1041
header[i].lowcase_key, header[i].key.len))
1046
len += header[i].key.len + sizeof(": ") - 1
1047
+ header[i].value.len + sizeof(CRLF) - 1;
1052
b = ngx_create_temp_buf(r->pool, len);
1057
cl = ngx_alloc_chain_link(r->pool);
1065
/* the request line */
1067
b->last = ngx_copy(b->last, method.data, method.len);
1069
u->uri.data = b->last;
1071
if (plcf->proxy_lengths && ctx->vars.uri.len) {
1072
b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
1074
} else if (unparsed_uri) {
1075
b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);
1078
if (r->valid_location) {
1079
b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
1083
ngx_escape_uri(b->last, r->uri.data + loc_len,
1084
r->uri.len - loc_len, NGX_ESCAPE_URI);
1085
b->last += r->uri.len - loc_len + escape;
1088
b->last = ngx_copy(b->last, r->uri.data + loc_len,
1089
r->uri.len - loc_len);
1092
if (r->args.len > 0) {
1094
b->last = ngx_copy(b->last, r->args.data, r->args.len);
1098
u->uri.len = b->last - u->uri.data;
1100
if (plcf->http_version == NGX_HTTP_VERSION_11) {
1101
b->last = ngx_cpymem(b->last, ngx_http_proxy_version_11,
1102
sizeof(ngx_http_proxy_version_11) - 1);
1105
b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
1106
sizeof(ngx_http_proxy_version) - 1);
1109
ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
1111
e.ip = plcf->headers_set->elts;
1116
le.ip = plcf->headers_set_len->elts;
1118
while (*(uintptr_t *) le.ip) {
1119
lcode = *(ngx_http_script_len_code_pt *) le.ip;
1121
/* skip the header line name length */
1124
if (*(ngx_http_script_len_code_pt *) le.ip) {
1126
for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
1127
lcode = *(ngx_http_script_len_code_pt *) le.ip;
1130
e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0;
1136
le.ip += sizeof(uintptr_t);
1138
while (*(uintptr_t *) e.ip) {
1139
code = *(ngx_http_script_code_pt *) e.ip;
1140
code((ngx_http_script_engine_t *) &e);
1142
e.ip += sizeof(uintptr_t);
1148
if (plcf->upstream.pass_request_headers) {
1149
part = &r->headers_in.headers.part;
1150
header = part->elts;
1152
for (i = 0; /* void */; i++) {
1154
if (i >= part->nelts) {
1155
if (part->next == NULL) {
1160
header = part->elts;
1164
if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
1165
header[i].lowcase_key, header[i].key.len))
1170
b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
1172
*b->last++ = ':'; *b->last++ = ' ';
1174
b->last = ngx_copy(b->last, header[i].value.data,
1175
header[i].value.len);
1177
*b->last++ = CR; *b->last++ = LF;
1179
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1180
"http proxy header: \"%V: %V\"",
1181
&header[i].key, &header[i].value);
1186
/* add "\r\n" at the header end */
1187
*b->last++ = CR; *b->last++ = LF;
1189
if (plcf->body_set) {
1190
e.ip = plcf->body_set->elts;
1193
while (*(uintptr_t *) e.ip) {
1194
code = *(ngx_http_script_code_pt *) e.ip;
1195
code((ngx_http_script_engine_t *) &e);
1201
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1202
"http proxy header:\n\"%*s\"",
1203
(size_t) (b->last - b->pos), b->pos);
1205
if (plcf->body_set == NULL && plcf->upstream.pass_request_body) {
1207
body = u->request_bufs;
1208
u->request_bufs = cl;
1211
b = ngx_alloc_buf(r->pool);
1216
ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
1218
cl->next = ngx_alloc_chain_link(r->pool);
1219
if (cl->next == NULL) {
1230
u->request_bufs = cl;
1241
ngx_http_proxy_reinit_request(ngx_http_request_t *r)
1243
ngx_http_proxy_ctx_t *ctx;
1245
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1251
ctx->status.code = 0;
1252
ctx->status.count = 0;
1253
ctx->status.start = NULL;
1254
ctx->status.end = NULL;
1257
r->upstream->process_header = ngx_http_proxy_process_status_line;
1258
r->upstream->pipe->input_filter = ngx_http_proxy_copy_filter;
1259
r->upstream->input_filter = ngx_http_proxy_non_buffered_copy_filter;
1267
ngx_http_proxy_process_status_line(ngx_http_request_t *r)
1271
ngx_http_upstream_t *u;
1272
ngx_http_proxy_ctx_t *ctx;
1274
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1282
rc = ngx_http_parse_status_line(r, &u->buffer, &ctx->status);
1284
if (rc == NGX_AGAIN) {
1288
if (rc == NGX_ERROR) {
1290
#if (NGX_HTTP_CACHE)
1293
r->http_version = NGX_HTTP_VERSION_9;
1299
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1300
"upstream sent no valid HTTP/1.0 header");
1304
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1308
r->http_version = NGX_HTTP_VERSION_9;
1309
u->state->status = NGX_HTTP_OK;
1310
u->headers_in.connection_close = 1;
1316
u->state->status = ctx->status.code;
1319
u->headers_in.status_n = ctx->status.code;
1321
len = ctx->status.end - ctx->status.start;
1322
u->headers_in.status_line.len = len;
1324
u->headers_in.status_line.data = ngx_pnalloc(r->pool, len);
1325
if (u->headers_in.status_line.data == NULL) {
1329
ngx_memcpy(u->headers_in.status_line.data, ctx->status.start, len);
1331
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1332
"http proxy status %ui \"%V\"",
1333
u->headers_in.status_n, &u->headers_in.status_line);
1335
if (ctx->status.http_version < NGX_HTTP_VERSION_11) {
1336
u->headers_in.connection_close = 1;
1339
u->process_header = ngx_http_proxy_process_header;
1341
return ngx_http_proxy_process_header(r);
1346
ngx_http_proxy_process_header(ngx_http_request_t *r)
1350
ngx_http_upstream_t *u;
1351
ngx_http_proxy_ctx_t *ctx;
1352
ngx_http_upstream_header_t *hh;
1353
ngx_http_upstream_main_conf_t *umcf;
1355
umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
1359
rc = ngx_http_parse_header_line(r, &r->upstream->buffer, 1);
1363
/* a header line has been parsed successfully */
1365
h = ngx_list_push(&r->upstream->headers_in.headers);
1370
h->hash = r->header_hash;
1372
h->key.len = r->header_name_end - r->header_name_start;
1373
h->value.len = r->header_end - r->header_start;
1375
h->key.data = ngx_pnalloc(r->pool,
1376
h->key.len + 1 + h->value.len + 1 + h->key.len);
1377
if (h->key.data == NULL) {
1381
h->value.data = h->key.data + h->key.len + 1;
1382
h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
1384
ngx_memcpy(h->key.data, r->header_name_start, h->key.len);
1385
h->key.data[h->key.len] = '\0';
1386
ngx_memcpy(h->value.data, r->header_start, h->value.len);
1387
h->value.data[h->value.len] = '\0';
1389
if (h->key.len == r->lowcase_index) {
1390
ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
1393
ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
1396
hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
1397
h->lowcase_key, h->key.len);
1399
if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
1403
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1404
"http proxy header: \"%V: %V\"",
1405
&h->key, &h->value);
1410
if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1412
/* a whole header has been parsed successfully */
1414
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1415
"http proxy header done");
1418
* if no "Server" and "Date" in header line,
1419
* then add the special empty headers
1422
if (r->upstream->headers_in.server == NULL) {
1423
h = ngx_list_push(&r->upstream->headers_in.headers);
1428
h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
1429
ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
1431
ngx_str_set(&h->key, "Server");
1432
ngx_str_null(&h->value);
1433
h->lowcase_key = (u_char *) "server";
1436
if (r->upstream->headers_in.date == NULL) {
1437
h = ngx_list_push(&r->upstream->headers_in.headers);
1442
h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');
1444
ngx_str_set(&h->key, "Date");
1445
ngx_str_null(&h->value);
1446
h->lowcase_key = (u_char *) "date";
1449
/* clear content length if response is chunked */
1453
if (u->headers_in.chunked) {
1454
u->headers_in.content_length_n = -1;
1458
* set u->keepalive if response has no body; this allows to keep
1459
* connections alive in case of r->header_only or X-Accel-Redirect
1462
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1464
if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
1465
|| u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
1467
|| (!u->headers_in.chunked
1468
&& u->headers_in.content_length_n == 0))
1470
u->keepalive = !u->headers_in.connection_close;
1476
if (rc == NGX_AGAIN) {
1480
/* there was error while a header line parsing */
1482
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1483
"upstream sent invalid header");
1485
return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1491
ngx_http_proxy_input_filter_init(void *data)
1493
ngx_http_request_t *r = data;
1494
ngx_http_upstream_t *u;
1495
ngx_http_proxy_ctx_t *ctx;
1498
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1504
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1505
"http proxy filter init s:%d h:%d c:%d l:%O",
1506
u->headers_in.status_n, ctx->head, u->headers_in.chunked,
1507
u->headers_in.content_length_n);
1509
/* as per RFC2616, 4.4 Message Length */
1511
if (u->headers_in.status_n == NGX_HTTP_NO_CONTENT
1512
|| u->headers_in.status_n == NGX_HTTP_NOT_MODIFIED
1515
/* 1xx, 204, and 304 and replies to HEAD requests */
1516
/* no 1xx since we don't send Expect and Upgrade */
1518
u->pipe->length = 0;
1520
u->keepalive = !u->headers_in.connection_close;
1522
} else if (u->headers_in.chunked) {
1525
u->pipe->input_filter = ngx_http_proxy_chunked_filter;
1526
u->pipe->length = 3; /* "0" LF LF */
1528
u->input_filter = ngx_http_proxy_non_buffered_chunked_filter;
1531
} else if (u->headers_in.content_length_n == 0) {
1532
/* empty body: special case as filter won't be called */
1534
u->pipe->length = 0;
1536
u->keepalive = !u->headers_in.connection_close;
1539
/* content length or connection close */
1541
u->pipe->length = u->headers_in.content_length_n;
1542
u->length = u->headers_in.content_length_n;
1550
ngx_http_proxy_copy_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
1554
ngx_http_request_t *r;
1556
if (buf->pos == buf->last) {
1564
ngx_free_chain(p->pool, cl);
1567
b = ngx_alloc_buf(p->pool);
1573
ngx_memcpy(b, buf, sizeof(ngx_buf_t));
1580
cl = ngx_alloc_chain_link(p->pool);
1588
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, p->log, 0, "input buf #%d", b->num);
1595
p->last_in = &cl->next;
1597
if (p->length == -1) {
1601
p->length -= b->last - b->pos;
1603
if (p->length == 0) {
1605
p->upstream_done = 1;
1606
r->upstream->keepalive = !r->upstream->headers_in.connection_close;
1608
} else if (p->length < 0) {
1610
p->upstream_done = 1;
1612
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
1613
"upstream sent too much data");
1620
static ngx_inline ngx_int_t
1621
ngx_http_proxy_parse_chunked(ngx_http_request_t *r, ngx_buf_t *buf)
1625
ngx_http_proxy_ctx_t *ctx;
1630
sw_chunk_extension_almost_done,
1633
sw_after_data_almost_done,
1634
sw_last_chunk_extension,
1635
sw_last_chunk_extension_almost_done,
1637
sw_trailer_almost_done,
1639
sw_trailer_header_almost_done
1642
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1650
if (state == sw_chunk_data && ctx->size == 0) {
1651
state = sw_after_data;
1656
for (pos = buf->pos; pos < buf->last; pos++) {
1660
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1661
"http proxy chunked byte: %02Xd s:%d", ch, state);
1665
case sw_chunk_start:
1666
if (ch >= '0' && ch <= '9') {
1667
state = sw_chunk_size;
1668
ctx->size = ch - '0';
1672
c = (u_char) (ch | 0x20);
1674
if (c >= 'a' && c <= 'f') {
1675
state = sw_chunk_size;
1676
ctx->size = c - 'a' + 10;
1683
if (ch >= '0' && ch <= '9') {
1684
ctx->size = ctx->size * 16 + (ch - '0');
1688
c = (u_char) (ch | 0x20);
1690
if (c >= 'a' && c <= 'f') {
1691
ctx->size = ctx->size * 16 + (c - 'a' + 10);
1695
if (ctx->size == 0) {
1699
state = sw_last_chunk_extension_almost_done;
1707
state = sw_last_chunk_extension;
1718
state = sw_chunk_extension_almost_done;
1721
state = sw_chunk_data;
1726
state = sw_chunk_extension;
1734
case sw_chunk_extension:
1737
state = sw_chunk_extension_almost_done;
1740
state = sw_chunk_data;
1744
case sw_chunk_extension_almost_done:
1746
state = sw_chunk_data;
1758
state = sw_after_data_almost_done;
1761
state = sw_chunk_start;
1765
case sw_after_data_almost_done:
1767
state = sw_chunk_start;
1772
case sw_last_chunk_extension:
1775
state = sw_last_chunk_extension_almost_done;
1782
case sw_last_chunk_extension_almost_done:
1792
state = sw_trailer_almost_done;
1797
state = sw_trailer_header;
1801
case sw_trailer_almost_done:
1807
case sw_trailer_header:
1810
state = sw_trailer_header_almost_done;
1817
case sw_trailer_header_almost_done:
1834
case sw_chunk_start:
1835
ctx->length = 3 /* "0" LF LF */;
1838
ctx->length = 2 /* LF LF */
1839
+ (ctx->size ? ctx->size + 4 /* LF "0" LF LF */ : 0);
1841
case sw_chunk_extension:
1842
case sw_chunk_extension_almost_done:
1843
ctx->length = 1 /* LF */ + ctx->size + 4 /* LF "0" LF LF */;
1846
ctx->length = ctx->size + 4 /* LF "0" LF LF */;
1849
case sw_after_data_almost_done:
1850
ctx->length = 4 /* LF "0" LF LF */;
1852
case sw_last_chunk_extension:
1853
case sw_last_chunk_extension_almost_done:
1854
ctx->length = 2 /* LF LF */;
1857
case sw_trailer_almost_done:
1858
ctx->length = 1 /* LF */;
1860
case sw_trailer_header:
1861
case sw_trailer_header_almost_done:
1862
ctx->length = 2 /* LF LF */;
1880
ngx_http_proxy_chunked_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
1883
ngx_buf_t *b, **prev;
1885
ngx_http_request_t *r;
1886
ngx_http_proxy_ctx_t *ctx;
1888
if (buf->pos == buf->last) {
1893
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1900
prev = &buf->shadow;
1904
rc = ngx_http_proxy_parse_chunked(r, buf);
1908
/* a chunk has been parsed successfully */
1914
ngx_free_chain(p->pool, cl);
1917
b = ngx_alloc_buf(p->pool);
1923
ngx_memzero(b, sizeof(ngx_buf_t));
1926
b->start = buf->start;
1935
cl = ngx_alloc_chain_link(p->pool);
1948
p->last_in = &cl->next;
1950
/* STUB */ b->num = buf->num;
1952
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
1953
"input buf #%d %p", b->num, b->pos);
1955
if (buf->last - buf->pos >= ctx->size) {
1957
buf->pos += ctx->size;
1964
ctx->size -= buf->last - buf->pos;
1965
buf->pos = buf->last;
1966
b->last = buf->last;
1971
if (rc == NGX_DONE) {
1973
/* a whole response has been parsed successfully */
1975
p->upstream_done = 1;
1976
r->upstream->keepalive = !r->upstream->headers_in.connection_close;
1981
if (rc == NGX_AGAIN) {
1983
/* set p->length, minimal amount of data we want to see */
1985
p->length = ctx->length;
1990
/* invalid response */
1992
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1993
"upstream sent invalid chunked response");
1998
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1999
"http proxy chunked state %d, length %d",
2000
ctx->state, p->length);
2006
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
2007
"input buf %p %z", b->pos, b->last - b->pos);
2012
/* there is no data record in the buf, add it to free chain */
2014
if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
2023
ngx_http_proxy_non_buffered_copy_filter(void *data, ssize_t bytes)
2025
ngx_http_request_t *r = data;
2028
ngx_chain_t *cl, **ll;
2029
ngx_http_upstream_t *u;
2033
for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
2037
cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
2045
cl->buf->memory = 1;
2049
cl->buf->pos = b->last;
2051
cl->buf->last = b->last;
2052
cl->buf->tag = u->output.tag;
2054
if (u->length == -1) {
2060
if (u->length == 0) {
2061
u->keepalive = !u->headers_in.connection_close;
2069
ngx_http_proxy_non_buffered_chunked_filter(void *data, ssize_t bytes)
2071
ngx_http_request_t *r = data;
2075
ngx_chain_t *cl, **ll;
2076
ngx_http_upstream_t *u;
2077
ngx_http_proxy_ctx_t *ctx;
2079
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
2088
buf->pos = buf->last;
2091
for (cl = u->out_bufs, ll = &u->out_bufs; cl; cl = cl->next) {
2097
rc = ngx_http_proxy_parse_chunked(r, buf);
2101
/* a chunk has been parsed successfully */
2103
cl = ngx_chain_get_free_buf(r->pool, &u->free_bufs);
2117
b->tag = u->output.tag;
2119
if (buf->last - buf->pos >= ctx->size) {
2120
buf->pos += ctx->size;
2125
ctx->size -= buf->last - buf->pos;
2126
buf->pos = buf->last;
2127
b->last = buf->last;
2130
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2131
"http proxy out buf %p %z",
2132
b->pos, b->last - b->pos);
2137
if (rc == NGX_DONE) {
2139
/* a whole response has been parsed successfully */
2141
u->keepalive = !u->headers_in.connection_close;
2147
if (rc == NGX_AGAIN) {
2151
/* invalid response */
2153
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
2154
"upstream sent invalid chunked response");
2159
/* provide continuous buffer for subrequests in memory */
2161
if (r->subrequest_in_memory) {
2166
buf->pos = cl->buf->pos;
2169
buf->last = buf->pos;
2171
for (cl = u->out_bufs; cl; cl = cl->next) {
2172
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2173
"http proxy in memory %p-%p %uz",
2174
cl->buf->pos, cl->buf->last, ngx_buf_size(cl->buf));
2176
if (buf->last == cl->buf->pos) {
2177
buf->last = cl->buf->last;
2181
buf->last = ngx_movemem(buf->last, cl->buf->pos,
2182
cl->buf->last - cl->buf->pos);
2184
cl->buf->pos = buf->last - (cl->buf->last - cl->buf->pos);
2185
cl->buf->last = buf->last;
2194
ngx_http_proxy_abort_request(ngx_http_request_t *r)
2196
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2197
"abort http proxy request");
2204
ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
2206
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
2207
"finalize http proxy request");
2214
ngx_http_proxy_host_variable(ngx_http_request_t *r,
2215
ngx_http_variable_value_t *v, uintptr_t data)
2217
ngx_http_proxy_ctx_t *ctx;
2219
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
2226
v->len = ctx->vars.host_header.len;
2228
v->no_cacheable = 0;
2230
v->data = ctx->vars.host_header.data;
2237
ngx_http_proxy_port_variable(ngx_http_request_t *r,
2238
ngx_http_variable_value_t *v, uintptr_t data)
2240
ngx_http_proxy_ctx_t *ctx;
2242
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
2249
v->len = ctx->vars.port.len;
2251
v->no_cacheable = 0;
2253
v->data = ctx->vars.port.data;
2260
ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
2261
ngx_http_variable_value_t *v, uintptr_t data)
2266
v->no_cacheable = 0;
2269
if (r->headers_in.x_forwarded_for == NULL) {
2270
v->len = r->connection->addr_text.len;
2271
v->data = r->connection->addr_text.data;
2275
v->len = r->headers_in.x_forwarded_for->value.len
2276
+ sizeof(", ") - 1 + r->connection->addr_text.len;
2278
p = ngx_pnalloc(r->pool, v->len);
2285
p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
2286
r->headers_in.x_forwarded_for->value.len);
2288
*p++ = ','; *p++ = ' ';
2290
ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
2297
ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
2298
ngx_http_variable_value_t *v, uintptr_t data)
2300
ngx_http_proxy_ctx_t *ctx;
2302
ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
2310
v->no_cacheable = 0;
2313
v->data = ngx_pnalloc(r->connection->pool, NGX_SIZE_T_LEN);
2315
if (v->data == NULL) {
2319
v->len = ngx_sprintf(v->data, "%uz", ctx->internal_body_length) - v->data;
2326
ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
2332
ngx_http_proxy_rewrite_t *pr;
2333
ngx_http_proxy_loc_conf_t *plcf;
2335
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
2337
pr = plcf->redirects->elts;
2340
return NGX_DECLINED;
2343
len = h->value.len - prefix;
2345
for (i = 0; i < plcf->redirects->nelts; i++) {
2346
rc = pr[i].handler(r, h, prefix, len, &pr[i]);
2348
if (rc != NGX_DECLINED) {
2353
return NGX_DECLINED;
2358
ngx_http_proxy_rewrite_cookie(ngx_http_request_t *r, ngx_table_elt_t *h)
2363
ngx_http_proxy_loc_conf_t *plcf;
2365
p = (u_char *) ngx_strchr(h->value.data, ';');
2367
return NGX_DECLINED;
2370
prefix = p + 1 - h->value.data;
2374
plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
2376
if (plcf->cookie_domains) {
2377
p = ngx_strcasestrn(h->value.data + prefix, "domain=", 7 - 1);
2380
rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 7,
2381
plcf->cookie_domains);
2382
if (rc == NGX_ERROR) {
2386
if (rc != NGX_DECLINED) {
2392
if (plcf->cookie_paths) {
2393
p = ngx_strcasestrn(h->value.data + prefix, "path=", 5 - 1);
2396
rc = ngx_http_proxy_rewrite_cookie_value(r, h, p + 5,
2397
plcf->cookie_paths);
2398
if (rc == NGX_ERROR) {
2402
if (rc != NGX_DECLINED) {
2413
ngx_http_proxy_rewrite_cookie_value(ngx_http_request_t *r, ngx_table_elt_t *h,
2414
u_char *value, ngx_array_t *rewrites)
2420
ngx_http_proxy_rewrite_t *pr;
2422
prefix = value - h->value.data;
2424
p = (u_char *) ngx_strchr(value, ';');
2426
len = p ? (size_t) (p - value) : (h->value.len - prefix);
2428
pr = rewrites->elts;
2430
for (i = 0; i < rewrites->nelts; i++) {
2431
rc = pr[i].handler(r, h, prefix, len, &pr[i]);
2433
if (rc != NGX_DECLINED) {
2438
return NGX_DECLINED;
2443
ngx_http_proxy_rewrite_complex_handler(ngx_http_request_t *r,
2444
ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
2446
ngx_str_t pattern, replacement;
2448
if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
2452
if (pattern.len > len
2453
|| ngx_rstrncmp(h->value.data + prefix, pattern.data,
2456
return NGX_DECLINED;
2459
if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
2463
return ngx_http_proxy_rewrite(r, h, prefix, pattern.len, &replacement);
2470
ngx_http_proxy_rewrite_regex_handler(ngx_http_request_t *r, ngx_table_elt_t *h,
2471
size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
2473
ngx_str_t pattern, replacement;
2476
pattern.data = h->value.data + prefix;
2478
if (ngx_http_regex_exec(r, pr->pattern.regex, &pattern) != NGX_OK) {
2479
return NGX_DECLINED;
2482
if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
2486
if (prefix == 0 && h->value.len == len) {
2487
h->value = replacement;
2491
return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement);
2498
ngx_http_proxy_rewrite_domain_handler(ngx_http_request_t *r,
2499
ngx_table_elt_t *h, size_t prefix, size_t len, ngx_http_proxy_rewrite_t *pr)
2502
ngx_str_t pattern, replacement;
2504
if (ngx_http_complex_value(r, &pr->pattern.complex, &pattern) != NGX_OK) {
2508
p = h->value.data + prefix;
2516
if (pattern.len != len || ngx_rstrncasecmp(pattern.data, p, len) != 0) {
2517
return NGX_DECLINED;
2520
if (ngx_http_complex_value(r, &pr->replacement, &replacement) != NGX_OK) {
2524
return ngx_http_proxy_rewrite(r, h, prefix, len, &replacement);
2529
ngx_http_proxy_rewrite(ngx_http_request_t *r, ngx_table_elt_t *h, size_t prefix,
2530
size_t len, ngx_str_t *replacement)
2535
new_len = replacement->len + h->value.len - len;
2537
if (replacement->len > len) {
2539
data = ngx_pnalloc(r->pool, new_len);
2544
p = ngx_copy(data, h->value.data, prefix);
2545
p = ngx_copy(p, replacement->data, replacement->len);
2547
ngx_memcpy(p, h->value.data + prefix + len,
2548
h->value.len - len - prefix);
2550
h->value.data = data;
2553
p = ngx_copy(h->value.data + prefix, replacement->data,
2556
ngx_memmove(p, h->value.data + prefix + len,
2557
h->value.len - len - prefix);
2560
h->value.len = new_len;
2567
ngx_http_proxy_add_variables(ngx_conf_t *cf)
2569
ngx_http_variable_t *var, *v;
2571
for (v = ngx_http_proxy_vars; v->name.len; v++) {
2572
var = ngx_http_add_variable(cf, &v->name, v->flags);
2577
var->get_handler = v->get_handler;
2578
var->data = v->data;
2586
ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
2588
ngx_http_proxy_loc_conf_t *conf;
2590
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
2596
* set by ngx_pcalloc():
2598
* conf->upstream.bufs.num = 0;
2599
* conf->upstream.ignore_headers = 0;
2600
* conf->upstream.next_upstream = 0;
2601
* conf->upstream.cache_use_stale = 0;
2602
* conf->upstream.cache_methods = 0;
2603
* conf->upstream.temp_path = NULL;
2604
* conf->upstream.hide_headers_hash = { NULL, 0 };
2605
* conf->upstream.uri = { 0, NULL };
2606
* conf->upstream.location = NULL;
2607
* conf->upstream.store_lengths = NULL;
2608
* conf->upstream.store_values = NULL;
2610
* conf->method = NULL;
2611
* conf->headers_source = NULL;
2612
* conf->headers_set_len = NULL;
2613
* conf->headers_set = NULL;
2614
* conf->headers_set_hash = NULL;
2615
* conf->body_set_len = NULL;
2616
* conf->body_set = NULL;
2617
* conf->body_source = { 0, NULL };
2618
* conf->redirects = NULL;
2621
conf->upstream.store = NGX_CONF_UNSET;
2622
conf->upstream.store_access = NGX_CONF_UNSET_UINT;
2623
conf->upstream.buffering = NGX_CONF_UNSET;
2624
conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
2626
conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
2627
conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
2628
conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
2630
conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
2631
conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
2633
conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
2634
conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
2635
conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
2637
conf->upstream.pass_request_headers = NGX_CONF_UNSET;
2638
conf->upstream.pass_request_body = NGX_CONF_UNSET;
2640
#if (NGX_HTTP_CACHE)
2641
conf->upstream.cache = NGX_CONF_UNSET_PTR;
2642
conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
2643
conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
2644
conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
2645
conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
2646
conf->upstream.cache_lock = NGX_CONF_UNSET;
2647
conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
2650
conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
2651
conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
2653
conf->upstream.intercept_errors = NGX_CONF_UNSET;
2655
conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
2658
/* "proxy_cyclic_temp_file" is disabled */
2659
conf->upstream.cyclic_temp_file = 0;
2661
conf->redirect = NGX_CONF_UNSET;
2662
conf->upstream.change_buffering = 1;
2664
conf->cookie_domains = NGX_CONF_UNSET_PTR;
2665
conf->cookie_paths = NGX_CONF_UNSET_PTR;
2667
conf->http_version = NGX_CONF_UNSET_UINT;
2669
conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
2670
conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
2672
ngx_str_set(&conf->upstream.module, "proxy");
2679
ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
2681
ngx_http_proxy_loc_conf_t *prev = parent;
2682
ngx_http_proxy_loc_conf_t *conf = child;
2686
ngx_hash_init_t hash;
2687
ngx_http_core_loc_conf_t *clcf;
2688
ngx_http_proxy_rewrite_t *pr;
2689
ngx_http_script_compile_t sc;
2691
if (conf->upstream.store != 0) {
2692
ngx_conf_merge_value(conf->upstream.store,
2693
prev->upstream.store, 0);
2695
if (conf->upstream.store_lengths == NULL) {
2696
conf->upstream.store_lengths = prev->upstream.store_lengths;
2697
conf->upstream.store_values = prev->upstream.store_values;
2701
ngx_conf_merge_uint_value(conf->upstream.store_access,
2702
prev->upstream.store_access, 0600);
2704
ngx_conf_merge_value(conf->upstream.buffering,
2705
prev->upstream.buffering, 1);
2707
ngx_conf_merge_value(conf->upstream.ignore_client_abort,
2708
prev->upstream.ignore_client_abort, 0);
2710
ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
2711
prev->upstream.connect_timeout, 60000);
2713
ngx_conf_merge_msec_value(conf->upstream.send_timeout,
2714
prev->upstream.send_timeout, 60000);
2716
ngx_conf_merge_msec_value(conf->upstream.read_timeout,
2717
prev->upstream.read_timeout, 60000);
2719
ngx_conf_merge_size_value(conf->upstream.send_lowat,
2720
prev->upstream.send_lowat, 0);
2722
ngx_conf_merge_size_value(conf->upstream.buffer_size,
2723
prev->upstream.buffer_size,
2724
(size_t) ngx_pagesize);
2726
ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
2729
if (conf->upstream.bufs.num < 2) {
2730
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2731
"there must be at least 2 \"proxy_buffers\"");
2732
return NGX_CONF_ERROR;
2736
size = conf->upstream.buffer_size;
2737
if (size < conf->upstream.bufs.size) {
2738
size = conf->upstream.bufs.size;
2742
ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
2743
prev->upstream.busy_buffers_size_conf,
2744
NGX_CONF_UNSET_SIZE);
2746
if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
2747
conf->upstream.busy_buffers_size = 2 * size;
2749
conf->upstream.busy_buffers_size =
2750
conf->upstream.busy_buffers_size_conf;
2753
if (conf->upstream.busy_buffers_size < size) {
2754
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2755
"\"proxy_busy_buffers_size\" must be equal to or greater than "
2756
"the maximum of the value of \"proxy_buffer_size\" and "
2757
"one of the \"proxy_buffers\"");
2759
return NGX_CONF_ERROR;
2762
if (conf->upstream.busy_buffers_size
2763
> (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
2765
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2766
"\"proxy_busy_buffers_size\" must be less than "
2767
"the size of all \"proxy_buffers\" minus one buffer");
2769
return NGX_CONF_ERROR;
2773
ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
2774
prev->upstream.temp_file_write_size_conf,
2775
NGX_CONF_UNSET_SIZE);
2777
if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
2778
conf->upstream.temp_file_write_size = 2 * size;
2780
conf->upstream.temp_file_write_size =
2781
conf->upstream.temp_file_write_size_conf;
2784
if (conf->upstream.temp_file_write_size < size) {
2785
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2786
"\"proxy_temp_file_write_size\" must be equal to or greater "
2787
"than the maximum of the value of \"proxy_buffer_size\" and "
2788
"one of the \"proxy_buffers\"");
2790
return NGX_CONF_ERROR;
2793
ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
2794
prev->upstream.max_temp_file_size_conf,
2795
NGX_CONF_UNSET_SIZE);
2797
if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
2798
conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
2800
conf->upstream.max_temp_file_size =
2801
conf->upstream.max_temp_file_size_conf;
2804
if (conf->upstream.max_temp_file_size != 0
2805
&& conf->upstream.max_temp_file_size < size)
2807
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2808
"\"proxy_max_temp_file_size\" must be equal to zero to disable "
2809
"temporary files usage or must be equal to or greater than "
2810
"the maximum of the value of \"proxy_buffer_size\" and "
2811
"one of the \"proxy_buffers\"");
2813
return NGX_CONF_ERROR;
2817
ngx_conf_merge_bitmask_value(conf->upstream.ignore_headers,
2818
prev->upstream.ignore_headers,
2819
NGX_CONF_BITMASK_SET);
2822
ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
2823
prev->upstream.next_upstream,
2824
(NGX_CONF_BITMASK_SET
2825
|NGX_HTTP_UPSTREAM_FT_ERROR
2826
|NGX_HTTP_UPSTREAM_FT_TIMEOUT));
2828
if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
2829
conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
2830
|NGX_HTTP_UPSTREAM_FT_OFF;
2833
if (ngx_conf_merge_path_value(cf, &conf->upstream.temp_path,
2834
prev->upstream.temp_path,
2835
&ngx_http_proxy_temp_path)
2838
return NGX_CONF_ERROR;
2842
#if (NGX_HTTP_CACHE)
2844
ngx_conf_merge_ptr_value(conf->upstream.cache,
2845
prev->upstream.cache, NULL);
2847
if (conf->upstream.cache && conf->upstream.cache->data == NULL) {
2848
ngx_shm_zone_t *shm_zone;
2850
shm_zone = conf->upstream.cache;
2852
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2853
"\"proxy_cache\" zone \"%V\" is unknown",
2854
&shm_zone->shm.name);
2856
return NGX_CONF_ERROR;
2859
ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
2860
prev->upstream.cache_min_uses, 1);
2862
ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
2863
prev->upstream.cache_use_stale,
2864
(NGX_CONF_BITMASK_SET
2865
|NGX_HTTP_UPSTREAM_FT_OFF));
2867
if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_OFF) {
2868
conf->upstream.cache_use_stale = NGX_CONF_BITMASK_SET
2869
|NGX_HTTP_UPSTREAM_FT_OFF;
2872
if (conf->upstream.cache_use_stale & NGX_HTTP_UPSTREAM_FT_ERROR) {
2873
conf->upstream.cache_use_stale |= NGX_HTTP_UPSTREAM_FT_NOLIVE;
2876
if (conf->upstream.cache_methods == 0) {
2877
conf->upstream.cache_methods = prev->upstream.cache_methods;
2880
conf->upstream.cache_methods |= NGX_HTTP_GET|NGX_HTTP_HEAD;
2882
ngx_conf_merge_ptr_value(conf->upstream.cache_bypass,
2883
prev->upstream.cache_bypass, NULL);
2885
ngx_conf_merge_ptr_value(conf->upstream.no_cache,
2886
prev->upstream.no_cache, NULL);
2888
if (conf->upstream.no_cache && conf->upstream.cache_bypass == NULL) {
2889
ngx_log_error(NGX_LOG_WARN, cf->log, 0,
2890
"\"proxy_no_cache\" functionality has been changed in 0.8.46, "
2891
"now it should be used together with \"proxy_cache_bypass\"");
2894
ngx_conf_merge_ptr_value(conf->upstream.cache_valid,
2895
prev->upstream.cache_valid, NULL);
2897
if (conf->cache_key.value.data == NULL) {
2898
conf->cache_key = prev->cache_key;
2901
ngx_conf_merge_value(conf->upstream.cache_lock,
2902
prev->upstream.cache_lock, 0);
2904
ngx_conf_merge_msec_value(conf->upstream.cache_lock_timeout,
2905
prev->upstream.cache_lock_timeout, 5000);
2909
if (conf->method.len == 0) {
2910
conf->method = prev->method;
2913
conf->method.data[conf->method.len] = ' ';
2917
ngx_conf_merge_value(conf->upstream.pass_request_headers,
2918
prev->upstream.pass_request_headers, 1);
2919
ngx_conf_merge_value(conf->upstream.pass_request_body,
2920
prev->upstream.pass_request_body, 1);
2922
ngx_conf_merge_value(conf->upstream.intercept_errors,
2923
prev->upstream.intercept_errors, 0);
2926
ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
2927
prev->upstream.ssl_session_reuse, 1);
2930
ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
2932
if (conf->redirect) {
2934
if (conf->redirects == NULL) {
2935
conf->redirects = prev->redirects;
2938
if (conf->redirects == NULL && conf->url.data) {
2940
conf->redirects = ngx_array_create(cf->pool, 1,
2941
sizeof(ngx_http_proxy_rewrite_t));
2942
if (conf->redirects == NULL) {
2943
return NGX_CONF_ERROR;
2946
pr = ngx_array_push(conf->redirects);
2948
return NGX_CONF_ERROR;
2951
ngx_memzero(&pr->pattern.complex,
2952
sizeof(ngx_http_complex_value_t));
2954
ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));
2956
pr->handler = ngx_http_proxy_rewrite_complex_handler;
2958
if (conf->vars.uri.len) {
2959
pr->pattern.complex.value = conf->url;
2960
pr->replacement.value = conf->location;
2963
pr->pattern.complex.value.len = conf->url.len
2966
p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
2968
return NGX_CONF_ERROR;
2971
pr->pattern.complex.value.data = p;
2973
p = ngx_cpymem(p, conf->url.data, conf->url.len);
2976
ngx_str_set(&pr->replacement.value, "/");
2981
ngx_conf_merge_ptr_value(conf->cookie_domains, prev->cookie_domains, NULL);
2983
ngx_conf_merge_ptr_value(conf->cookie_paths, prev->cookie_paths, NULL);
2986
if (conf->upstream.ssl == NULL) {
2987
conf->upstream.ssl = prev->upstream.ssl;
2991
ngx_conf_merge_uint_value(conf->http_version, prev->http_version,
2992
NGX_HTTP_VERSION_10);
2994
ngx_conf_merge_uint_value(conf->headers_hash_max_size,
2995
prev->headers_hash_max_size, 512);
2997
ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
2998
prev->headers_hash_bucket_size, 64);
3000
conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
3001
ngx_cacheline_size);
3003
hash.max_size = conf->headers_hash_max_size;
3004
hash.bucket_size = conf->headers_hash_bucket_size;
3005
hash.name = "proxy_headers_hash";
3007
if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
3008
&prev->upstream, ngx_http_proxy_hide_headers, &hash)
3011
return NGX_CONF_ERROR;
3014
if (conf->upstream.upstream == NULL) {
3015
conf->upstream.upstream = prev->upstream.upstream;
3016
conf->vars = prev->vars;
3019
if (conf->proxy_lengths == NULL) {
3020
conf->proxy_lengths = prev->proxy_lengths;
3021
conf->proxy_values = prev->proxy_values;
3024
if (conf->upstream.upstream || conf->proxy_lengths) {
3025
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
3026
if (clcf->handler == NULL && clcf->lmt_excpt) {
3027
clcf->handler = ngx_http_proxy_handler;
3028
conf->location = prev->location;
3032
if (conf->body_source.data == NULL) {
3033
conf->body_source = prev->body_source;
3034
conf->body_set_len = prev->body_set_len;
3035
conf->body_set = prev->body_set;
3038
if (conf->body_source.data && conf->body_set_len == NULL) {
3040
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
3043
sc.source = &conf->body_source;
3044
sc.flushes = &conf->flushes;
3045
sc.lengths = &conf->body_set_len;
3046
sc.values = &conf->body_set;
3047
sc.complete_lengths = 1;
3048
sc.complete_values = 1;
3050
if (ngx_http_script_compile(&sc) != NGX_OK) {
3051
return NGX_CONF_ERROR;
3055
if (ngx_http_proxy_merge_headers(cf, conf, prev) != NGX_OK) {
3056
return NGX_CONF_ERROR;
3064
ngx_http_proxy_merge_headers(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *conf,
3065
ngx_http_proxy_loc_conf_t *prev)
3071
ngx_array_t headers_names, headers_merged;
3072
ngx_keyval_t *src, *s, *h;
3074
ngx_hash_init_t hash;
3075
ngx_http_script_compile_t sc;
3076
ngx_http_script_copy_code_t *copy;
3078
if (conf->headers_source == NULL) {
3079
conf->flushes = prev->flushes;
3080
conf->headers_set_len = prev->headers_set_len;
3081
conf->headers_set = prev->headers_set;
3082
conf->headers_set_hash = prev->headers_set_hash;
3083
conf->headers_source = prev->headers_source;
3086
if (conf->headers_set_hash.buckets
3087
&& ((conf->body_source.data == NULL)
3088
== (prev->body_source.data == NULL))
3089
#if (NGX_HTTP_CACHE)
3090
&& ((conf->upstream.cache == NULL) == (prev->upstream.cache == NULL))
3098
if (ngx_array_init(&headers_names, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
3104
if (ngx_array_init(&headers_merged, cf->temp_pool, 4, sizeof(ngx_keyval_t))
3110
if (conf->headers_source == NULL) {
3111
conf->headers_source = ngx_array_create(cf->pool, 4,
3112
sizeof(ngx_keyval_t));
3113
if (conf->headers_source == NULL) {
3118
conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
3119
if (conf->headers_set_len == NULL) {
3123
conf->headers_set = ngx_array_create(cf->pool, 512, 1);
3124
if (conf->headers_set == NULL) {
3129
#if (NGX_HTTP_CACHE)
3131
h = conf->upstream.cache ? ngx_http_proxy_cache_headers:
3132
ngx_http_proxy_headers;
3135
h = ngx_http_proxy_headers;
3139
src = conf->headers_source->elts;
3140
for (i = 0; i < conf->headers_source->nelts; i++) {
3142
s = ngx_array_push(&headers_merged);
3150
while (h->key.len) {
3152
src = headers_merged.elts;
3153
for (i = 0; i < headers_merged.nelts; i++) {
3154
if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
3159
s = ngx_array_push(&headers_merged);
3171
if (conf->body_source.data) {
3172
s = ngx_array_push(&headers_merged);
3177
ngx_str_set(&s->key, "Content-Length");
3178
ngx_str_set(&s->value, "$proxy_internal_body_length");
3182
src = headers_merged.elts;
3183
for (i = 0; i < headers_merged.nelts; i++) {
3185
hk = ngx_array_push(&headers_names);
3190
hk->key = src[i].key;
3191
hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
3192
hk->value = (void *) 1;
3194
if (src[i].value.len == 0) {
3198
if (ngx_http_script_variables_count(&src[i].value) == 0) {
3199
copy = ngx_array_push_n(conf->headers_set_len,
3200
sizeof(ngx_http_script_copy_code_t));
3205
copy->code = (ngx_http_script_code_pt)
3206
ngx_http_script_copy_len_code;
3207
copy->len = src[i].key.len + sizeof(": ") - 1
3208
+ src[i].value.len + sizeof(CRLF) - 1;
3211
size = (sizeof(ngx_http_script_copy_code_t)
3212
+ src[i].key.len + sizeof(": ") - 1
3213
+ src[i].value.len + sizeof(CRLF) - 1
3214
+ sizeof(uintptr_t) - 1)
3215
& ~(sizeof(uintptr_t) - 1);
3217
copy = ngx_array_push_n(conf->headers_set, size);
3222
copy->code = ngx_http_script_copy_code;
3223
copy->len = src[i].key.len + sizeof(": ") - 1
3224
+ src[i].value.len + sizeof(CRLF) - 1;
3226
p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
3228
p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
3229
*p++ = ':'; *p++ = ' ';
3230
p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
3234
copy = ngx_array_push_n(conf->headers_set_len,
3235
sizeof(ngx_http_script_copy_code_t));
3240
copy->code = (ngx_http_script_code_pt)
3241
ngx_http_script_copy_len_code;
3242
copy->len = src[i].key.len + sizeof(": ") - 1;
3245
size = (sizeof(ngx_http_script_copy_code_t)
3246
+ src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
3247
& ~(sizeof(uintptr_t) - 1);
3249
copy = ngx_array_push_n(conf->headers_set, size);
3254
copy->code = ngx_http_script_copy_code;
3255
copy->len = src[i].key.len + sizeof(": ") - 1;
3257
p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
3258
p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
3259
*p++ = ':'; *p = ' ';
3262
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
3265
sc.source = &src[i].value;
3266
sc.flushes = &conf->flushes;
3267
sc.lengths = &conf->headers_set_len;
3268
sc.values = &conf->headers_set;
3270
if (ngx_http_script_compile(&sc) != NGX_OK) {
3275
copy = ngx_array_push_n(conf->headers_set_len,
3276
sizeof(ngx_http_script_copy_code_t));
3281
copy->code = (ngx_http_script_code_pt)
3282
ngx_http_script_copy_len_code;
3283
copy->len = sizeof(CRLF) - 1;
3286
size = (sizeof(ngx_http_script_copy_code_t)
3287
+ sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
3288
& ~(sizeof(uintptr_t) - 1);
3290
copy = ngx_array_push_n(conf->headers_set, size);
3295
copy->code = ngx_http_script_copy_code;
3296
copy->len = sizeof(CRLF) - 1;
3298
p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
3302
code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
3307
*code = (uintptr_t) NULL;
3309
code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
3314
*code = (uintptr_t) NULL;
3317
code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
3322
*code = (uintptr_t) NULL;
3325
hash.hash = &conf->headers_set_hash;
3326
hash.key = ngx_hash_key_lc;
3327
hash.max_size = conf->headers_hash_max_size;
3328
hash.bucket_size = conf->headers_hash_bucket_size;
3329
hash.name = "proxy_headers_hash";
3330
hash.pool = cf->pool;
3331
hash.temp_pool = NULL;
3333
return ngx_hash_init(&hash, headers_names.elts, headers_names.nelts);
3338
ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3340
ngx_http_proxy_loc_conf_t *plcf = conf;
3344
ngx_str_t *value, *url;
3347
ngx_http_core_loc_conf_t *clcf;
3348
ngx_http_script_compile_t sc;
3350
if (plcf->upstream.upstream || plcf->proxy_lengths) {
3351
return "is duplicate";
3354
clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
3356
clcf->handler = ngx_http_proxy_handler;
3358
if (clcf->name.data[clcf->name.len - 1] == '/') {
3359
clcf->auto_redirect = 1;
3362
value = cf->args->elts;
3366
n = ngx_http_script_variables_count(url);
3370
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
3374
sc.lengths = &plcf->proxy_lengths;
3375
sc.values = &plcf->proxy_values;
3377
sc.complete_lengths = 1;
3378
sc.complete_values = 1;
3380
if (ngx_http_script_compile(&sc) != NGX_OK) {
3381
return NGX_CONF_ERROR;
3385
if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
3386
return NGX_CONF_ERROR;
3393
if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
3397
} else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
3400
if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
3401
return NGX_CONF_ERROR;
3407
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3408
"https protocol requires SSL support");
3409
return NGX_CONF_ERROR;
3413
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
3414
return NGX_CONF_ERROR;
3417
ngx_memzero(&u, sizeof(ngx_url_t));
3419
u.url.len = url->len - add;
3420
u.url.data = url->data + add;
3421
u.default_port = port;
3425
plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
3426
if (plcf->upstream.upstream == NULL) {
3427
return NGX_CONF_ERROR;
3430
plcf->vars.schema.len = add;
3431
plcf->vars.schema.data = url->data;
3432
plcf->vars.key_start = plcf->vars.schema;
3434
ngx_http_proxy_set_vars(&u, &plcf->vars);
3436
plcf->location = clcf->name;
3444
if (plcf->vars.uri.len) {
3445
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3446
"\"proxy_pass\" cannot have URI part in "
3447
"location given by regular expression, "
3448
"or inside named location, "
3449
"or inside \"if\" statement, "
3450
"or inside \"limit_except\" block");
3451
return NGX_CONF_ERROR;
3454
plcf->location.len = 0;
3464
ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3466
ngx_http_proxy_loc_conf_t *plcf = conf;
3470
ngx_http_proxy_rewrite_t *pr;
3471
ngx_http_compile_complex_value_t ccv;
3473
if (plcf->redirect == 0) {
3479
value = cf->args->elts;
3481
if (cf->args->nelts == 2) {
3482
if (ngx_strcmp(value[1].data, "off") == 0) {
3484
plcf->redirects = NULL;
3488
if (ngx_strcmp(value[1].data, "false") == 0) {
3489
ngx_conf_log_error(NGX_LOG_ERR, cf, 0,
3490
"invalid parameter \"false\", use \"off\" instead");
3492
plcf->redirects = NULL;
3496
if (ngx_strcmp(value[1].data, "default") != 0) {
3497
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3498
"invalid parameter \"%V\"", &value[1]);
3499
return NGX_CONF_ERROR;
3503
if (plcf->redirects == NULL) {
3504
plcf->redirects = ngx_array_create(cf->pool, 1,
3505
sizeof(ngx_http_proxy_rewrite_t));
3506
if (plcf->redirects == NULL) {
3507
return NGX_CONF_ERROR;
3511
pr = ngx_array_push(plcf->redirects);
3513
return NGX_CONF_ERROR;
3516
if (ngx_strcmp(value[1].data, "default") == 0) {
3517
if (plcf->proxy_lengths) {
3518
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3519
"\"proxy_redirect default\" cannot be used "
3520
"with \"proxy_pass\" directive with variables");
3521
return NGX_CONF_ERROR;
3524
if (plcf->url.data == NULL) {
3525
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3526
"\"proxy_redirect default\" should be placed "
3527
"after the \"proxy_pass\" directive");
3528
return NGX_CONF_ERROR;
3531
pr->handler = ngx_http_proxy_rewrite_complex_handler;
3533
ngx_memzero(&pr->pattern.complex, sizeof(ngx_http_complex_value_t));
3535
ngx_memzero(&pr->replacement, sizeof(ngx_http_complex_value_t));
3537
if (plcf->vars.uri.len) {
3538
pr->pattern.complex.value = plcf->url;
3539
pr->replacement.value = plcf->location;
3542
pr->pattern.complex.value.len = plcf->url.len + sizeof("/") - 1;
3544
p = ngx_pnalloc(cf->pool, pr->pattern.complex.value.len);
3546
return NGX_CONF_ERROR;
3549
pr->pattern.complex.value.data = p;
3551
p = ngx_cpymem(p, plcf->url.data, plcf->url.len);
3554
ngx_str_set(&pr->replacement.value, "/");
3561
if (value[1].data[0] == '~') {
3565
if (value[1].data[0] == '*') {
3569
if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
3570
return NGX_CONF_ERROR;
3574
if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
3575
return NGX_CONF_ERROR;
3581
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3584
ccv.value = &value[1];
3585
ccv.complex_value = &pr->pattern.complex;
3587
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3588
return NGX_CONF_ERROR;
3591
pr->handler = ngx_http_proxy_rewrite_complex_handler;
3595
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3598
ccv.value = &value[2];
3599
ccv.complex_value = &pr->replacement;
3601
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3602
return NGX_CONF_ERROR;
3610
ngx_http_proxy_cookie_domain(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3612
ngx_http_proxy_loc_conf_t *plcf = conf;
3615
ngx_http_proxy_rewrite_t *pr;
3616
ngx_http_compile_complex_value_t ccv;
3618
if (plcf->cookie_domains == NULL) {
3622
value = cf->args->elts;
3624
if (cf->args->nelts == 2) {
3626
if (ngx_strcmp(value[1].data, "off") == 0) {
3627
plcf->cookie_domains = NULL;
3631
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3632
"invalid parameter \"%V\"", &value[1]);
3633
return NGX_CONF_ERROR;
3636
if (plcf->cookie_domains == NGX_CONF_UNSET_PTR) {
3637
plcf->cookie_domains = ngx_array_create(cf->pool, 1,
3638
sizeof(ngx_http_proxy_rewrite_t));
3639
if (plcf->cookie_domains == NULL) {
3640
return NGX_CONF_ERROR;
3644
pr = ngx_array_push(plcf->cookie_domains);
3646
return NGX_CONF_ERROR;
3649
if (value[1].data[0] == '~') {
3653
if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
3654
return NGX_CONF_ERROR;
3659
if (value[1].data[0] == '.') {
3664
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3667
ccv.value = &value[1];
3668
ccv.complex_value = &pr->pattern.complex;
3670
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3671
return NGX_CONF_ERROR;
3674
pr->handler = ngx_http_proxy_rewrite_domain_handler;
3676
if (value[2].data[0] == '.') {
3682
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3685
ccv.value = &value[2];
3686
ccv.complex_value = &pr->replacement;
3688
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3689
return NGX_CONF_ERROR;
3697
ngx_http_proxy_cookie_path(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3699
ngx_http_proxy_loc_conf_t *plcf = conf;
3702
ngx_http_proxy_rewrite_t *pr;
3703
ngx_http_compile_complex_value_t ccv;
3705
if (plcf->cookie_paths == NULL) {
3709
value = cf->args->elts;
3711
if (cf->args->nelts == 2) {
3713
if (ngx_strcmp(value[1].data, "off") == 0) {
3714
plcf->cookie_paths = NULL;
3718
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3719
"invalid parameter \"%V\"", &value[1]);
3720
return NGX_CONF_ERROR;
3723
if (plcf->cookie_paths == NGX_CONF_UNSET_PTR) {
3724
plcf->cookie_paths = ngx_array_create(cf->pool, 1,
3725
sizeof(ngx_http_proxy_rewrite_t));
3726
if (plcf->cookie_paths == NULL) {
3727
return NGX_CONF_ERROR;
3731
pr = ngx_array_push(plcf->cookie_paths);
3733
return NGX_CONF_ERROR;
3736
if (value[1].data[0] == '~') {
3740
if (value[1].data[0] == '*') {
3744
if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 1) != NGX_OK) {
3745
return NGX_CONF_ERROR;
3749
if (ngx_http_proxy_rewrite_regex(cf, pr, &value[1], 0) != NGX_OK) {
3750
return NGX_CONF_ERROR;
3756
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3759
ccv.value = &value[1];
3760
ccv.complex_value = &pr->pattern.complex;
3762
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3763
return NGX_CONF_ERROR;
3766
pr->handler = ngx_http_proxy_rewrite_complex_handler;
3769
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3772
ccv.value = &value[2];
3773
ccv.complex_value = &pr->replacement;
3775
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3776
return NGX_CONF_ERROR;
3784
ngx_http_proxy_rewrite_regex(ngx_conf_t *cf, ngx_http_proxy_rewrite_t *pr,
3785
ngx_str_t *regex, ngx_uint_t caseless)
3788
u_char errstr[NGX_MAX_CONF_ERRSTR];
3789
ngx_regex_compile_t rc;
3791
ngx_memzero(&rc, sizeof(ngx_regex_compile_t));
3793
rc.pattern = *regex;
3794
rc.err.len = NGX_MAX_CONF_ERRSTR;
3795
rc.err.data = errstr;
3798
rc.options = NGX_REGEX_CASELESS;
3801
pr->pattern.regex = ngx_http_regex_compile(cf, &rc);
3802
if (pr->pattern.regex == NULL) {
3806
pr->handler = ngx_http_proxy_rewrite_regex_handler;
3812
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3813
"using regex \"%V\" requires PCRE library", regex);
3821
ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3823
ngx_http_proxy_loc_conf_t *plcf = conf;
3826
ngx_http_script_compile_t sc;
3828
if (plcf->upstream.store != NGX_CONF_UNSET
3829
|| plcf->upstream.store_lengths)
3831
return "is duplicate";
3834
value = cf->args->elts;
3836
if (ngx_strcmp(value[1].data, "off") == 0) {
3837
plcf->upstream.store = 0;
3841
#if (NGX_HTTP_CACHE)
3843
if (plcf->upstream.cache != NGX_CONF_UNSET_PTR
3844
&& plcf->upstream.cache != NULL)
3846
return "is incompatible with \"proxy_cache\"";
3851
if (ngx_strcmp(value[1].data, "on") == 0) {
3852
plcf->upstream.store = 1;
3856
/* include the terminating '\0' into script */
3859
ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
3862
sc.source = &value[1];
3863
sc.lengths = &plcf->upstream.store_lengths;
3864
sc.values = &plcf->upstream.store_values;
3865
sc.variables = ngx_http_script_variables_count(&value[1]);
3866
sc.complete_lengths = 1;
3867
sc.complete_values = 1;
3869
if (ngx_http_script_compile(&sc) != NGX_OK) {
3870
return NGX_CONF_ERROR;
3877
#if (NGX_HTTP_CACHE)
3880
ngx_http_proxy_cache(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3882
ngx_http_proxy_loc_conf_t *plcf = conf;
3886
value = cf->args->elts;
3888
if (plcf->upstream.cache != NGX_CONF_UNSET_PTR) {
3889
return "is duplicate";
3892
if (ngx_strcmp(value[1].data, "off") == 0) {
3893
plcf->upstream.cache = NULL;
3897
if (plcf->upstream.store > 0 || plcf->upstream.store_lengths) {
3898
return "is incompatible with \"proxy_store\"";
3901
plcf->upstream.cache = ngx_shared_memory_add(cf, &value[1], 0,
3902
&ngx_http_proxy_module);
3903
if (plcf->upstream.cache == NULL) {
3904
return NGX_CONF_ERROR;
3912
ngx_http_proxy_cache_key(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
3914
ngx_http_proxy_loc_conf_t *plcf = conf;
3917
ngx_http_compile_complex_value_t ccv;
3919
value = cf->args->elts;
3921
if (plcf->cache_key.value.len) {
3922
return "is duplicate";
3925
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
3928
ccv.value = &value[1];
3929
ccv.complex_value = &plcf->cache_key;
3931
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
3932
return NGX_CONF_ERROR;
3942
ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
3947
if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
3948
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
3949
"\"proxy_send_lowat\" must be less than %d "
3950
"(sysctl net.inet.tcp.sendspace)",
3951
ngx_freebsd_net_inet_tcp_sendspace);
3953
return NGX_CONF_ERROR;
3956
#elif !(NGX_HAVE_SO_SNDLOWAT)
3959
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
3960
"\"proxy_send_lowat\" is not supported, ignored");
3973
ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
3975
ngx_pool_cleanup_t *cln;
3977
plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
3978
if (plcf->upstream.ssl == NULL) {
3982
plcf->upstream.ssl->log = cf->log;
3984
if (ngx_ssl_create(plcf->upstream.ssl,
3985
NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1
3986
|NGX_SSL_TLSv1_1|NGX_SSL_TLSv1_2,
3993
cln = ngx_pool_cleanup_add(cf->pool, 0);
3998
cln->handler = ngx_ssl_cleanup_ctx;
3999
cln->data = plcf->upstream.ssl;
4008
ngx_http_proxy_set_vars(ngx_url_t *u, ngx_http_proxy_vars_t *v)
4010
if (u->family != AF_UNIX) {
4012
if (u->no_port || u->port == u->default_port) {
4014
v->host_header = u->host;
4016
if (u->default_port == 80) {
4017
ngx_str_set(&v->port, "80");
4020
ngx_str_set(&v->port, "443");
4024
v->host_header.len = u->host.len + 1 + u->port_text.len;
4025
v->host_header.data = u->host.data;
4026
v->port = u->port_text;
4029
v->key_start.len += v->host_header.len;
4032
ngx_str_set(&v->host_header, "localhost");
4033
ngx_str_null(&v->port);
4034
v->key_start.len += sizeof("unix:") - 1 + u->host.len + 1;