3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 ngx_http_upstream_conf_t upstream;
18 ngx_array_t *params_len;
20 ngx_array_t *params_source;
21 ngx_array_t *catch_stderr;
23 ngx_array_t *fastcgi_lengths;
24 ngx_array_t *fastcgi_values;
27 ngx_regex_t *split_regex;
30 } ngx_http_fastcgi_loc_conf_t;
34 ngx_http_fastcgi_st_version = 0,
35 ngx_http_fastcgi_st_type,
36 ngx_http_fastcgi_st_request_id_hi,
37 ngx_http_fastcgi_st_request_id_lo,
38 ngx_http_fastcgi_st_content_length_hi,
39 ngx_http_fastcgi_st_content_length_lo,
40 ngx_http_fastcgi_st_padding_length,
41 ngx_http_fastcgi_st_reserved,
42 ngx_http_fastcgi_st_data,
43 ngx_http_fastcgi_st_padding
44 } ngx_http_fastcgi_state_e;
50 } ngx_http_fastcgi_split_part_t;
54 ngx_http_fastcgi_state_e state;
61 ngx_uint_t fastcgi_stdout; /* unsigned :1 */
63 ngx_array_t *split_parts;
65 ngx_str_t script_name;
67 } ngx_http_fastcgi_ctx_t;
70 #define NGX_HTTP_FASTCGI_RESPONDER 1
72 #define NGX_HTTP_FASTCGI_BEGIN_REQUEST 1
73 #define NGX_HTTP_FASTCGI_ABORT_REQUEST 2
74 #define NGX_HTTP_FASTCGI_END_REQUEST 3
75 #define NGX_HTTP_FASTCGI_PARAMS 4
76 #define NGX_HTTP_FASTCGI_STDIN 5
77 #define NGX_HTTP_FASTCGI_STDOUT 6
78 #define NGX_HTTP_FASTCGI_STDERR 7
79 #define NGX_HTTP_FASTCGI_DATA 8
87 u_char content_length_hi;
88 u_char content_length_lo;
89 u_char padding_length;
91 } ngx_http_fastcgi_header_t;
99 } ngx_http_fastcgi_begin_request_t;
105 u_char request_id_hi;
106 u_char request_id_lo;
107 } ngx_http_fastcgi_header_small_t;
111 ngx_http_fastcgi_header_t h0;
112 ngx_http_fastcgi_begin_request_t br;
113 ngx_http_fastcgi_header_small_t h1;
114 } ngx_http_fastcgi_request_start_t;
117 static ngx_int_t ngx_http_fastcgi_eval(ngx_http_request_t *r,
118 ngx_http_fastcgi_loc_conf_t *flcf);
119 static ngx_int_t ngx_http_fastcgi_create_request(ngx_http_request_t *r);
120 static ngx_int_t ngx_http_fastcgi_reinit_request(ngx_http_request_t *r);
121 static ngx_int_t ngx_http_fastcgi_process_header(ngx_http_request_t *r);
122 static ngx_int_t ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p,
124 static ngx_int_t ngx_http_fastcgi_process_record(ngx_http_request_t *r,
125 ngx_http_fastcgi_ctx_t *f);
126 static void ngx_http_fastcgi_abort_request(ngx_http_request_t *r);
127 static void ngx_http_fastcgi_finalize_request(ngx_http_request_t *r,
130 static ngx_int_t ngx_http_fastcgi_add_variables(ngx_conf_t *cf);
131 static void *ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf);
132 static char *ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf,
133 void *parent, void *child);
134 static ngx_int_t ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r,
135 ngx_http_variable_value_t *v, uintptr_t data);
136 static ngx_int_t ngx_http_fastcgi_path_info_variable(ngx_http_request_t *r,
137 ngx_http_variable_value_t *v, uintptr_t data);
138 static ngx_http_fastcgi_ctx_t *ngx_http_fastcgi_split(ngx_http_request_t *r,
139 ngx_http_fastcgi_loc_conf_t *flcf);
141 static char *ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd,
143 static char *ngx_http_fastcgi_split_path_info(ngx_conf_t *cf,
144 ngx_command_t *cmd, void *conf);
145 static char *ngx_http_fastcgi_store(ngx_conf_t *cf, ngx_command_t *cmd,
147 static char *ngx_http_fastcgi_lowat_check(ngx_conf_t *cf, void *post,
150 static char *ngx_http_fastcgi_upstream_max_fails_unsupported(ngx_conf_t *cf,
151 ngx_command_t *cmd, void *conf);
152 static char *ngx_http_fastcgi_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
153 ngx_command_t *cmd, void *conf);
156 static ngx_conf_post_t ngx_http_fastcgi_lowat_post =
157 { ngx_http_fastcgi_lowat_check };
160 static ngx_conf_bitmask_t ngx_http_fastcgi_next_upstream_masks[] = {
161 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
162 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
163 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
164 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
165 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
166 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
167 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
168 { ngx_null_string, 0 }
172 static ngx_command_t ngx_http_fastcgi_commands[] = {
174 { ngx_string("fastcgi_pass"),
175 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
176 ngx_http_fastcgi_pass,
177 NGX_HTTP_LOC_CONF_OFFSET,
181 { ngx_string("fastcgi_index"),
182 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
183 ngx_conf_set_str_slot,
184 NGX_HTTP_LOC_CONF_OFFSET,
185 offsetof(ngx_http_fastcgi_loc_conf_t, index),
188 { ngx_string("fastcgi_split_path_info"),
189 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
190 ngx_http_fastcgi_split_path_info,
191 NGX_HTTP_LOC_CONF_OFFSET,
195 { ngx_string("fastcgi_store"),
196 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
197 ngx_http_fastcgi_store,
198 NGX_HTTP_LOC_CONF_OFFSET,
202 { ngx_string("fastcgi_store_access"),
203 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
204 ngx_conf_set_access_slot,
205 NGX_HTTP_LOC_CONF_OFFSET,
206 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.store_access),
209 { ngx_string("fastcgi_ignore_client_abort"),
210 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
211 ngx_conf_set_flag_slot,
212 NGX_HTTP_LOC_CONF_OFFSET,
213 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.ignore_client_abort),
216 { ngx_string("fastcgi_connect_timeout"),
217 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
218 ngx_conf_set_msec_slot,
219 NGX_HTTP_LOC_CONF_OFFSET,
220 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.connect_timeout),
223 { ngx_string("fastcgi_send_timeout"),
224 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
225 ngx_conf_set_msec_slot,
226 NGX_HTTP_LOC_CONF_OFFSET,
227 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.send_timeout),
230 { ngx_string("fastcgi_send_lowat"),
231 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
232 ngx_conf_set_size_slot,
233 NGX_HTTP_LOC_CONF_OFFSET,
234 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.send_lowat),
235 &ngx_http_fastcgi_lowat_post },
237 { ngx_string("fastcgi_buffer_size"),
238 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
239 ngx_conf_set_size_slot,
240 NGX_HTTP_LOC_CONF_OFFSET,
241 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.buffer_size),
244 { ngx_string("fastcgi_pass_request_headers"),
245 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
246 ngx_conf_set_flag_slot,
247 NGX_HTTP_LOC_CONF_OFFSET,
248 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_request_headers),
251 { ngx_string("fastcgi_pass_request_body"),
252 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
253 ngx_conf_set_flag_slot,
254 NGX_HTTP_LOC_CONF_OFFSET,
255 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_request_body),
258 { ngx_string("fastcgi_intercept_errors"),
259 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
260 ngx_conf_set_flag_slot,
261 NGX_HTTP_LOC_CONF_OFFSET,
262 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.intercept_errors),
265 { ngx_string("fastcgi_read_timeout"),
266 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
267 ngx_conf_set_msec_slot,
268 NGX_HTTP_LOC_CONF_OFFSET,
269 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.read_timeout),
272 { ngx_string("fastcgi_buffers"),
273 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
274 ngx_conf_set_bufs_slot,
275 NGX_HTTP_LOC_CONF_OFFSET,
276 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.bufs),
279 { ngx_string("fastcgi_busy_buffers_size"),
280 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
281 ngx_conf_set_size_slot,
282 NGX_HTTP_LOC_CONF_OFFSET,
283 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.busy_buffers_size_conf),
286 { ngx_string("fastcgi_temp_path"),
287 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
288 ngx_conf_set_path_slot,
289 NGX_HTTP_LOC_CONF_OFFSET,
290 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.temp_path),
291 (void *) ngx_garbage_collector_temp_handler },
293 { ngx_string("fastcgi_max_temp_file_size"),
294 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
295 ngx_conf_set_size_slot,
296 NGX_HTTP_LOC_CONF_OFFSET,
297 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.max_temp_file_size_conf),
300 { ngx_string("fastcgi_temp_file_write_size"),
301 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
302 ngx_conf_set_size_slot,
303 NGX_HTTP_LOC_CONF_OFFSET,
304 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.temp_file_write_size_conf),
307 { ngx_string("fastcgi_next_upstream"),
308 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
309 ngx_conf_set_bitmask_slot,
310 NGX_HTTP_LOC_CONF_OFFSET,
311 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.next_upstream),
312 &ngx_http_fastcgi_next_upstream_masks },
314 { ngx_string("fastcgi_upstream_max_fails"),
315 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
316 ngx_http_fastcgi_upstream_max_fails_unsupported,
321 { ngx_string("fastcgi_upstream_fail_timeout"),
322 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
323 ngx_http_fastcgi_upstream_fail_timeout_unsupported,
328 { ngx_string("fastcgi_param"),
329 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
330 ngx_conf_set_keyval_slot,
331 NGX_HTTP_LOC_CONF_OFFSET,
332 offsetof(ngx_http_fastcgi_loc_conf_t, params_source),
335 { ngx_string("fastcgi_pass_header"),
336 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
337 ngx_conf_set_str_array_slot,
338 NGX_HTTP_LOC_CONF_OFFSET,
339 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.pass_headers),
342 { ngx_string("fastcgi_hide_header"),
343 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
344 ngx_conf_set_str_array_slot,
345 NGX_HTTP_LOC_CONF_OFFSET,
346 offsetof(ngx_http_fastcgi_loc_conf_t, upstream.hide_headers),
349 { ngx_string("fastcgi_catch_stderr"),
350 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
351 ngx_conf_set_str_array_slot,
352 NGX_HTTP_LOC_CONF_OFFSET,
353 offsetof(ngx_http_fastcgi_loc_conf_t, catch_stderr),
360 static ngx_http_module_t ngx_http_fastcgi_module_ctx = {
361 ngx_http_fastcgi_add_variables, /* preconfiguration */
362 NULL, /* postconfiguration */
364 NULL, /* create main configuration */
365 NULL, /* init main configuration */
367 NULL, /* create server configuration */
368 NULL, /* merge server configuration */
370 ngx_http_fastcgi_create_loc_conf, /* create location configuration */
371 ngx_http_fastcgi_merge_loc_conf /* merge location configuration */
375 ngx_module_t ngx_http_fastcgi_module = {
377 &ngx_http_fastcgi_module_ctx, /* module context */
378 ngx_http_fastcgi_commands, /* module directives */
379 NGX_HTTP_MODULE, /* module type */
380 NULL, /* init master */
381 NULL, /* init module */
382 NULL, /* init process */
383 NULL, /* init thread */
384 NULL, /* exit thread */
385 NULL, /* exit process */
386 NULL, /* exit master */
387 NGX_MODULE_V1_PADDING
391 static ngx_http_fastcgi_request_start_t ngx_http_fastcgi_request_start = {
393 NGX_HTTP_FASTCGI_BEGIN_REQUEST, /* type */
394 0, /* request_id_hi */
395 1, /* request_id_lo */
396 0, /* content_length_hi */
397 sizeof(ngx_http_fastcgi_begin_request_t), /* content_length_lo */
398 0, /* padding_length */
402 NGX_HTTP_FASTCGI_RESPONDER, /* role_lo */
403 0, /* NGX_HTTP_FASTCGI_KEEP_CONN */ /* flags */
404 { 0, 0, 0, 0, 0 } }, /* reserved[5] */
407 NGX_HTTP_FASTCGI_PARAMS, /* type */
408 0, /* request_id_hi */
409 1 }, /* request_id_lo */
414 static ngx_http_variable_t ngx_http_fastcgi_vars[] = {
416 { ngx_string("fastcgi_script_name"), NULL,
417 ngx_http_fastcgi_script_name_variable, 0,
418 NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
420 { ngx_string("fastcgi_path_info"), NULL,
421 ngx_http_fastcgi_path_info_variable, 0,
422 NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
424 { ngx_null_string, NULL, NULL, 0, 0, 0 }
428 static ngx_str_t ngx_http_fastcgi_hide_headers[] = {
429 ngx_string("Status"),
430 ngx_string("X-Accel-Expires"),
431 ngx_string("X-Accel-Redirect"),
432 ngx_string("X-Accel-Limit-Rate"),
433 ngx_string("X-Accel-Buffering"),
434 ngx_string("X-Accel-Charset"),
440 ngx_http_fastcgi_handler(ngx_http_request_t *r)
443 ngx_http_upstream_t *u;
444 ngx_http_fastcgi_ctx_t *f;
445 ngx_http_fastcgi_loc_conf_t *flcf;
447 if (r->subrequest_in_memory) {
448 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
449 "ngx_http_fastcgi_module does not support "
450 "subrequest in memory");
451 return NGX_HTTP_INTERNAL_SERVER_ERROR;
454 f = ngx_pcalloc(r->pool, sizeof(ngx_http_fastcgi_ctx_t));
459 ngx_http_set_ctx(r, f, ngx_http_fastcgi_module);
461 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
463 return NGX_HTTP_INTERNAL_SERVER_ERROR;
468 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
470 if (flcf->fastcgi_lengths) {
471 if (ngx_http_fastcgi_eval(r, flcf) != NGX_OK) {
472 return NGX_HTTP_INTERNAL_SERVER_ERROR;
476 u->schema.len = sizeof("fastcgi://") - 1;
477 u->schema.data = (u_char *) "fastcgi://";
479 u->peer.log = r->connection->log;
480 u->peer.log_error = NGX_ERROR_ERR;
482 u->peer.lock = &r->connection->lock;
485 u->output.tag = (ngx_buf_tag_t) &ngx_http_fastcgi_module;
487 u->conf = &flcf->upstream;
489 u->create_request = ngx_http_fastcgi_create_request;
490 u->reinit_request = ngx_http_fastcgi_reinit_request;
491 u->process_header = ngx_http_fastcgi_process_header;
492 u->abort_request = ngx_http_fastcgi_abort_request;
493 u->finalize_request = ngx_http_fastcgi_finalize_request;
497 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
498 if (u->pipe == NULL) {
499 return NGX_HTTP_INTERNAL_SERVER_ERROR;
502 u->pipe->input_filter = ngx_http_fastcgi_input_filter;
503 u->pipe->input_ctx = r;
505 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
507 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
516 ngx_http_fastcgi_eval(ngx_http_request_t *r, ngx_http_fastcgi_loc_conf_t *flcf)
520 ngx_memzero(&u, sizeof(ngx_url_t));
522 if (ngx_http_script_run(r, &u.url, flcf->fastcgi_lengths->elts, 0,
523 flcf->fastcgi_values->elts)
531 if (ngx_parse_url(r->pool, &u) != NGX_OK) {
533 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
534 "%s in upstream \"%V\"", u.err, &u.url);
541 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
542 "no port in upstream \"%V\"", &u.url);
546 r->upstream->resolved = ngx_pcalloc(r->pool,
547 sizeof(ngx_http_upstream_resolved_t));
548 if (r->upstream->resolved == NULL) {
552 if (u.addrs && u.addrs[0].sockaddr) {
553 r->upstream->resolved->sockaddr = u.addrs[0].sockaddr;
554 r->upstream->resolved->socklen = u.addrs[0].socklen;
555 r->upstream->resolved->naddrs = 1;
556 r->upstream->resolved->host = u.addrs[0].name;
559 r->upstream->resolved->host = u.host;
560 r->upstream->resolved->port = u.port;
568 ngx_http_fastcgi_create_request(ngx_http_request_t *r)
572 size_t size, len, key_len, val_len, padding;
573 ngx_uint_t i, n, next;
575 ngx_chain_t *cl, *body;
576 ngx_list_part_t *part;
577 ngx_table_elt_t *header;
578 ngx_http_script_code_pt code;
579 ngx_http_script_engine_t e, le;
580 ngx_http_fastcgi_header_t *h;
581 ngx_http_fastcgi_loc_conf_t *flcf;
582 ngx_http_script_len_code_pt lcode;
586 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
588 if (flcf->params_len) {
589 ngx_memzero(&le, sizeof(ngx_http_script_engine_t));
591 ngx_http_script_flush_no_cacheable_variables(r, flcf->flushes);
594 le.ip = flcf->params_len->elts;
597 while (*(uintptr_t *) le.ip) {
599 lcode = *(ngx_http_script_len_code_pt *) le.ip;
600 key_len = lcode(&le);
602 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
603 lcode = *(ngx_http_script_len_code_pt *) le.ip;
605 le.ip += sizeof(uintptr_t);
607 len += 1 + key_len + ((val_len > 127) ? 4 : 1) + val_len;
611 if (flcf->upstream.pass_request_headers) {
613 part = &r->headers_in.headers.part;
616 for (i = 0; /* void */; i++) {
618 if (i >= part->nelts) {
619 if (part->next == NULL) {
628 len += ((sizeof("HTTP_") - 1 + header[i].key.len > 127) ? 4 : 1)
629 + ((header[i].value.len > 127) ? 4 : 1)
630 + sizeof("HTTP_") - 1 + header[i].key.len + header[i].value.len;
636 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
637 "fastcgi request record is too big: %uz", len);
642 padding = 8 - len % 8;
643 padding = (padding == 8) ? 0 : padding;
646 size = sizeof(ngx_http_fastcgi_header_t)
647 + sizeof(ngx_http_fastcgi_begin_request_t)
649 + sizeof(ngx_http_fastcgi_header_t) /* NGX_HTTP_FASTCGI_PARAMS */
651 + sizeof(ngx_http_fastcgi_header_t) /* NGX_HTTP_FASTCGI_PARAMS */
653 + sizeof(ngx_http_fastcgi_header_t); /* NGX_HTTP_FASTCGI_STDIN */
656 b = ngx_create_temp_buf(r->pool, size);
661 cl = ngx_alloc_chain_link(r->pool);
668 ngx_memcpy(b->pos, &ngx_http_fastcgi_request_start,
669 sizeof(ngx_http_fastcgi_request_start_t));
671 h = (ngx_http_fastcgi_header_t *)
672 (b->pos + sizeof(ngx_http_fastcgi_header_t)
673 + sizeof(ngx_http_fastcgi_begin_request_t));
675 h->content_length_hi = (u_char) ((len >> 8) & 0xff);
676 h->content_length_lo = (u_char) (len & 0xff);
677 h->padding_length = (u_char) padding;
680 b->last = b->pos + sizeof(ngx_http_fastcgi_header_t)
681 + sizeof(ngx_http_fastcgi_begin_request_t)
682 + sizeof(ngx_http_fastcgi_header_t);
685 if (flcf->params_len) {
686 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
688 e.ip = flcf->params->elts;
693 le.ip = flcf->params_len->elts;
695 while (*(uintptr_t *) le.ip) {
697 lcode = *(ngx_http_script_len_code_pt *) le.ip;
698 key_len = (u_char) lcode(&le);
700 for (val_len = 0; *(uintptr_t *) le.ip; val_len += lcode(&le)) {
701 lcode = *(ngx_http_script_len_code_pt *) le.ip;
703 le.ip += sizeof(uintptr_t);
705 *e.pos++ = (u_char) key_len;
708 *e.pos++ = (u_char) (((val_len >> 24) & 0x7f) | 0x80);
709 *e.pos++ = (u_char) ((val_len >> 16) & 0xff);
710 *e.pos++ = (u_char) ((val_len >> 8) & 0xff);
711 *e.pos++ = (u_char) (val_len & 0xff);
714 *e.pos++ = (u_char) val_len;
717 while (*(uintptr_t *) e.ip) {
718 code = *(ngx_http_script_code_pt *) e.ip;
719 code((ngx_http_script_engine_t *) &e);
721 e.ip += sizeof(uintptr_t);
723 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
724 "fastcgi param: \"%*s: %*s\"",
725 key_len, e.pos - (key_len + val_len),
726 val_len, e.pos - val_len);
733 if (flcf->upstream.pass_request_headers) {
735 part = &r->headers_in.headers.part;
738 for (i = 0; /* void */; i++) {
740 if (i >= part->nelts) {
741 if (part->next == NULL) {
750 len = sizeof("HTTP_") - 1 + header[i].key.len;
752 *b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80);
753 *b->last++ = (u_char) ((len >> 16) & 0xff);
754 *b->last++ = (u_char) ((len >> 8) & 0xff);
755 *b->last++ = (u_char) (len & 0xff);
758 *b->last++ = (u_char) len;
761 len = header[i].value.len;
763 *b->last++ = (u_char) (((len >> 24) & 0x7f) | 0x80);
764 *b->last++ = (u_char) ((len >> 16) & 0xff);
765 *b->last++ = (u_char) ((len >> 8) & 0xff);
766 *b->last++ = (u_char) (len & 0xff);
769 *b->last++ = (u_char) len;
772 b->last = ngx_cpymem(b->last, "HTTP_", sizeof("HTTP_") - 1);
774 for (n = 0; n < header[i].key.len; n++) {
775 ch = header[i].key.data[n];
777 if (ch >= 'a' && ch <= 'z') {
780 } else if (ch == '-') {
787 b->last = ngx_copy(b->last, header[i].value.data,
788 header[i].value.len);
794 ngx_memzero(b->last, padding);
799 h = (ngx_http_fastcgi_header_t *) b->last;
800 b->last += sizeof(ngx_http_fastcgi_header_t);
803 h->type = NGX_HTTP_FASTCGI_PARAMS;
804 h->request_id_hi = 0;
805 h->request_id_lo = 1;
806 h->content_length_hi = 0;
807 h->content_length_lo = 0;
808 h->padding_length = 0;
811 h = (ngx_http_fastcgi_header_t *) b->last;
812 b->last += sizeof(ngx_http_fastcgi_header_t);
814 if (flcf->upstream.pass_request_body) {
815 body = r->upstream->request_bufs;
816 r->upstream->request_bufs = cl;
818 #if (NGX_SUPPRESS_WARN)
825 if (body->buf->in_file) {
826 file_pos = body->buf->file_pos;
829 pos = body->buf->pos;
835 b = ngx_alloc_buf(r->pool);
840 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
842 if (body->buf->in_file) {
843 b->file_pos = file_pos;
844 file_pos += 32 * 1024;
846 if (file_pos >= body->buf->file_last) {
847 file_pos = body->buf->file_last;
851 b->file_last = file_pos;
852 len = (ngx_uint_t) (file_pos - b->file_pos);
858 if (pos >= body->buf->last) {
859 pos = body->buf->last;
864 len = (ngx_uint_t) (pos - b->pos);
867 padding = 8 - len % 8;
868 padding = (padding == 8) ? 0 : padding;
871 h->type = NGX_HTTP_FASTCGI_STDIN;
872 h->request_id_hi = 0;
873 h->request_id_lo = 1;
874 h->content_length_hi = (u_char) ((len >> 8) & 0xff);
875 h->content_length_lo = (u_char) (len & 0xff);
876 h->padding_length = (u_char) padding;
879 cl->next = ngx_alloc_chain_link(r->pool);
880 if (cl->next == NULL) {
887 b = ngx_create_temp_buf(r->pool,
888 sizeof(ngx_http_fastcgi_header_t)
895 ngx_memzero(b->last, padding);
899 h = (ngx_http_fastcgi_header_t *) b->last;
900 b->last += sizeof(ngx_http_fastcgi_header_t);
902 cl->next = ngx_alloc_chain_link(r->pool);
903 if (cl->next == NULL) {
916 r->upstream->request_bufs = cl;
920 h->type = NGX_HTTP_FASTCGI_STDIN;
921 h->request_id_hi = 0;
922 h->request_id_lo = 1;
923 h->content_length_hi = 0;
924 h->content_length_lo = 0;
925 h->padding_length = 0;
935 ngx_http_fastcgi_reinit_request(ngx_http_request_t *r)
937 ngx_http_fastcgi_ctx_t *f;
939 f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
945 f->state = ngx_http_fastcgi_st_version;
946 f->fastcgi_stdout = 0;
953 ngx_http_fastcgi_process_header(ngx_http_request_t *r)
955 u_char *p, *start, *last, *part_start;
957 ngx_str_t *status_line, line, *pattern;
958 ngx_int_t rc, status;
962 ngx_http_upstream_t *u;
963 ngx_http_fastcgi_ctx_t *f;
964 ngx_http_upstream_header_t *hh;
965 ngx_http_fastcgi_loc_conf_t *flcf;
966 ngx_http_fastcgi_split_part_t *part;
967 ngx_http_upstream_main_conf_t *umcf;
969 f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
971 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
977 if (f->state < ngx_http_fastcgi_st_data) {
979 f->pos = u->buffer.pos;
980 f->last = u->buffer.last;
982 rc = ngx_http_fastcgi_process_record(r, f);
984 u->buffer.pos = f->pos;
985 u->buffer.last = f->last;
987 if (rc == NGX_AGAIN) {
991 if (rc == NGX_ERROR) {
992 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
995 if (f->type != NGX_HTTP_FASTCGI_STDOUT
996 && f->type != NGX_HTTP_FASTCGI_STDERR)
998 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
999 "upstream sent unexpected FastCGI record: %d",
1002 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1005 if (f->type == NGX_HTTP_FASTCGI_STDOUT && f->length == 0) {
1006 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1007 "upstream closed prematurely FastCGI stdout");
1009 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1013 if (f->state == ngx_http_fastcgi_st_padding) {
1015 if (u->buffer.pos + f->padding < u->buffer.last) {
1016 f->state = ngx_http_fastcgi_st_version;
1017 u->buffer.pos += f->padding;
1022 if (u->buffer.pos + f->padding == u->buffer.last) {
1023 f->state = ngx_http_fastcgi_st_version;
1024 u->buffer.pos = u->buffer.last;
1029 f->padding -= u->buffer.last - u->buffer.pos;
1030 u->buffer.pos = u->buffer.last;
1036 /* f->state == ngx_http_fastcgi_st_data */
1038 if (f->type == NGX_HTTP_FASTCGI_STDERR) {
1041 line.data = u->buffer.pos;
1043 if (u->buffer.pos + f->length <= u->buffer.last) {
1044 line.len = f->length;
1045 u->buffer.pos += f->length;
1047 f->state = ngx_http_fastcgi_st_padding;
1050 line.len = u->buffer.last - u->buffer.pos;
1051 f->length -= u->buffer.last - u->buffer.pos;
1052 u->buffer.pos = u->buffer.last;
1055 while (line.data[line.len - 1] == LF
1056 || line.data[line.len - 1] == CR
1057 || line.data[line.len - 1] == '.'
1058 || line.data[line.len - 1] == ' ')
1063 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1064 "FastCGI sent in stderr: \"%V\"", &line);
1066 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
1068 if (flcf->catch_stderr) {
1069 pattern = flcf->catch_stderr->elts;
1071 line.data[line.len - 1] = '\0';
1073 for (i = 0; i < flcf->catch_stderr->nelts; i++) {
1074 if (ngx_strstr(line.data, pattern[i].data)) {
1075 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1080 if (u->buffer.pos == u->buffer.last) {
1082 if (!f->fastcgi_stdout) {
1085 * the special handling the large number
1086 * of the PHP warnings to not allocate memory
1089 u->buffer.pos = u->buffer.start;
1090 u->buffer.last = u->buffer.start;
1097 f->state = ngx_http_fastcgi_st_version;
1104 /* f->type == NGX_HTTP_FASTCGI_STDOUT */
1106 f->fastcgi_stdout = 1;
1108 start = u->buffer.pos;
1110 if (u->buffer.pos + f->length < u->buffer.last) {
1113 * set u->buffer.last to the end of the FastCGI record data
1114 * for ngx_http_parse_header_line()
1117 last = u->buffer.last;
1118 u->buffer.last = u->buffer.pos + f->length;
1126 part_start = u->buffer.pos;
1128 rc = ngx_http_parse_header_line(r, &u->buffer, 1);
1130 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1131 "http fastcgi parser: %d", rc);
1133 if (rc == NGX_AGAIN) {
1139 /* a header line has been parsed successfully */
1141 h = ngx_list_push(&u->headers_in.headers);
1146 if (f->split_parts && f->split_parts->nelts) {
1148 part = f->split_parts->elts;
1149 size = u->buffer.pos - part_start;
1151 for (i = 0; i < f->split_parts->nelts; i++) {
1152 size += part[i].end - part[i].start;
1155 p = ngx_pnalloc(r->pool, size);
1162 for (i = 0; i < f->split_parts->nelts; i++) {
1163 p = ngx_cpymem(p, part[i].start,
1164 part[i].end - part[i].start);
1167 p = ngx_cpymem(p, part_start, u->buffer.pos - part_start);
1171 f->split_parts->nelts = 0;
1173 rc = ngx_http_parse_header_line(r, &buf, 1);
1175 h->key.len = r->header_name_end - r->header_name_start;
1176 h->key.data = r->header_name_start;
1177 h->key.data[h->key.len] = '\0';
1179 h->value.len = r->header_end - r->header_start;
1180 h->value.data = r->header_start;
1181 h->value.data[h->value.len] = '\0';
1183 h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
1184 if (h->lowcase_key == NULL) {
1190 h->key.len = r->header_name_end - r->header_name_start;
1191 h->value.len = r->header_end - r->header_start;
1193 h->key.data = ngx_pnalloc(r->pool,
1194 h->key.len + 1 + h->value.len + 1
1196 if (h->key.data == NULL) {
1200 h->value.data = h->key.data + h->key.len + 1;
1201 h->lowcase_key = h->key.data + h->key.len + 1
1204 ngx_cpystrn(h->key.data, r->header_name_start,
1206 ngx_cpystrn(h->value.data, r->header_start,
1210 h->hash = r->header_hash;
1212 if (h->key.len == r->lowcase_index) {
1213 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
1216 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
1219 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
1220 h->lowcase_key, h->key.len);
1222 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
1226 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1227 "http fastcgi header: \"%V: %V\"",
1228 &h->key, &h->value);
1230 if (u->buffer.pos < u->buffer.last) {
1234 /* the end of the FastCGI record */
1239 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1241 /* a whole header has been parsed successfully */
1243 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1244 "http fastcgi header done");
1246 if (u->headers_in.status) {
1247 status_line = &u->headers_in.status->value;
1249 status = ngx_atoi(status_line->data, 3);
1251 if (status == NGX_ERROR) {
1252 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1253 "upstream sent invalid status \"%V\"",
1255 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1258 u->headers_in.status_n = status;
1259 u->headers_in.status_line = *status_line;
1261 } else if (u->headers_in.location) {
1262 u->headers_in.status_n = 302;
1263 u->headers_in.status_line.len =
1264 sizeof("302 Moved Temporarily") - 1;
1265 u->headers_in.status_line.data =
1266 (u_char *) "302 Moved Temporarily";
1269 u->headers_in.status_n = 200;
1270 u->headers_in.status_line.len = sizeof("200 OK") - 1;
1271 u->headers_in.status_line.data = (u_char *) "200 OK";
1274 u->state->status = u->headers_in.status_n;
1277 u->cacheable = ngx_http_upstream_is_cacheable(r);
1284 /* there was error while a header line parsing */
1286 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1287 "upstream sent invalid header");
1289 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1293 u->buffer.last = last;
1296 f->length -= u->buffer.pos - start;
1298 if (f->length == 0) {
1300 f->state = ngx_http_fastcgi_st_padding;
1302 f->state = ngx_http_fastcgi_st_version;
1306 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1314 /* rc == NGX_AGAIN */
1316 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1317 "upstream split a header line in FastCGI records");
1319 if (f->split_parts == NULL) {
1320 f->split_parts = ngx_array_create(r->pool, 1,
1321 sizeof(ngx_http_fastcgi_split_part_t));
1322 if (f->split_parts == NULL) {
1327 part = ngx_array_push(f->split_parts);
1329 part->start = part_start;
1330 part->end = u->buffer.last;
1338 ngx_http_fastcgi_input_filter(ngx_event_pipe_t *p, ngx_buf_t *buf)
1341 ngx_buf_t *b, **prev;
1344 ngx_http_request_t *r;
1345 ngx_http_fastcgi_ctx_t *f;
1347 if (buf->pos == buf->last) {
1352 f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
1355 prev = &buf->shadow;
1358 f->last = buf->last;
1361 if (f->state < ngx_http_fastcgi_st_data) {
1363 rc = ngx_http_fastcgi_process_record(r, f);
1365 if (rc == NGX_AGAIN) {
1369 if (rc == NGX_ERROR) {
1373 if (f->type == NGX_HTTP_FASTCGI_STDOUT && f->length == 0) {
1374 f->state = ngx_http_fastcgi_st_version;
1375 p->upstream_done = 1;
1377 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0,
1378 "http fastcgi closed stdout");
1383 if (f->type == NGX_HTTP_FASTCGI_END_REQUEST) {
1384 f->state = ngx_http_fastcgi_st_version;
1385 p->upstream_done = 1;
1387 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, p->log, 0,
1388 "http fastcgi sent end request");
1395 if (f->state == ngx_http_fastcgi_st_padding) {
1397 if (f->pos + f->padding < f->last) {
1398 f->state = ngx_http_fastcgi_st_version;
1399 f->pos += f->padding;
1404 if (f->pos + f->padding == f->last) {
1405 f->state = ngx_http_fastcgi_st_version;
1410 f->padding -= f->last - f->pos;
1416 /* f->state == ngx_http_fastcgi_st_data */
1418 if (f->type == NGX_HTTP_FASTCGI_STDERR) {
1422 if (f->pos == f->last) {
1428 if (f->pos + f->length <= f->last) {
1429 line.len = f->length;
1430 f->pos += f->length;
1432 f->state = ngx_http_fastcgi_st_padding;
1435 line.len = f->last - f->pos;
1436 f->length -= f->last - f->pos;
1440 while (line.data[line.len - 1] == LF
1441 || line.data[line.len - 1] == CR
1442 || line.data[line.len - 1] == '.'
1443 || line.data[line.len - 1] == ' ')
1448 ngx_log_error(NGX_LOG_ERR, p->log, 0,
1449 "FastCGI sent in stderr: \"%V\"", &line);
1451 if (f->pos == f->last) {
1456 f->state = ngx_http_fastcgi_st_version;
1463 /* f->type == NGX_HTTP_FASTCGI_STDOUT */
1465 if (f->pos == f->last) {
1471 p->free = p->free->next;
1474 b = ngx_alloc_buf(p->pool);
1480 ngx_memzero(b, sizeof(ngx_buf_t));
1483 b->start = buf->start;
1492 cl = ngx_alloc_chain_link(p->pool);
1505 p->last_in = &cl->next;
1508 /* STUB */ b->num = buf->num;
1510 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
1511 "input buf #%d %p", b->num, b->pos);
1513 if (f->pos + f->length < f->last) {
1516 f->state = ngx_http_fastcgi_st_padding;
1518 f->state = ngx_http_fastcgi_st_version;
1521 f->pos += f->length;
1527 if (f->pos + f->length == f->last) {
1530 f->state = ngx_http_fastcgi_st_padding;
1532 f->state = ngx_http_fastcgi_st_version;
1540 f->length -= f->last - f->pos;
1552 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, p->log, 0,
1553 "input buf %p %z", b->pos, b->last - b->pos);
1558 /* there is no data record in the buf, add it to free chain */
1560 if (ngx_event_pipe_add_free_buf(p, buf) != NGX_OK) {
1569 ngx_http_fastcgi_process_record(ngx_http_request_t *r,
1570 ngx_http_fastcgi_ctx_t *f)
1573 ngx_http_fastcgi_state_e state;
1577 for (p = f->pos; p < f->last; p++) {
1581 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1582 "http fastcgi record byte: %02Xd", ch);
1586 case ngx_http_fastcgi_st_version:
1588 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1589 "upstream sent unsupported FastCGI "
1590 "protocol version: %d", ch);
1593 state = ngx_http_fastcgi_st_type;
1596 case ngx_http_fastcgi_st_type:
1598 case NGX_HTTP_FASTCGI_STDOUT:
1599 case NGX_HTTP_FASTCGI_STDERR:
1600 case NGX_HTTP_FASTCGI_END_REQUEST:
1601 f->type = (ngx_uint_t) ch;
1604 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1605 "upstream sent invalid FastCGI "
1606 "record type: %d", ch);
1610 state = ngx_http_fastcgi_st_request_id_hi;
1613 /* we support the single request per connection */
1615 case ngx_http_fastcgi_st_request_id_hi:
1617 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1618 "upstream sent unexpected FastCGI "
1619 "request id high byte: %d", ch);
1622 state = ngx_http_fastcgi_st_request_id_lo;
1625 case ngx_http_fastcgi_st_request_id_lo:
1627 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
1628 "upstream sent unexpected FastCGI "
1629 "request id low byte: %d", ch);
1632 state = ngx_http_fastcgi_st_content_length_hi;
1635 case ngx_http_fastcgi_st_content_length_hi:
1636 f->length = ch << 8;
1637 state = ngx_http_fastcgi_st_content_length_lo;
1640 case ngx_http_fastcgi_st_content_length_lo:
1641 f->length |= (size_t) ch;
1642 state = ngx_http_fastcgi_st_padding_length;
1645 case ngx_http_fastcgi_st_padding_length:
1646 f->padding = (size_t) ch;
1647 state = ngx_http_fastcgi_st_reserved;
1650 case ngx_http_fastcgi_st_reserved:
1651 state = ngx_http_fastcgi_st_data;
1653 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1654 "http fastcgi record length: %z", f->length);
1661 /* suppress warning */
1662 case ngx_http_fastcgi_st_data:
1663 case ngx_http_fastcgi_st_padding:
1675 ngx_http_fastcgi_abort_request(ngx_http_request_t *r)
1677 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1678 "abort http fastcgi request");
1685 ngx_http_fastcgi_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
1687 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1688 "finalize http fastcgi request");
1695 ngx_http_fastcgi_add_variables(ngx_conf_t *cf)
1697 ngx_http_variable_t *var, *v;
1699 for (v = ngx_http_fastcgi_vars; v->name.len; v++) {
1700 var = ngx_http_add_variable(cf, &v->name, v->flags);
1705 var->get_handler = v->get_handler;
1706 var->data = v->data;
1714 ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf)
1716 ngx_http_fastcgi_loc_conf_t *conf;
1718 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_fastcgi_loc_conf_t));
1720 return NGX_CONF_ERROR;
1724 * set by ngx_pcalloc():
1726 * conf->upstream.bufs.num = 0;
1727 * conf->upstream.next_upstream = 0;
1728 * conf->upstream.temp_path = NULL;
1729 * conf->upstream.hide_headers_hash = { NULL, 0 };
1730 * conf->upstream.uri = { 0, NULL };
1731 * conf->upstream.location = NULL;
1732 * conf->upstream.store_lengths = NULL;
1733 * conf->upstream.store_values = NULL;
1735 * conf->index.len = 0;
1736 * conf->index.data = NULL;
1739 conf->upstream.store = NGX_CONF_UNSET;
1740 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
1741 conf->upstream.buffering = NGX_CONF_UNSET;
1742 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
1744 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
1745 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
1746 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
1748 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
1749 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
1751 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
1752 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
1753 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
1755 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
1756 conf->upstream.pass_request_body = NGX_CONF_UNSET;
1758 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
1759 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
1761 conf->upstream.intercept_errors = NGX_CONF_UNSET;
1763 /* "fastcgi_cyclic_temp_file" is disabled */
1764 conf->upstream.cyclic_temp_file = 0;
1766 conf->catch_stderr = NGX_CONF_UNSET_PTR;
1773 ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
1775 ngx_http_fastcgi_loc_conf_t *prev = parent;
1776 ngx_http_fastcgi_loc_conf_t *conf = child;
1783 ngx_hash_init_t hash;
1784 ngx_http_script_compile_t sc;
1785 ngx_http_script_copy_code_t *copy;
1787 if (conf->upstream.store != 0) {
1788 ngx_conf_merge_value(conf->upstream.store,
1789 prev->upstream.store, 0);
1791 if (conf->upstream.store_lengths == NULL) {
1792 conf->upstream.store_lengths = prev->upstream.store_lengths;
1793 conf->upstream.store_values = prev->upstream.store_values;
1797 ngx_conf_merge_uint_value(conf->upstream.store_access,
1798 prev->upstream.store_access, 0600);
1800 ngx_conf_merge_value(conf->upstream.buffering,
1801 prev->upstream.buffering, 1);
1803 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
1804 prev->upstream.ignore_client_abort, 0);
1806 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
1807 prev->upstream.connect_timeout, 60000);
1809 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
1810 prev->upstream.send_timeout, 60000);
1812 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
1813 prev->upstream.read_timeout, 60000);
1815 ngx_conf_merge_size_value(conf->upstream.send_lowat,
1816 prev->upstream.send_lowat, 0);
1818 ngx_conf_merge_size_value(conf->upstream.buffer_size,
1819 prev->upstream.buffer_size,
1820 (size_t) ngx_pagesize);
1823 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
1826 if (conf->upstream.bufs.num < 2) {
1827 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1828 "there must be at least 2 \"fastcgi_buffers\"");
1829 return NGX_CONF_ERROR;
1833 size = conf->upstream.buffer_size;
1834 if (size < conf->upstream.bufs.size) {
1835 size = conf->upstream.bufs.size;
1839 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
1840 prev->upstream.busy_buffers_size_conf,
1841 NGX_CONF_UNSET_SIZE);
1843 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
1844 conf->upstream.busy_buffers_size = 2 * size;
1846 conf->upstream.busy_buffers_size =
1847 conf->upstream.busy_buffers_size_conf;
1850 if (conf->upstream.busy_buffers_size < size) {
1851 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1852 "\"fastcgi_busy_buffers_size\" must be equal or bigger than "
1853 "maximum of the value of \"fastcgi_buffer_size\" and "
1854 "one of the \"fastcgi_buffers\"");
1856 return NGX_CONF_ERROR;
1859 if (conf->upstream.busy_buffers_size
1860 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
1862 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1863 "\"fastcgi_busy_buffers_size\" must be less than "
1864 "the size of all \"fastcgi_buffers\" minus one buffer");
1866 return NGX_CONF_ERROR;
1870 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
1871 prev->upstream.temp_file_write_size_conf,
1872 NGX_CONF_UNSET_SIZE);
1874 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
1875 conf->upstream.temp_file_write_size = 2 * size;
1877 conf->upstream.temp_file_write_size =
1878 conf->upstream.temp_file_write_size_conf;
1881 if (conf->upstream.temp_file_write_size < size) {
1882 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1883 "\"fastcgi_temp_file_write_size\" must be equal or bigger than "
1884 "maximum of the value of \"fastcgi_buffer_size\" and "
1885 "one of the \"fastcgi_buffers\"");
1887 return NGX_CONF_ERROR;
1891 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
1892 prev->upstream.max_temp_file_size_conf,
1893 NGX_CONF_UNSET_SIZE);
1895 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
1896 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
1898 conf->upstream.max_temp_file_size =
1899 conf->upstream.max_temp_file_size_conf;
1902 if (conf->upstream.max_temp_file_size != 0
1903 && conf->upstream.max_temp_file_size < size)
1905 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1906 "\"fastcgi_max_temp_file_size\" must be equal to zero to disable "
1907 "the temporary files usage or must be equal or bigger than "
1908 "maximum of the value of \"fastcgi_buffer_size\" and "
1909 "one of the \"fastcgi_buffers\"");
1911 return NGX_CONF_ERROR;
1915 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
1916 prev->upstream.next_upstream,
1917 (NGX_CONF_BITMASK_SET
1918 |NGX_HTTP_UPSTREAM_FT_ERROR
1919 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
1921 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
1922 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
1923 |NGX_HTTP_UPSTREAM_FT_OFF;
1926 ngx_conf_merge_path_value(conf->upstream.temp_path,
1927 prev->upstream.temp_path,
1928 NGX_HTTP_FASTCGI_TEMP_PATH, 1, 2, 0,
1929 ngx_garbage_collector_temp_handler, cf);
1931 ngx_conf_merge_value(conf->upstream.pass_request_headers,
1932 prev->upstream.pass_request_headers, 1);
1933 ngx_conf_merge_value(conf->upstream.pass_request_body,
1934 prev->upstream.pass_request_body, 1);
1936 ngx_conf_merge_value(conf->upstream.intercept_errors,
1937 prev->upstream.intercept_errors, 0);
1939 ngx_conf_merge_ptr_value(conf->catch_stderr, prev->catch_stderr, NULL);
1942 ngx_conf_merge_str_value(conf->index, prev->index, "");
1944 hash.max_size = 512;
1945 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
1946 hash.name = "fastcgi_hide_headers_hash";
1948 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
1950 ngx_http_fastcgi_hide_headers,
1954 return NGX_CONF_ERROR;
1957 if (conf->upstream.upstream == NULL) {
1958 conf->upstream.upstream = prev->upstream.upstream;
1961 if (conf->fastcgi_lengths == NULL) {
1962 conf->fastcgi_lengths = prev->fastcgi_lengths;
1963 conf->fastcgi_values = prev->fastcgi_values;
1967 if (conf->split_regex == NULL) {
1968 conf->split_regex = prev->split_regex;
1969 conf->split_name = prev->split_name;
1973 if (conf->params_source == NULL) {
1974 conf->flushes = prev->flushes;
1975 conf->params_len = prev->params_len;
1976 conf->params = prev->params;
1977 conf->params_source = prev->params_source;
1979 if (conf->params_source == NULL) {
1984 conf->params_len = ngx_array_create(cf->pool, 64, 1);
1985 if (conf->params_len == NULL) {
1986 return NGX_CONF_ERROR;
1989 conf->params = ngx_array_create(cf->pool, 512, 1);
1990 if (conf->params == NULL) {
1991 return NGX_CONF_ERROR;
1994 src = conf->params_source->elts;
1995 for (i = 0; i < conf->params_source->nelts; i++) {
1997 if (ngx_http_script_variables_count(&src[i].value) == 0) {
1998 copy = ngx_array_push_n(conf->params_len,
1999 sizeof(ngx_http_script_copy_code_t));
2001 return NGX_CONF_ERROR;
2004 copy->code = (ngx_http_script_code_pt)
2005 ngx_http_script_copy_len_code;
2006 copy->len = src[i].key.len;
2009 copy = ngx_array_push_n(conf->params_len,
2010 sizeof(ngx_http_script_copy_code_t));
2012 return NGX_CONF_ERROR;
2015 copy->code = (ngx_http_script_code_pt)
2016 ngx_http_script_copy_len_code;
2017 copy->len = src[i].value.len;
2020 size = (sizeof(ngx_http_script_copy_code_t)
2021 + src[i].key.len + src[i].value.len
2022 + sizeof(uintptr_t) - 1)
2023 & ~(sizeof(uintptr_t) - 1);
2025 copy = ngx_array_push_n(conf->params, size);
2027 return NGX_CONF_ERROR;
2030 copy->code = ngx_http_script_copy_code;
2031 copy->len = src[i].key.len + src[i].value.len;
2033 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
2035 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
2036 ngx_memcpy(p, src[i].value.data, src[i].value.len);
2039 copy = ngx_array_push_n(conf->params_len,
2040 sizeof(ngx_http_script_copy_code_t));
2042 return NGX_CONF_ERROR;
2045 copy->code = (ngx_http_script_code_pt)
2046 ngx_http_script_copy_len_code;
2047 copy->len = src[i].key.len;
2050 size = (sizeof(ngx_http_script_copy_code_t)
2051 + src[i].key.len + sizeof(uintptr_t) - 1)
2052 & ~(sizeof(uintptr_t) - 1);
2054 copy = ngx_array_push_n(conf->params, size);
2056 return NGX_CONF_ERROR;
2059 copy->code = ngx_http_script_copy_code;
2060 copy->len = src[i].key.len;
2062 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
2063 ngx_memcpy(p, src[i].key.data, src[i].key.len);
2066 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2069 sc.source = &src[i].value;
2070 sc.flushes = &conf->flushes;
2071 sc.lengths = &conf->params_len;
2072 sc.values = &conf->params;
2074 if (ngx_http_script_compile(&sc) != NGX_OK) {
2075 return NGX_CONF_ERROR;
2079 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t));
2081 return NGX_CONF_ERROR;
2084 *code = (uintptr_t) NULL;
2087 code = ngx_array_push_n(conf->params, sizeof(uintptr_t));
2089 return NGX_CONF_ERROR;
2092 *code = (uintptr_t) NULL;
2095 code = ngx_array_push_n(conf->params_len, sizeof(uintptr_t));
2097 return NGX_CONF_ERROR;
2100 *code = (uintptr_t) NULL;
2107 ngx_http_fastcgi_script_name_variable(ngx_http_request_t *r,
2108 ngx_http_variable_value_t *v, uintptr_t data)
2111 ngx_http_fastcgi_ctx_t *f;
2112 ngx_http_fastcgi_loc_conf_t *flcf;
2114 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
2116 f = ngx_http_fastcgi_split(r, flcf);
2122 if (f->script_name.len == 0
2123 || f->script_name.data[f->script_name.len - 1] != '/')
2125 v->len = f->script_name.len;
2127 v->no_cacheable = 0;
2129 v->data = f->script_name.data;
2134 v->len = f->script_name.len + flcf->index.len;
2136 v->data = ngx_pnalloc(r->pool, v->len);
2137 if (v->data == NULL) {
2141 p = ngx_copy(v->data, f->script_name.data, f->script_name.len);
2142 ngx_memcpy(p, flcf->index.data, flcf->index.len);
2149 ngx_http_fastcgi_path_info_variable(ngx_http_request_t *r,
2150 ngx_http_variable_value_t *v, uintptr_t data)
2152 ngx_http_fastcgi_ctx_t *f;
2153 ngx_http_fastcgi_loc_conf_t *flcf;
2155 flcf = ngx_http_get_module_loc_conf(r, ngx_http_fastcgi_module);
2157 f = ngx_http_fastcgi_split(r, flcf);
2163 v->len = f->path_info.len;
2165 v->no_cacheable = 0;
2167 v->data = f->path_info.data;
2173 static ngx_http_fastcgi_ctx_t *
2174 ngx_http_fastcgi_split(ngx_http_request_t *r, ngx_http_fastcgi_loc_conf_t *flcf)
2176 ngx_http_fastcgi_ctx_t *f;
2179 int captures[(1 + 2) * 3];
2181 f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
2184 f = ngx_pcalloc(r->pool, sizeof(ngx_http_fastcgi_ctx_t));
2189 ngx_http_set_ctx(r, f, ngx_http_fastcgi_module);
2192 if (f->script_name.len) {
2196 if (flcf->split_regex == NULL) {
2197 f->script_name = r->uri;
2201 n = ngx_regex_exec(flcf->split_regex, &r->uri, captures, (1 + 2) * 3);
2203 if (n == NGX_REGEX_NO_MATCHED) {
2204 f->script_name = r->uri;
2209 ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
2210 ngx_regex_exec_n " failed: %d on \"%V\" using \"%V\"",
2211 n, &r->uri, &flcf->split_name);
2217 f->script_name.len = captures[3] - captures[2];
2218 f->script_name.data = r->uri.data;
2220 f->path_info.len = captures[5] - captures[4];
2221 f->path_info.data = r->uri.data + f->script_name.len;
2227 f = ngx_http_get_module_ctx(r, ngx_http_fastcgi_module);
2230 f = ngx_pcalloc(r->pool, sizeof(ngx_http_fastcgi_ctx_t));
2235 ngx_http_set_ctx(r, f, ngx_http_fastcgi_module);
2238 f->script_name = r->uri;
2247 ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2249 ngx_http_fastcgi_loc_conf_t *flcf = conf;
2252 ngx_str_t *value, *url;
2254 ngx_http_core_loc_conf_t *clcf;
2255 ngx_http_script_compile_t sc;
2257 if (flcf->upstream.upstream || flcf->fastcgi_lengths) {
2258 return "is duplicate";
2261 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
2262 clcf->handler = ngx_http_fastcgi_handler;
2264 value = cf->args->elts;
2268 n = ngx_http_script_variables_count(url);
2272 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2276 sc.lengths = &flcf->fastcgi_lengths;
2277 sc.values = &flcf->fastcgi_values;
2279 sc.complete_lengths = 1;
2280 sc.complete_values = 1;
2282 if (ngx_http_script_compile(&sc) != NGX_OK) {
2283 return NGX_CONF_ERROR;
2289 ngx_memzero(&u, sizeof(ngx_url_t));
2294 flcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
2295 if (flcf->upstream.upstream == NULL) {
2296 return NGX_CONF_ERROR;
2299 if (clcf->name.data[clcf->name.len - 1] == '/') {
2300 clcf->auto_redirect = 1;
2308 ngx_http_fastcgi_split_path_info(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2311 ngx_http_fastcgi_loc_conf_t *flcf = conf;
2314 ngx_str_t *value, err;
2315 u_char errstr[NGX_MAX_CONF_ERRSTR];
2317 value = cf->args->elts;
2319 flcf->split_name = value[1];
2321 err.len = NGX_MAX_CONF_ERRSTR;
2324 flcf->split_regex = ngx_regex_compile(&value[1], 0, cf->pool, &err);
2326 if (flcf->split_regex == NULL) {
2327 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "%s", err.data);
2328 return NGX_CONF_ERROR;
2331 n = ngx_regex_capture_count(flcf->split_regex);
2334 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2335 ngx_regex_capture_count_n " failed for "
2336 "pattern \"%V\"", &value[1]);
2337 return NGX_CONF_ERROR;
2341 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2342 "pattern \"%V\" must have 2 captures", &value[1]);
2343 return NGX_CONF_ERROR;
2350 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2351 "\"%V\" requires PCRE library", &cmd->name);
2352 return NGX_CONF_ERROR;
2359 ngx_http_fastcgi_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2361 ngx_http_fastcgi_loc_conf_t *flcf = conf;
2364 ngx_http_script_compile_t sc;
2366 if (flcf->upstream.store != NGX_CONF_UNSET || flcf->upstream.store_lengths)
2368 return "is duplicate";
2371 value = cf->args->elts;
2373 if (ngx_strcmp(value[1].data, "on") == 0) {
2374 flcf->upstream.store = 1;
2378 if (ngx_strcmp(value[1].data, "off") == 0) {
2379 flcf->upstream.store = 0;
2383 /* include the terminating '\0' into script */
2386 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2389 sc.source = &value[1];
2390 sc.lengths = &flcf->upstream.store_lengths;
2391 sc.values = &flcf->upstream.store_values;
2392 sc.variables = ngx_http_script_variables_count(&value[1]);
2393 sc.complete_lengths = 1;
2394 sc.complete_values = 1;
2396 if (ngx_http_script_compile(&sc) != NGX_OK) {
2397 return NGX_CONF_ERROR;
2405 ngx_http_fastcgi_lowat_check(ngx_conf_t *cf, void *post, void *data)
2410 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
2411 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2412 "\"fastcgi_send_lowat\" must be less than %d "
2413 "(sysctl net.inet.tcp.sendspace)",
2414 ngx_freebsd_net_inet_tcp_sendspace);
2416 return NGX_CONF_ERROR;
2419 #elif !(NGX_HAVE_SO_SNDLOWAT)
2422 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
2423 "\"fastcgi_send_lowat\" is not supported, ignored");
2434 ngx_http_fastcgi_upstream_max_fails_unsupported(ngx_conf_t *cf,
2435 ngx_command_t *cmd, void *conf)
2437 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2438 "\"fastcgi_upstream_max_fails\" is not supported, "
2439 "use the \"max_fails\" parameter of the \"server\" directive ",
2440 "inside the \"upstream\" block");
2442 return NGX_CONF_ERROR;
2447 ngx_http_fastcgi_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
2448 ngx_command_t *cmd, void *conf)
2450 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2451 "\"fastcgi_upstream_fail_timeout\" is not supported, "
2452 "use the \"fail_timeout\" parameter of the \"server\" directive ",
2453 "inside the \"upstream\" block");
2455 return NGX_CONF_ERROR;