blob: cbd2ce8e01d0363b7139ee4d82670baa192d5b9f [file] [log] [blame]
Igor Sysoev02f742b2005-04-08 15:18:55 +00001
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002/*
Igor Sysoev02f742b2005-04-08 15:18:55 +00003 * Copyright (C) Igor Sysoev
4 */
5
6
7#include <ngx_config.h>
8#include <ngx_core.h>
9#include <ngx_event.h>
10#include <ngx_http.h>
11
12
Igor Sysoev899b44e2005-05-12 14:58:06 +000013typedef struct ngx_http_proxy_redirect_s ngx_http_proxy_redirect_t;
14
15typedef ngx_int_t (*ngx_http_proxy_redirect_pt)(ngx_http_request_t *r,
16 ngx_table_elt_t *h, size_t prefix, ngx_http_proxy_redirect_t *pr);
17
18struct ngx_http_proxy_redirect_s {
Igor Sysoev6f134cc2006-05-23 14:54:58 +000019 ngx_http_proxy_redirect_pt handler;
20 ngx_str_t redirect;
Igor Sysoev899b44e2005-05-12 14:58:06 +000021
22 union {
Igor Sysoev6f134cc2006-05-23 14:54:58 +000023 ngx_str_t text;
Igor Sysoev899b44e2005-05-12 14:58:06 +000024
25 struct {
Igor Sysoev6f134cc2006-05-23 14:54:58 +000026 void *lengths;
27 void *values;
Igor Sysoev899b44e2005-05-12 14:58:06 +000028 } vars;
29
Igor Sysoev6f134cc2006-05-23 14:54:58 +000030 void *regex;
Igor Sysoev899b44e2005-05-12 14:58:06 +000031 } replacement;
32};
33
34
Igor Sysoev02f742b2005-04-08 15:18:55 +000035typedef struct {
Igor Sysoev6f134cc2006-05-23 14:54:58 +000036 ngx_http_upstream_conf_t upstream;
Igor Sysoev02f742b2005-04-08 15:18:55 +000037
Igor Sysoev6f134cc2006-05-23 14:54:58 +000038 ngx_array_t *flushes;
39 ngx_array_t *body_set_len;
40 ngx_array_t *body_set;
41 ngx_array_t *headers_set_len;
42 ngx_array_t *headers_set;
43 ngx_hash_t headers_set_hash;
Igor Sysoev02f742b2005-04-08 15:18:55 +000044
Igor Sysoev6f134cc2006-05-23 14:54:58 +000045 ngx_array_t *headers_source;
46 ngx_array_t *headers_names;
Igor Sysoev02f742b2005-04-08 15:18:55 +000047
Igor Sysoev6f134cc2006-05-23 14:54:58 +000048 ngx_array_t *redirects;
Igor Sysoev02f742b2005-04-08 15:18:55 +000049
Igor Sysoev6f134cc2006-05-23 14:54:58 +000050 ngx_str_t body_source;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000051
Igor Sysoev6f134cc2006-05-23 14:54:58 +000052 ngx_str_t method;
53 ngx_str_t host_header;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +000054 ngx_str_t port;
Igor Sysoev02f742b2005-04-08 15:18:55 +000055
Igor Sysoev6f134cc2006-05-23 14:54:58 +000056 ngx_flag_t redirect;
Igor Sysoev02f742b2005-04-08 15:18:55 +000057} ngx_http_proxy_loc_conf_t;
58
59
60typedef struct {
Igor Sysoev6f134cc2006-05-23 14:54:58 +000061 ngx_uint_t status;
62 ngx_uint_t status_count;
63 u_char *status_start;
64 u_char *status_end;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000065
Igor Sysoev6f134cc2006-05-23 14:54:58 +000066 size_t internal_body_length;
Igor Sysoev899b44e2005-05-12 14:58:06 +000067} ngx_http_proxy_ctx_t;
Igor Sysoev02f742b2005-04-08 15:18:55 +000068
Igor Sysoev02f742b2005-04-08 15:18:55 +000069
Igor Sysoev899b44e2005-05-12 14:58:06 +000070#define NGX_HTTP_PROXY_PARSE_NO_HEADER 20
Igor Sysoev02f742b2005-04-08 15:18:55 +000071
72
73static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
74static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r);
Igor Sysoev899b44e2005-05-12 14:58:06 +000075static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r);
76static ngx_int_t ngx_http_proxy_parse_status_line(ngx_http_request_t *r,
77 ngx_http_proxy_ctx_t *p);
Igor Sysoev02f742b2005-04-08 15:18:55 +000078static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r);
Igor Sysoev02f742b2005-04-08 15:18:55 +000079static void ngx_http_proxy_abort_request(ngx_http_request_t *r);
80static void ngx_http_proxy_finalize_request(ngx_http_request_t *r,
81 ngx_int_t rc);
82
Igor Sysoev09c684b2005-11-09 17:25:55 +000083static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r,
84 ngx_http_variable_value_t *v, uintptr_t data);
85static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r,
86 ngx_http_variable_value_t *v, uintptr_t data);
87static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +000088 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
Igor Sysoev09c684b2005-11-09 17:25:55 +000089 ngx_http_variable_value_t *v, uintptr_t data);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000090static ngx_int_t
91 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
92 ngx_http_variable_value_t *v, uintptr_t data);
Igor Sysoev899b44e2005-05-12 14:58:06 +000093static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
94 ngx_table_elt_t *h, size_t prefix);
Igor Sysoev02f742b2005-04-08 15:18:55 +000095
Igor Sysoev899b44e2005-05-12 14:58:06 +000096static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
Igor Sysoev02f742b2005-04-08 15:18:55 +000097static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
98static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
99 void *parent, void *child);
100
101static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
102 void *conf);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000103static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000104 void *conf);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000105
Igor Sysoev02f742b2005-04-08 15:18:55 +0000106static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
107
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000108static char *ngx_http_proxy_upstream_max_fails_unsupported(ngx_conf_t *cf,
109 ngx_command_t *cmd, void *conf);
110static char *ngx_http_proxy_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
111 ngx_command_t *cmd, void *conf);
112
Igor Sysoev899b44e2005-05-12 14:58:06 +0000113
Igor Sysoev02f742b2005-04-08 15:18:55 +0000114static ngx_conf_post_t ngx_http_proxy_lowat_post =
Igor Sysoev899b44e2005-05-12 14:58:06 +0000115 { ngx_http_proxy_lowat_check };
116
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000117static ngx_conf_deprecated_t ngx_conf_deprecated_proxy_header_buffer_size = {
118 ngx_conf_deprecated, "proxy_header_buffer_size", "proxy_buffer_size"
119};
120
Igor Sysoevef809b82006-06-28 16:00:26 +0000121static ngx_conf_deprecated_t ngx_conf_deprecated_proxy_redirect_errors = {
122 ngx_conf_deprecated, "proxy_redirect_errors", "proxy_intercept_errors"
123};
124
Igor Sysoev02f742b2005-04-08 15:18:55 +0000125
126static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = {
127 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
128 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
129 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
130 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
Igor Sysoev8fea8852006-03-15 09:53:04 +0000131 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
Igor Sysoev02f742b2005-04-08 15:18:55 +0000132 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
Igor Sysoevbb28b6d2006-07-11 13:20:19 +0000133 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
Igor Sysoev02f742b2005-04-08 15:18:55 +0000134 { ngx_null_string, 0 }
135};
136
137
138static ngx_command_t ngx_http_proxy_commands[] = {
139
140 { ngx_string("proxy_pass"),
Igor Sysoeve927b1e2006-11-14 12:43:14 +0000141 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000142 ngx_http_proxy_pass,
143 NGX_HTTP_LOC_CONF_OFFSET,
144 0,
145 NULL },
146
Igor Sysoev899b44e2005-05-12 14:58:06 +0000147 { ngx_string("proxy_redirect"),
148 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
149 ngx_http_proxy_redirect,
150 NGX_HTTP_LOC_CONF_OFFSET,
151 0,
152 NULL },
153
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000154 { ngx_string("proxy_buffering"),
155 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
156 ngx_conf_set_flag_slot,
157 NGX_HTTP_LOC_CONF_OFFSET,
158 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffering),
159 NULL },
160
Igor Sysoev6d16e1e2006-04-05 13:40:54 +0000161 { ngx_string("proxy_ignore_client_abort"),
162 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
163 ngx_conf_set_flag_slot,
164 NGX_HTTP_LOC_CONF_OFFSET,
165 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort),
166 NULL },
167
Igor Sysoev02f742b2005-04-08 15:18:55 +0000168 { ngx_string("proxy_connect_timeout"),
169 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
170 ngx_conf_set_msec_slot,
171 NGX_HTTP_LOC_CONF_OFFSET,
172 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
173 NULL },
174
175 { ngx_string("proxy_send_timeout"),
176 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
177 ngx_conf_set_msec_slot,
178 NGX_HTTP_LOC_CONF_OFFSET,
179 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
180 NULL },
181
182 { ngx_string("proxy_send_lowat"),
183 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
184 ngx_conf_set_size_slot,
185 NGX_HTTP_LOC_CONF_OFFSET,
186 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
187 &ngx_http_proxy_lowat_post },
188
Igor Sysoevef809b82006-06-28 16:00:26 +0000189 { ngx_string("proxy_intercept_errors"),
190 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
191 ngx_conf_set_flag_slot,
192 NGX_HTTP_LOC_CONF_OFFSET,
193 offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors),
194 NULL },
195
Igor Sysoev899b44e2005-05-12 14:58:06 +0000196 { ngx_string("proxy_redirect_errors"),
197 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
198 ngx_conf_set_flag_slot,
199 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoevef809b82006-06-28 16:00:26 +0000200 offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors),
201 &ngx_conf_deprecated_proxy_redirect_errors },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000202
Igor Sysoev899b44e2005-05-12 14:58:06 +0000203 { ngx_string("proxy_set_header"),
204 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000205 ngx_conf_set_keyval_slot,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000206 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev899b44e2005-05-12 14:58:06 +0000207 offsetof(ngx_http_proxy_loc_conf_t, headers_source),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000208 NULL },
209
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000210 { ngx_string("proxy_set_body"),
211 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
212 ngx_conf_set_str_slot,
213 NGX_HTTP_LOC_CONF_OFFSET,
214 offsetof(ngx_http_proxy_loc_conf_t, body_source),
215 NULL },
216
Igor Sysoev899b44e2005-05-12 14:58:06 +0000217 { ngx_string("proxy_method"),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000218 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
Igor Sysoev78452232005-10-12 13:50:36 +0000219 ngx_conf_set_str_slot,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000220 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev78452232005-10-12 13:50:36 +0000221 offsetof(ngx_http_proxy_loc_conf_t, method),
222 NULL },
Igor Sysoev02f742b2005-04-08 15:18:55 +0000223
Igor Sysoev899b44e2005-05-12 14:58:06 +0000224 { ngx_string("proxy_pass_request_headers"),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000225 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
226 ngx_conf_set_flag_slot,
227 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev899b44e2005-05-12 14:58:06 +0000228 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers),
229 NULL },
230
231 { ngx_string("proxy_pass_request_body"),
232 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
233 ngx_conf_set_flag_slot,
234 NGX_HTTP_LOC_CONF_OFFSET,
235 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000236 NULL },
237
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000238 { ngx_string("proxy_buffer_size"),
239 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
240 ngx_conf_set_size_slot,
241 NGX_HTTP_LOC_CONF_OFFSET,
242 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
243 NULL },
244
Igor Sysoev02f742b2005-04-08 15:18:55 +0000245 { ngx_string("proxy_header_buffer_size"),
246 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
247 ngx_conf_set_size_slot,
248 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000249 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
250 &ngx_conf_deprecated_proxy_header_buffer_size },
Igor Sysoev02f742b2005-04-08 15:18:55 +0000251
252 { ngx_string("proxy_read_timeout"),
253 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
254 ngx_conf_set_msec_slot,
255 NGX_HTTP_LOC_CONF_OFFSET,
256 offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout),
257 NULL },
258
259 { ngx_string("proxy_buffers"),
260 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
261 ngx_conf_set_bufs_slot,
262 NGX_HTTP_LOC_CONF_OFFSET,
263 offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
264 NULL },
265
266 { ngx_string("proxy_busy_buffers_size"),
267 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
268 ngx_conf_set_size_slot,
269 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev187b7d92005-07-14 12:51:53 +0000270 offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000271 NULL },
272
273 { ngx_string("proxy_temp_path"),
274 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
275 ngx_conf_set_path_slot,
276 NGX_HTTP_LOC_CONF_OFFSET,
277 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
278 (void *) ngx_garbage_collector_temp_handler },
279
280 { ngx_string("proxy_max_temp_file_size"),
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,
Igor Sysoev187b7d92005-07-14 12:51:53 +0000284 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000285 NULL },
286
287 { ngx_string("proxy_temp_file_write_size"),
288 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
289 ngx_conf_set_size_slot,
290 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev187b7d92005-07-14 12:51:53 +0000291 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000292 NULL },
293
294 { ngx_string("proxy_next_upstream"),
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000295 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000296 ngx_conf_set_bitmask_slot,
297 NGX_HTTP_LOC_CONF_OFFSET,
298 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
299 &ngx_http_proxy_next_upstream_masks },
300
Igor Sysoev78452232005-10-12 13:50:36 +0000301 { ngx_string("proxy_upstream_max_fails"),
302 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000303 ngx_http_proxy_upstream_max_fails_unsupported,
304 0,
305 0,
Igor Sysoev78452232005-10-12 13:50:36 +0000306 NULL },
307
308 { ngx_string("proxy_upstream_fail_timeout"),
309 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000310 ngx_http_proxy_upstream_fail_timeout_unsupported,
311 0,
312 0,
Igor Sysoev78452232005-10-12 13:50:36 +0000313 NULL },
314
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000315 { ngx_string("proxy_pass_header"),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000316 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000317 ngx_conf_set_str_array_slot,
Igor Sysoev899b44e2005-05-12 14:58:06 +0000318 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000319 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000320 NULL },
321
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000322 { ngx_string("proxy_hide_header"),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000323 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000324 ngx_conf_set_str_array_slot,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000325 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000326 offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000327 NULL },
328
329 ngx_null_command
330};
331
332
Igor Sysoev8f125582006-07-28 15:16:17 +0000333static ngx_http_module_t ngx_http_proxy_module_ctx = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000334 ngx_http_proxy_add_variables, /* preconfiguration */
335 NULL, /* postconfiguration */
Igor Sysoev02f742b2005-04-08 15:18:55 +0000336
337 NULL, /* create main configuration */
338 NULL, /* init main configuration */
339
340 NULL, /* create server configuration */
341 NULL, /* merge server configuration */
342
343 ngx_http_proxy_create_loc_conf, /* create location configration */
344 ngx_http_proxy_merge_loc_conf /* merge location configration */
345};
346
347
348ngx_module_t ngx_http_proxy_module = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000349 NGX_MODULE_V1,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000350 &ngx_http_proxy_module_ctx, /* module context */
351 ngx_http_proxy_commands, /* module directives */
352 NGX_HTTP_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000353 NULL, /* init master */
Igor Sysoev899b44e2005-05-12 14:58:06 +0000354 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000355 NULL, /* init process */
356 NULL, /* init thread */
357 NULL, /* exit thread */
358 NULL, /* exit process */
359 NULL, /* exit master */
360 NGX_MODULE_V1_PADDING
Igor Sysoev02f742b2005-04-08 15:18:55 +0000361};
362
363
Igor Sysoev02f742b2005-04-08 15:18:55 +0000364static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
365
Igor Sysoev899b44e2005-05-12 14:58:06 +0000366
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000367static ngx_keyval_t ngx_http_proxy_headers[] = {
368 { ngx_string("Host"), ngx_string("$proxy_host") },
369 { ngx_string("Connection"), ngx_string("close") },
370 { ngx_string("Keep-Alive"), ngx_string("") },
371 { ngx_null_string, ngx_null_string }
372};
373
374
375static ngx_str_t ngx_http_proxy_hide_headers[] = {
376 ngx_string("Date"),
377 ngx_string("Server"),
378 ngx_string("X-Pad"),
379 ngx_string("X-Accel-Expires"),
380 ngx_string("X-Accel-Redirect"),
381 ngx_string("X-Accel-Limit-Rate"),
382 ngx_string("X-Accel-Buffer"),
383 ngx_null_string
Igor Sysoev899b44e2005-05-12 14:58:06 +0000384};
385
386
387static ngx_http_variable_t ngx_http_proxy_vars[] = {
388
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000389 { ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000390 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000391
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000392 { ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000393 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000394
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000395 { ngx_string("proxy_add_x_forwarded_for"), NULL,
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000396 ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000397
398#if 0
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000399 { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000400#endif
401
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000402 { ngx_string("proxy_internal_body_length"), NULL,
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000403 ngx_http_proxy_internal_body_length_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000404
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000405 { ngx_null_string, NULL, NULL, 0, 0, 0 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000406};
Igor Sysoev02f742b2005-04-08 15:18:55 +0000407
408
Igor Sysoev02f742b2005-04-08 15:18:55 +0000409static ngx_int_t
410ngx_http_proxy_handler(ngx_http_request_t *r)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000411{
Igor Sysoev02f742b2005-04-08 15:18:55 +0000412 ngx_int_t rc;
413 ngx_http_upstream_t *u;
414 ngx_http_proxy_loc_conf_t *plcf;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000415
Igor Sysoev02f742b2005-04-08 15:18:55 +0000416 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000417
Igor Sysoev02f742b2005-04-08 15:18:55 +0000418 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
419 if (u == NULL) {
420 return NGX_HTTP_INTERNAL_SERVER_ERROR;
421 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000422
Igor Sysoev02f742b2005-04-08 15:18:55 +0000423 u->peer.log = r->connection->log;
424 u->peer.log_error = NGX_ERROR_ERR;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000425#if (NGX_THREADS)
426 u->peer.lock = &r->connection->lock;
427#endif
428
429 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
430
431 u->conf = &plcf->upstream;
432
433 u->create_request = ngx_http_proxy_create_request;
434 u->reinit_request = ngx_http_proxy_reinit_request;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000435 u->process_header = ngx_http_proxy_process_status_line;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000436 u->abort_request = ngx_http_proxy_abort_request;
437 u->finalize_request = ngx_http_proxy_finalize_request;
438
Igor Sysoev899b44e2005-05-12 14:58:06 +0000439 if (plcf->redirects) {
440 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
441 }
442
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000443 u->buffering = plcf->upstream.buffering;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000444
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000445 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
446 if (u->pipe == NULL) {
447 return NGX_HTTP_INTERNAL_SERVER_ERROR;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000448 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000449
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000450 u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
451
Igor Sysoev899b44e2005-05-12 14:58:06 +0000452 u->accel = 1;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000453
Igor Sysoev02f742b2005-04-08 15:18:55 +0000454 r->upstream = u;
455
456 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
457
458 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
459 return rc;
460 }
461
462 return NGX_DONE;
463}
464
465
466static ngx_int_t
467ngx_http_proxy_create_request(ngx_http_request_t *r)
468{
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000469 size_t len, loc_len, body_len;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000470 uintptr_t escape;
471 ngx_buf_t *b;
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000472 ngx_str_t method;
473 ngx_uint_t i, unparsed_uri;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000474 ngx_chain_t *cl, *body;
475 ngx_list_part_t *part;
476 ngx_table_elt_t *header;
477 ngx_http_upstream_t *u;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000478 ngx_http_proxy_ctx_t *p;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000479 ngx_http_script_code_pt code;
480 ngx_http_script_engine_t e, le;
481 ngx_http_proxy_loc_conf_t *plcf;
482 ngx_http_script_len_code_pt lcode;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000483
484 u = r->upstream;
485
486 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
487
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000488 p = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
489 if (p == NULL) {
490 return NGX_HTTP_INTERNAL_SERVER_ERROR;
491 }
492
493 ngx_http_set_ctx(r, p, ngx_http_proxy_module);
494
Igor Sysoev02f742b2005-04-08 15:18:55 +0000495 len = sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;
496
Igor Sysoev78452232005-10-12 13:50:36 +0000497 if (u->method.len) {
498 /* HEAD was changed to GET to cache response */
499 method = u->method;
500 method.len++;
501
502 } else if (plcf->method.len) {
503 method = plcf->method;
504
Igor Sysoev02f742b2005-04-08 15:18:55 +0000505 } else {
Igor Sysoev78452232005-10-12 13:50:36 +0000506 method = r->method_name;
507 method.len++;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000508 }
509
Igor Sysoev78452232005-10-12 13:50:36 +0000510 len += method.len + u->conf->uri.len;
511
Igor Sysoev02f742b2005-04-08 15:18:55 +0000512 escape = 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000513
Igor Sysoev6275f562006-03-28 12:24:47 +0000514 loc_len = (r->valid_location && u->conf->uri.len) ? u->conf->location.len:
515 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000516
Igor Sysoev8290d282006-02-03 12:58:48 +0000517 if (u->conf->uri.len == 0 && r->valid_unparsed_uri && r == r->main) {
518 unparsed_uri = 1;
Igor Sysoevceb99292005-09-06 16:09:32 +0000519 len += r->unparsed_uri.len;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000520
521 } else {
Igor Sysoev8290d282006-02-03 12:58:48 +0000522 unparsed_uri = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000523 if (r->quoted_uri) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000524 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
525 r->uri.len - loc_len, NGX_ESCAPE_URI);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000526 }
527
Igor Sysoev899b44e2005-05-12 14:58:06 +0000528 len += r->uri.len - loc_len + escape + sizeof("?") - 1 + r->args.len;
529 }
530
Igor Sysoev09c684b2005-11-09 17:25:55 +0000531 ngx_http_script_flush_no_cachable_variables(r, plcf->flushes);
532
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000533 if (plcf->body_set_len) {
534 le.ip = plcf->body_set_len->elts;
535 le.request = r;
536 le.flushed = 1;
537 body_len = 0;
538
539 while (*(uintptr_t *) le.ip) {
540 lcode = *(ngx_http_script_len_code_pt *) le.ip;
541 body_len += lcode(&le);
542 }
543
544 p->internal_body_length = body_len;
545 len += body_len;
546 }
547
Igor Sysoev899b44e2005-05-12 14:58:06 +0000548 le.ip = plcf->headers_set_len->elts;
549 le.request = r;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000550 le.flushed = 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000551
552 while (*(uintptr_t *) le.ip) {
553 while (*(uintptr_t *) le.ip) {
554 lcode = *(ngx_http_script_len_code_pt *) le.ip;
555 len += lcode(&le);
556 }
557 le.ip += sizeof(uintptr_t);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000558 }
559
560
Igor Sysoev899b44e2005-05-12 14:58:06 +0000561 if (plcf->upstream.pass_request_headers) {
562 part = &r->headers_in.headers.part;
563 header = part->elts;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000564
Igor Sysoev899b44e2005-05-12 14:58:06 +0000565 for (i = 0; /* void */; i++) {
566
567 if (i >= part->nelts) {
568 if (part->next == NULL) {
569 break;
570 }
571
572 part = part->next;
573 header = part->elts;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000574 i = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000575 }
576
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000577 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
578 header[i].lowcase_key, header[i].key.len))
Igor Sysoev899b44e2005-05-12 14:58:06 +0000579 {
580 continue;
581 }
582
583 len += header[i].key.len + sizeof(": ") - 1
584 + header[i].value.len + sizeof(CRLF) - 1;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000585 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000586 }
587
Igor Sysoev899b44e2005-05-12 14:58:06 +0000588
Igor Sysoev02f742b2005-04-08 15:18:55 +0000589 b = ngx_create_temp_buf(r->pool, len);
590 if (b == NULL) {
591 return NGX_ERROR;
592 }
593
594 cl = ngx_alloc_chain_link(r->pool);
595 if (cl == NULL) {
596 return NGX_ERROR;
597 }
598
599 cl->buf = b;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000600
601
602 /* the request line */
603
Igor Sysoev09c684b2005-11-09 17:25:55 +0000604 b->last = ngx_copy(b->last, method.data, method.len);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000605
Igor Sysoeva2573672005-10-05 14:46:21 +0000606 u->uri.data = b->last;
607
Igor Sysoev8290d282006-02-03 12:58:48 +0000608 if (unparsed_uri) {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000609 b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);
610
Igor Sysoev02f742b2005-04-08 15:18:55 +0000611 } else {
Igor Sysoeva2573672005-10-05 14:46:21 +0000612 if (r->valid_location) {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000613 b->last = ngx_copy(b->last, u->conf->uri.data, u->conf->uri.len);
Igor Sysoeva2573672005-10-05 14:46:21 +0000614 }
Igor Sysoev82dc95e2005-10-03 12:53:14 +0000615
Igor Sysoev02f742b2005-04-08 15:18:55 +0000616 if (escape) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000617 ngx_escape_uri(b->last, r->uri.data + loc_len,
618 r->uri.len - loc_len, NGX_ESCAPE_URI);
619 b->last += r->uri.len - loc_len + escape;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000620
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000621 } else {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000622 b->last = ngx_copy(b->last, r->uri.data + loc_len,
623 r->uri.len - loc_len);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000624 }
625
626 if (r->args.len > 0) {
627 *b->last++ = '?';
Igor Sysoev09c684b2005-11-09 17:25:55 +0000628 b->last = ngx_copy(b->last, r->args.data, r->args.len);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000629 }
630 }
631
Igor Sysoeva2573672005-10-05 14:46:21 +0000632 u->uri.len = b->last - u->uri.data;
633
Igor Sysoev02f742b2005-04-08 15:18:55 +0000634 b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
635 sizeof(ngx_http_proxy_version) - 1);
636
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000637 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
Igor Sysoev02f742b2005-04-08 15:18:55 +0000638
639 e.ip = plcf->headers_set->elts;
640 e.pos = b->last;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000641 e.request = r;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000642 e.flushed = 1;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000643
Igor Sysoev899b44e2005-05-12 14:58:06 +0000644 le.ip = plcf->headers_set_len->elts;
645
646 while (*(uintptr_t *) le.ip) {
647 lcode = *(ngx_http_script_len_code_pt *) le.ip;
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000648
649 /* skip the header line name length */
650 (void) lcode(&le);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000651
652 if (*(ngx_http_script_len_code_pt *) le.ip) {
653
654 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
655 lcode = *(ngx_http_script_len_code_pt *) le.ip;
656 }
657
658 e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0;
659
660 } else {
661 e.skip = 0;
662 }
663
664 le.ip += sizeof(uintptr_t);
665
666 while (*(uintptr_t *) e.ip) {
667 code = *(ngx_http_script_code_pt *) e.ip;
668 code((ngx_http_script_engine_t *) &e);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000669 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000670 e.ip += sizeof(uintptr_t);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000671 }
672
673 b->last = e.pos;
674
675
Igor Sysoev899b44e2005-05-12 14:58:06 +0000676 if (plcf->upstream.pass_request_headers) {
677 part = &r->headers_in.headers.part;
678 header = part->elts;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000679
Igor Sysoev899b44e2005-05-12 14:58:06 +0000680 for (i = 0; /* void */; i++) {
Igor Sysoev02f742b2005-04-08 15:18:55 +0000681
Igor Sysoev899b44e2005-05-12 14:58:06 +0000682 if (i >= part->nelts) {
683 if (part->next == NULL) {
684 break;
685 }
686
687 part = part->next;
688 header = part->elts;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000689 i = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000690 }
691
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000692 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
693 header[i].lowcase_key, header[i].key.len))
Igor Sysoev899b44e2005-05-12 14:58:06 +0000694 {
695 continue;
696 }
697
Igor Sysoev09c684b2005-11-09 17:25:55 +0000698 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000699
700 *b->last++ = ':'; *b->last++ = ' ';
701
Igor Sysoev09c684b2005-11-09 17:25:55 +0000702 b->last = ngx_copy(b->last, header[i].value.data,
703 header[i].value.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000704
705 *b->last++ = CR; *b->last++ = LF;
706
707 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
708 "http proxy header: \"%V: %V\"",
709 &header[i].key, &header[i].value);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000710 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000711 }
712
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000713
Igor Sysoev02f742b2005-04-08 15:18:55 +0000714 /* add "\r\n" at the header end */
715 *b->last++ = CR; *b->last++ = LF;
716
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000717 if (plcf->body_set) {
718 e.ip = plcf->body_set->elts;
719 e.pos = b->last;
720
721 while (*(uintptr_t *) e.ip) {
722 code = *(ngx_http_script_code_pt *) e.ip;
723 code((ngx_http_script_engine_t *) &e);
724 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000725
Igor Sysoevb141f2b2005-11-15 14:49:57 +0000726 b->last = e.pos;
727 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000728
Igor Sysoev02f742b2005-04-08 15:18:55 +0000729#if (NGX_DEBUG)
730 {
731 ngx_str_t s;
732
733 s.len = b->last - b->pos;
734 s.data = b->pos;
735 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
736 "http proxy header:\n\"%V\"", &s);
737 }
738#endif
739
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000740 if (plcf->body_set == NULL && plcf->upstream.pass_request_body) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000741
742 body = u->request_bufs;
743 u->request_bufs = cl;
744
745 while (body) {
746 b = ngx_alloc_buf(r->pool);
747 if (b == NULL) {
748 return NGX_ERROR;
749 }
750
751 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
752
753 cl->next = ngx_alloc_chain_link(r->pool);
754 if (cl->next == NULL) {
755 return NGX_ERROR;
756 }
757
758 cl = cl->next;
759 cl->buf = b;
760
761 body = body->next;
762 }
763
Igor Sysoevffe71442006-02-08 15:33:12 +0000764 b->flush = 1;
765
Igor Sysoev899b44e2005-05-12 14:58:06 +0000766 } else {
767 u->request_bufs = cl;
768 }
769
770 cl->next = NULL;
771
Igor Sysoev02f742b2005-04-08 15:18:55 +0000772 return NGX_OK;
773}
774
775
776static ngx_int_t
777ngx_http_proxy_reinit_request(ngx_http_request_t *r)
778{
Igor Sysoev899b44e2005-05-12 14:58:06 +0000779 ngx_http_proxy_ctx_t *p;
780
781 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
782
783 if (p == NULL) {
784 return NGX_OK;
785 }
786
787 p->status = 0;
788 p->status_count = 0;
789 p->status_start = NULL;
790 p->status_end = NULL;
791
792 r->upstream->process_header = ngx_http_proxy_process_status_line;
793
794 return NGX_OK;
795}
796
797
798static ngx_int_t
799ngx_http_proxy_process_status_line(ngx_http_request_t *r)
800{
801 ngx_int_t rc;
802 ngx_http_upstream_t *u;
803 ngx_http_proxy_ctx_t *p;
804
805 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
806
807 if (p == NULL) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000808 return NGX_HTTP_INTERNAL_SERVER_ERROR;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000809 }
810
811 rc = ngx_http_proxy_parse_status_line(r, p);
812
813 if (rc == NGX_AGAIN) {
814 return rc;
815 }
816
817 u = r->upstream;
818
819 if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
820 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000821 "upstream sent no valid HTTP/1.0 header");
Igor Sysoev899b44e2005-05-12 14:58:06 +0000822
Igor Sysoev13c68742006-03-10 12:51:52 +0000823#if 0
Igor Sysoev899b44e2005-05-12 14:58:06 +0000824 if (u->accel) {
825 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
826 }
Igor Sysoev13c68742006-03-10 12:51:52 +0000827#endif
Igor Sysoev899b44e2005-05-12 14:58:06 +0000828
829 r->http_version = NGX_HTTP_VERSION_9;
830 p->status = NGX_HTTP_OK;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000831
Igor Sysoev899b44e2005-05-12 14:58:06 +0000832 return NGX_OK;
833 }
834
Igor Sysoev187b7d92005-07-14 12:51:53 +0000835 u->headers_in.status_n = p->status;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000836 u->state->status = p->status;
837
Igor Sysoev187b7d92005-07-14 12:51:53 +0000838 u->headers_in.status_line.len = p->status_end - p->status_start;
839 u->headers_in.status_line.data = ngx_palloc(r->pool,
840 u->headers_in.status_line.len);
841 if (u->headers_in.status_line.data == NULL) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000842 return NGX_HTTP_INTERNAL_SERVER_ERROR;
843 }
Igor Sysoev09c684b2005-11-09 17:25:55 +0000844
Igor Sysoev187b7d92005-07-14 12:51:53 +0000845 ngx_memcpy(u->headers_in.status_line.data, p->status_start,
846 u->headers_in.status_line.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000847
848 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
849 "http proxy status %ui \"%V\"",
Igor Sysoev4e08b9c2006-12-20 19:38:06 +0000850 u->headers_in.status_n, &u->headers_in.status_line);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000851
852 u->process_header = ngx_http_proxy_process_header;
853
854 return ngx_http_proxy_process_header(r);
855}
856
857
858static ngx_int_t
859ngx_http_proxy_parse_status_line(ngx_http_request_t *r, ngx_http_proxy_ctx_t *p)
860{
861 u_char ch;
862 u_char *pos;
863 ngx_http_upstream_t *u;
864 enum {
865 sw_start = 0,
866 sw_H,
867 sw_HT,
868 sw_HTT,
869 sw_HTTP,
870 sw_first_major_digit,
871 sw_major_digit,
872 sw_first_minor_digit,
873 sw_minor_digit,
874 sw_status,
875 sw_space_after_status,
876 sw_status_text,
877 sw_almost_done
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000878 } state;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000879
880 u = r->upstream;
881
882 state = r->state;
883
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000884 for (pos = u->buffer.pos; pos < u->buffer.last; pos++) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000885 ch = *pos;
886
887 switch (state) {
888
889 /* "HTTP/" */
890 case sw_start:
891 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000892 case 'H':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000893 state = sw_H;
894 break;
895 default:
896 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
897 }
898 break;
899
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000900 case sw_H:
Igor Sysoev899b44e2005-05-12 14:58:06 +0000901 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000902 case 'T':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000903 state = sw_HT;
904 break;
905 default:
906 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
907 }
908 break;
909
910 case sw_HT:
911 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000912 case 'T':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000913 state = sw_HTT;
914 break;
915 default:
916 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
917 }
918 break;
919
920 case sw_HTT:
921 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000922 case 'P':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000923 state = sw_HTTP;
924 break;
925 default:
926 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
927 }
928 break;
929
930 case sw_HTTP:
931 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000932 case '/':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000933 state = sw_first_major_digit;
934 break;
935 default:
936 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
937 }
938 break;
939
940 /* the first digit of major HTTP version */
941 case sw_first_major_digit:
942 if (ch < '1' || ch > '9') {
943 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
944 }
945
946 state = sw_major_digit;
947 break;
948
949 /* the major HTTP version or dot */
950 case sw_major_digit:
951 if (ch == '.') {
952 state = sw_first_minor_digit;
953 break;
954 }
955
956 if (ch < '0' || ch > '9') {
957 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
958 }
959
960 break;
961
962 /* the first digit of minor HTTP version */
963 case sw_first_minor_digit:
964 if (ch < '0' || ch > '9') {
965 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
966 }
967
968 state = sw_minor_digit;
969 break;
970
971 /* the minor HTTP version or the end of the request line */
972 case sw_minor_digit:
973 if (ch == ' ') {
974 state = sw_status;
975 break;
976 }
977
978 if (ch < '0' || ch > '9') {
979 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
980 }
981
982 break;
983
984 /* HTTP status code */
985 case sw_status:
Igor Sysoev13c68742006-03-10 12:51:52 +0000986 if (ch == ' ') {
987 break;
988 }
989
Igor Sysoev899b44e2005-05-12 14:58:06 +0000990 if (ch < '0' || ch > '9') {
991 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
992 }
993
994 p->status = p->status * 10 + ch - '0';
995
996 if (++p->status_count == 3) {
997 state = sw_space_after_status;
998 p->status_start = pos - 2;
999 }
1000
1001 break;
1002
1003 /* space or end of line */
1004 case sw_space_after_status:
1005 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001006 case ' ':
Igor Sysoev899b44e2005-05-12 14:58:06 +00001007 state = sw_status_text;
1008 break;
1009 case '.': /* IIS may send 403.1, 403.2, etc */
1010 state = sw_status_text;
1011 break;
1012 case CR:
1013 state = sw_almost_done;
1014 break;
1015 case LF:
1016 goto done;
1017 default:
1018 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
1019 }
1020 break;
1021
1022 /* any text until end of line */
1023 case sw_status_text:
1024 switch (ch) {
1025 case CR:
1026 state = sw_almost_done;
1027
1028 break;
1029 case LF:
1030 goto done;
1031 }
1032 break;
1033
Igor Sysoev5192b362005-07-08 14:34:20 +00001034 /* end of status line */
Igor Sysoev899b44e2005-05-12 14:58:06 +00001035 case sw_almost_done:
1036 p->status_end = pos - 1;
1037 switch (ch) {
1038 case LF:
1039 goto done;
1040 default:
1041 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
1042 }
1043 }
1044 }
1045
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001046 u->buffer.pos = pos;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001047 r->state = state;
1048
1049 return NGX_AGAIN;
1050
1051done:
1052
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001053 u->buffer.pos = pos + 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001054
1055 if (p->status_end == NULL) {
1056 p->status_end = pos;
1057 }
1058
1059 r->state = sw_start;
1060
Igor Sysoev02f742b2005-04-08 15:18:55 +00001061 return NGX_OK;
1062}
1063
1064
1065static ngx_int_t
1066ngx_http_proxy_process_header(ngx_http_request_t *r)
1067{
Igor Sysoev899b44e2005-05-12 14:58:06 +00001068 ngx_int_t rc;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001069 ngx_uint_t i;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001070 ngx_table_elt_t *h;
1071 ngx_http_upstream_header_t *hh;
1072 ngx_http_upstream_main_conf_t *umcf;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001073
Igor Sysoev899b44e2005-05-12 14:58:06 +00001074 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
Igor Sysoev02f742b2005-04-08 15:18:55 +00001075
Igor Sysoev899b44e2005-05-12 14:58:06 +00001076 for ( ;; ) {
1077
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001078 rc = ngx_http_parse_header_line(r, &r->upstream->buffer);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001079
1080 if (rc == NGX_OK) {
1081
1082 /* a header line has been parsed successfully */
1083
1084 h = ngx_list_push(&r->upstream->headers_in.headers);
1085 if (h == NULL) {
1086 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1087 }
1088
1089 h->hash = r->header_hash;
1090
1091 h->key.len = r->header_name_end - r->header_name_start;
1092 h->value.len = r->header_end - r->header_start;
1093
1094 h->key.data = ngx_palloc(r->pool,
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001095 h->key.len + 1 + h->value.len + 1 + h->key.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001096 if (h->key.data == NULL) {
1097 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1098 }
1099
1100 h->value.data = h->key.data + h->key.len + 1;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001101 h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001102
1103 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
1104 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
1105
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001106 if (h->key.len == r->lowcase_index) {
1107 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001108
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001109 } else {
1110 for (i = 0; i < h->key.len; i++) {
1111 h->lowcase_key[i] = ngx_tolower(h->lowcase_key[i]);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001112 }
1113 }
1114
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001115 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
1116 h->lowcase_key, h->key.len);
1117
1118 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
1119 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1120 }
1121
Igor Sysoev899b44e2005-05-12 14:58:06 +00001122 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1123 "http proxy header: \"%V: %V\"",
1124 &h->key, &h->value);
1125
1126 continue;
1127 }
1128
1129 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1130
1131 /* a whole header has been parsed successfully */
1132
1133 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1134 "http proxy header done");
1135
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001136 /*
1137 * if no "Server" and "Date" in header line,
1138 * then add the special empty headers
1139 */
1140
1141 if (r->upstream->headers_in.server == NULL) {
1142 h = ngx_list_push(&r->upstream->headers_in.headers);
1143 if (h == NULL) {
1144 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1145 }
1146
1147 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
1148 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
1149
1150 h->key.len = sizeof("Server") - 1;
1151 h->key.data = (u_char *) "Server";
1152 h->value.len = 0;
1153 h->value.data = NULL;
1154 h->lowcase_key = (u_char *) "server";
1155 }
1156
1157 if (r->upstream->headers_in.date == NULL) {
1158 h = ngx_list_push(&r->upstream->headers_in.headers);
1159 if (h == NULL) {
1160 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1161 }
1162
1163 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');
1164
1165 h->key.len = sizeof("Date") - 1;
1166 h->key.data = (u_char *) "Date";
1167 h->value.len = 0;
1168 h->value.data = NULL;
1169 h->lowcase_key = (u_char *) "date";
1170 }
1171
Igor Sysoev899b44e2005-05-12 14:58:06 +00001172 return NGX_OK;
1173 }
1174
Igor Sysoev7b190b42005-06-07 15:56:31 +00001175 if (rc == NGX_AGAIN) {
1176 return NGX_AGAIN;
1177 }
1178
Igor Sysoev899b44e2005-05-12 14:58:06 +00001179 /* there was error while a header line parsing */
1180
1181 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
Igor Sysoev13c68742006-03-10 12:51:52 +00001182 "upstream sent invalid header");
Igor Sysoev899b44e2005-05-12 14:58:06 +00001183
1184 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1185 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001186}
1187
1188
1189static void
1190ngx_http_proxy_abort_request(ngx_http_request_t *r)
1191{
1192 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1193 "abort http proxy request");
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001194
Igor Sysoev02f742b2005-04-08 15:18:55 +00001195 return;
1196}
1197
1198
1199static void
1200ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001201{
Igor Sysoev02f742b2005-04-08 15:18:55 +00001202 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1203 "finalize http proxy request");
1204
1205 return;
1206}
1207
1208
Igor Sysoev09c684b2005-11-09 17:25:55 +00001209static ngx_int_t
1210ngx_http_proxy_host_variable(ngx_http_request_t *r,
1211 ngx_http_variable_value_t *v, uintptr_t data)
Igor Sysoev02f742b2005-04-08 15:18:55 +00001212{
Igor Sysoev02f742b2005-04-08 15:18:55 +00001213 ngx_http_proxy_loc_conf_t *plcf;
1214
Igor Sysoev02f742b2005-04-08 15:18:55 +00001215 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
1216
Igor Sysoev09c684b2005-11-09 17:25:55 +00001217 v->len = plcf->host_header.len;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001218 v->valid = 1;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001219 v->no_cachable = 0;
1220 v->not_found = 0;
1221 v->data = plcf->host_header.data;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001222
Igor Sysoev09c684b2005-11-09 17:25:55 +00001223 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001224}
1225
1226
Igor Sysoev09c684b2005-11-09 17:25:55 +00001227static ngx_int_t
1228ngx_http_proxy_port_variable(ngx_http_request_t *r,
1229 ngx_http_variable_value_t *v, uintptr_t data)
Igor Sysoev899b44e2005-05-12 14:58:06 +00001230{
Igor Sysoev899b44e2005-05-12 14:58:06 +00001231 ngx_http_proxy_loc_conf_t *plcf;
1232
Igor Sysoev899b44e2005-05-12 14:58:06 +00001233 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
1234
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00001235 v->len = plcf->port.len;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001236 v->valid = 1;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001237 v->no_cachable = 0;
1238 v->not_found = 0;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00001239 v->data = plcf->port.data;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001240
Igor Sysoev09c684b2005-11-09 17:25:55 +00001241 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001242}
1243
1244
Igor Sysoev09c684b2005-11-09 17:25:55 +00001245static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00001246ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
Igor Sysoev09c684b2005-11-09 17:25:55 +00001247 ngx_http_variable_value_t *v, uintptr_t data)
Igor Sysoev899b44e2005-05-12 14:58:06 +00001248{
Igor Sysoev09c684b2005-11-09 17:25:55 +00001249 u_char *p;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001250
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001251 v->valid = 1;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001252 v->no_cachable = 0;
1253 v->not_found = 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001254
1255 if (r->headers_in.x_forwarded_for == NULL) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00001256 v->len = r->connection->addr_text.len;
1257 v->data = r->connection->addr_text.data;
1258 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001259 }
1260
Igor Sysoev09c684b2005-11-09 17:25:55 +00001261 v->len = r->headers_in.x_forwarded_for->value.len
1262 + sizeof(", ") - 1 + r->connection->addr_text.len;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001263
Igor Sysoev09c684b2005-11-09 17:25:55 +00001264 p = ngx_palloc(r->pool, v->len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001265 if (p == NULL) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00001266 return NGX_ERROR;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001267 }
1268
Igor Sysoev09c684b2005-11-09 17:25:55 +00001269 v->data = p;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001270
Igor Sysoev09c684b2005-11-09 17:25:55 +00001271 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
1272 r->headers_in.x_forwarded_for->value.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001273
1274 *p++ = ','; *p++ = ' ';
1275
1276 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
1277
Igor Sysoev09c684b2005-11-09 17:25:55 +00001278 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001279}
1280
1281
1282static ngx_int_t
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001283ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
1284 ngx_http_variable_value_t *v, uintptr_t data)
1285{
1286 ngx_http_proxy_ctx_t *p;
1287
1288 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1289
1290 if (p == NULL) {
1291 v->not_found = 1;
1292 return NGX_OK;
1293 }
1294
1295 v->valid = 1;
1296 v->no_cachable = 0;
1297 v->not_found = 0;
1298
1299 v->data = ngx_palloc(r->connection->pool, NGX_SIZE_T_LEN);
1300
1301 if (v->data == NULL) {
1302 return NGX_ERROR;
1303 }
1304
1305 v->len = ngx_sprintf(v->data, "%uz", p->internal_body_length) - v->data;
1306
1307 return NGX_OK;
1308}
1309
1310
1311static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00001312ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
1313 size_t prefix)
1314{
1315 ngx_int_t rc;
1316 ngx_uint_t i;
1317 ngx_http_proxy_loc_conf_t *plcf;
1318 ngx_http_proxy_redirect_t *pr;
1319
1320 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
1321
1322 pr = plcf->redirects->elts;
1323
1324 if (pr == NULL) {
1325 return NGX_DECLINED;
1326 }
1327
1328 for (i = 0; i < plcf->redirects->nelts; i++) {
Igor Sysoev9e580192006-02-01 18:22:15 +00001329 rc = pr[i].handler(r, h, prefix, &pr[i]);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001330
1331 if (rc != NGX_DECLINED) {
1332 return rc;
1333 }
1334 }
1335
1336 return NGX_DECLINED;
1337}
1338
1339
1340static ngx_int_t
1341ngx_http_proxy_rewrite_redirect_text(ngx_http_request_t *r, ngx_table_elt_t *h,
1342 size_t prefix, ngx_http_proxy_redirect_t *pr)
1343{
1344 size_t len;
1345 u_char *data, *p;
1346
1347 if (pr->redirect.len > h->value.len - prefix
1348 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
1349 pr->redirect.len) != 0)
1350 {
1351 return NGX_DECLINED;
1352 }
1353
1354 len = prefix + pr->replacement.text.len + h->value.len - pr->redirect.len;
1355
1356 data = ngx_palloc(r->pool, len);
1357 if (data == NULL) {
1358 return NGX_ERROR;
1359 }
1360
1361 p = data;
1362
Igor Sysoev09c684b2005-11-09 17:25:55 +00001363 p = ngx_copy(p, h->value.data, prefix);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001364
Igor Sysoevafd7ec52006-05-29 17:28:12 +00001365 if (pr->replacement.text.len) {
1366 p = ngx_copy(p, pr->replacement.text.data, pr->replacement.text.len);
1367 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00001368
1369 ngx_memcpy(p, h->value.data + prefix + pr->redirect.len,
1370 h->value.len - pr->redirect.len - prefix);
1371
1372 h->value.len = len;
1373 h->value.data = data;
1374
1375 return NGX_OK;
1376}
1377
1378
1379static ngx_int_t
1380ngx_http_proxy_rewrite_redirect_vars(ngx_http_request_t *r, ngx_table_elt_t *h,
1381 size_t prefix, ngx_http_proxy_redirect_t *pr)
1382{
1383 size_t len;
1384 u_char *data, *p;
1385 ngx_http_script_code_pt code;
1386 ngx_http_script_engine_t e;
1387 ngx_http_script_len_code_pt lcode;
1388
1389 if (pr->redirect.len > h->value.len - prefix
1390 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
1391 pr->redirect.len) != 0)
1392 {
1393 return NGX_DECLINED;
1394 }
1395
1396 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
1397
1398 e.ip = pr->replacement.vars.lengths;
1399 e.request = r;
1400
Igor Sysoevc9098082006-09-26 21:15:52 +00001401 len = prefix + h->value.len - pr->redirect.len;
1402
1403 while (*(uintptr_t *) e.ip) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00001404 lcode = *(ngx_http_script_len_code_pt *) e.ip;
Igor Sysoevc9098082006-09-26 21:15:52 +00001405 len += lcode(&e);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001406 }
1407
1408 data = ngx_palloc(r->pool, len);
1409 if (data == NULL) {
1410 return NGX_ERROR;
1411 }
1412
1413 p = data;
1414
Igor Sysoev09c684b2005-11-09 17:25:55 +00001415 p = ngx_copy(p, h->value.data, prefix);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001416
1417 e.ip = pr->replacement.vars.values;
1418 e.pos = p;
1419
1420 while (*(uintptr_t *) e.ip) {
1421 code = *(ngx_http_script_code_pt *) e.ip;
1422 code(&e);
1423 }
1424
Igor Sysoevc9098082006-09-26 21:15:52 +00001425 ngx_memcpy(e.pos, h->value.data + prefix + pr->redirect.len,
1426 h->value.len - pr->redirect.len - prefix);
1427
Igor Sysoev899b44e2005-05-12 14:58:06 +00001428 h->value.len = len;
1429 h->value.data = data;
1430
1431 return NGX_OK;
1432}
1433
1434
1435static ngx_int_t
1436ngx_http_proxy_add_variables(ngx_conf_t *cf)
1437{
1438 ngx_http_variable_t *var, *v;
1439
1440 for (v = ngx_http_proxy_vars; v->name.len; v++) {
1441 var = ngx_http_add_variable(cf, &v->name, v->flags);
1442 if (var == NULL) {
1443 return NGX_ERROR;
1444 }
1445
Igor Sysoev7bdb7202006-04-19 15:30:56 +00001446 var->get_handler = v->get_handler;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001447 var->data = v->data;
1448 }
1449
1450 return NGX_OK;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001451}
1452
1453
1454static void *
1455ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
1456{
1457 ngx_http_proxy_loc_conf_t *conf;
1458
1459 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
1460 if (conf == NULL) {
1461 return NGX_CONF_ERROR;
1462 }
1463
1464 /*
1465 * set by ngx_pcalloc():
1466 *
1467 * conf->upstream.bufs.num = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001468 * conf->upstream.next_upstream = 0;
1469 * conf->upstream.temp_path = NULL;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001470 * conf->upstream.hide_headers_hash = { NULL, 0 };
1471 * conf->upstream.hide_headers = NULL;
1472 * conf->upstream.pass_headers = NULL;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001473 * conf->upstream.schema = { 0, NULL };
1474 * conf->upstream.uri = { 0, NULL };
1475 * conf->upstream.location = NULL;
1476 *
Igor Sysoev78452232005-10-12 13:50:36 +00001477 * conf->method = NULL;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001478 * conf->headers_source = NULL;
1479 * conf->headers_set_len = NULL;
1480 * conf->headers_set = NULL;
1481 * conf->headers_set_hash = NULL;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001482 * conf->body_set_len = NULL;
1483 * conf->body_set = NULL;
1484 * conf->body_source = { 0, NULL };
Igor Sysoev899b44e2005-05-12 14:58:06 +00001485 * conf->rewrite_locations = NULL;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001486 */
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001487
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001488 conf->upstream.buffering = NGX_CONF_UNSET;
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00001489 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001490
Igor Sysoev02f742b2005-04-08 15:18:55 +00001491 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
1492 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
1493 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
1494
1495 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001496 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001497
1498 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001499 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001500 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001501
Igor Sysoev899b44e2005-05-12 14:58:06 +00001502 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
1503 conf->upstream.pass_request_body = NGX_CONF_UNSET;
Igor Sysoev78452232005-10-12 13:50:36 +00001504
Igor Sysoevef809b82006-06-28 16:00:26 +00001505 conf->upstream.intercept_errors = NGX_CONF_UNSET;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001506
1507 /* "proxy_cyclic_temp_file" is disabled */
1508 conf->upstream.cyclic_temp_file = 0;
1509
Igor Sysoev899b44e2005-05-12 14:58:06 +00001510 conf->redirect = NGX_CONF_UNSET;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001511 conf->upstream.change_buffering = 1;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001512
1513 return conf;
1514}
1515
1516
1517static char *
1518ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
1519{
1520 ngx_http_proxy_loc_conf_t *prev = parent;
1521 ngx_http_proxy_loc_conf_t *conf = child;
1522
Igor Sysoev899b44e2005-05-12 14:58:06 +00001523 u_char *p;
1524 size_t size;
1525 uintptr_t *code;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001526 ngx_str_t *header;
1527 ngx_uint_t i, j;
1528 ngx_array_t hide_headers;
1529 ngx_keyval_t *src, *s, *h;
1530 ngx_hash_key_t *hk;
1531 ngx_hash_init_t hash;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001532 ngx_http_proxy_redirect_t *pr;
1533 ngx_http_script_compile_t sc;
1534 ngx_http_script_copy_code_t *copy;
1535
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001536 ngx_conf_merge_value(conf->upstream.buffering,
1537 prev->upstream.buffering, 1);
1538
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00001539 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
1540 prev->upstream.ignore_client_abort, 0);
1541
Igor Sysoev02f742b2005-04-08 15:18:55 +00001542 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
1543 prev->upstream.connect_timeout, 60000);
1544
1545 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
1546 prev->upstream.send_timeout, 60000);
1547
1548 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
1549 prev->upstream.read_timeout, 60000);
1550
1551 ngx_conf_merge_size_value(conf->upstream.send_lowat,
1552 prev->upstream.send_lowat, 0);
1553
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001554 ngx_conf_merge_size_value(conf->upstream.buffer_size,
1555 prev->upstream.buffer_size,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001556 (size_t) ngx_pagesize);
1557
1558 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
1559 8, ngx_pagesize);
1560
1561 if (conf->upstream.bufs.num < 2) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001562 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001563 "there must be at least 2 \"proxy_buffers\"");
1564 return NGX_CONF_ERROR;
1565 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001566
Igor Sysoev02f742b2005-04-08 15:18:55 +00001567
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001568 size = conf->upstream.buffer_size;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001569 if (size < conf->upstream.bufs.size) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001570 size = conf->upstream.bufs.size;
1571 }
1572
1573
Igor Sysoev187b7d92005-07-14 12:51:53 +00001574 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
1575 prev->upstream.busy_buffers_size_conf,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001576 NGX_CONF_UNSET_SIZE);
1577
Igor Sysoev187b7d92005-07-14 12:51:53 +00001578 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001579 conf->upstream.busy_buffers_size = 2 * size;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001580 } else {
1581 conf->upstream.busy_buffers_size =
1582 conf->upstream.busy_buffers_size_conf;
1583 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001584
Igor Sysoev187b7d92005-07-14 12:51:53 +00001585 if (conf->upstream.busy_buffers_size < size) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001586 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1587 "\"proxy_busy_buffers_size\" must be equal or bigger than "
1588 "maximum of the value of \"proxy_header_buffer_size\" and "
1589 "one of the \"proxy_buffers\"");
1590
1591 return NGX_CONF_ERROR;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001592 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001593
Igor Sysoev187b7d92005-07-14 12:51:53 +00001594 if (conf->upstream.busy_buffers_size
1595 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
Igor Sysoev02f742b2005-04-08 15:18:55 +00001596 {
1597 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1598 "\"proxy_busy_buffers_size\" must be less than "
1599 "the size of all \"proxy_buffers\" minus one buffer");
1600
1601 return NGX_CONF_ERROR;
1602 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001603
Igor Sysoev187b7d92005-07-14 12:51:53 +00001604
1605 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
1606 prev->upstream.temp_file_write_size_conf,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001607 NGX_CONF_UNSET_SIZE);
1608
Igor Sysoev187b7d92005-07-14 12:51:53 +00001609 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001610 conf->upstream.temp_file_write_size = 2 * size;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001611 } else {
1612 conf->upstream.temp_file_write_size =
1613 conf->upstream.temp_file_write_size_conf;
1614 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001615
Igor Sysoev187b7d92005-07-14 12:51:53 +00001616 if (conf->upstream.temp_file_write_size < size) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001617 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1618 "\"proxy_temp_file_write_size\" must be equal or bigger than "
1619 "maximum of the value of \"proxy_header_buffer_size\" and "
1620 "one of the \"proxy_buffers\"");
1621
1622 return NGX_CONF_ERROR;
1623 }
1624
Igor Sysoev187b7d92005-07-14 12:51:53 +00001625 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
1626 prev->upstream.max_temp_file_size_conf,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001627 NGX_CONF_UNSET_SIZE);
1628
Igor Sysoev187b7d92005-07-14 12:51:53 +00001629 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001630 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001631 } else {
1632 conf->upstream.max_temp_file_size =
1633 conf->upstream.max_temp_file_size_conf;
1634 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001635
Igor Sysoev187b7d92005-07-14 12:51:53 +00001636 if (conf->upstream.max_temp_file_size != 0
1637 && conf->upstream.max_temp_file_size < size)
Igor Sysoev02f742b2005-04-08 15:18:55 +00001638 {
1639 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev78452232005-10-12 13:50:36 +00001640 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
Igor Sysoev02f742b2005-04-08 15:18:55 +00001641 "the temporary files usage or must be equal or bigger than "
Igor Sysoev78452232005-10-12 13:50:36 +00001642 "maximum of the value of \"proxy_header_buffer_size\" and "
1643 "one of the \"proxy_buffers\"");
Igor Sysoev02f742b2005-04-08 15:18:55 +00001644
1645 return NGX_CONF_ERROR;
1646 }
1647
Igor Sysoev187b7d92005-07-14 12:51:53 +00001648
Igor Sysoev02f742b2005-04-08 15:18:55 +00001649 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
1650 prev->upstream.next_upstream,
1651 (NGX_CONF_BITMASK_SET
1652 |NGX_HTTP_UPSTREAM_FT_ERROR
1653 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
1654
Igor Sysoevbb28b6d2006-07-11 13:20:19 +00001655 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
1656 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
1657 |NGX_HTTP_UPSTREAM_FT_OFF;
1658 }
1659
Igor Sysoev899b44e2005-05-12 14:58:06 +00001660 ngx_conf_merge_path_value(conf->upstream.temp_path,
1661 prev->upstream.temp_path,
1662 NGX_HTTP_PROXY_TEMP_PATH, 1, 2, 0,
1663 ngx_garbage_collector_temp_handler, cf);
Igor Sysoev02f742b2005-04-08 15:18:55 +00001664
Igor Sysoev78452232005-10-12 13:50:36 +00001665 if (conf->method.len == 0) {
1666 conf->method = prev->method;
1667
1668 } else {
1669 conf->method.data[conf->method.len] = ' ';
1670 conf->method.len++;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001671 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001672
Igor Sysoev899b44e2005-05-12 14:58:06 +00001673 ngx_conf_merge_value(conf->upstream.pass_request_headers,
1674 prev->upstream.pass_request_headers, 1);
1675 ngx_conf_merge_value(conf->upstream.pass_request_body,
1676 prev->upstream.pass_request_body, 1);
1677
Igor Sysoevef809b82006-06-28 16:00:26 +00001678 ngx_conf_merge_value(conf->upstream.intercept_errors,
1679 prev->upstream.intercept_errors, 0);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001680
Igor Sysoev899b44e2005-05-12 14:58:06 +00001681 ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
1682
1683 if (conf->redirect) {
1684
1685 if (conf->redirects == NULL) {
1686 conf->redirects = prev->redirects;
1687 }
1688
1689 if (conf->redirects == NULL && conf->upstream.url.data) {
1690
1691 conf->redirects = ngx_array_create(cf->pool, 1,
1692 sizeof(ngx_http_proxy_redirect_t));
1693 if (conf->redirects == NULL) {
1694 return NGX_CONF_ERROR;
1695 }
1696
1697 pr = ngx_array_push(conf->redirects);
1698 if (pr == NULL) {
1699 return NGX_CONF_ERROR;
1700 }
1701
1702 pr->handler = ngx_http_proxy_rewrite_redirect_text;
1703 pr->redirect = conf->upstream.url;
Igor Sysoevafd7ec52006-05-29 17:28:12 +00001704
1705 if (conf->upstream.uri.len) {
1706 pr->replacement.text = conf->upstream.location;
1707
1708 } else {
1709 pr->replacement.text.len = 0;
1710 pr->replacement.text.data = NULL;
1711 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00001712 }
1713 }
1714
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001715 if (conf->upstream.hide_headers == NULL
1716 && conf->upstream.pass_headers == NULL)
1717 {
1718 conf->upstream.hide_headers = prev->upstream.hide_headers;
1719 conf->upstream.pass_headers = prev->upstream.pass_headers;
1720 conf->upstream.hide_headers_hash = prev->upstream.hide_headers_hash;
1721
1722 if (conf->upstream.hide_headers_hash.buckets) {
1723 goto peers;
1724 }
1725
1726 } else {
1727 if (conf->upstream.hide_headers == NULL) {
1728 conf->upstream.hide_headers = prev->upstream.hide_headers;
1729 }
1730
1731 if (conf->upstream.pass_headers == NULL) {
1732 conf->upstream.pass_headers = prev->upstream.pass_headers;
1733 }
1734 }
1735
1736 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
1737 != NGX_OK)
1738 {
1739 return NGX_CONF_ERROR;
1740 }
1741
1742 for (header = ngx_http_proxy_hide_headers; header->len; header++) {
1743 hk = ngx_array_push(&hide_headers);
1744 if (hk == NULL) {
1745 return NGX_CONF_ERROR;
1746 }
1747
1748 hk->key = *header;
1749 hk->key_hash = ngx_hash_key_lc(header->data, header->len);
1750 hk->value = (void *) 1;
1751 }
1752
1753 if (conf->upstream.hide_headers) {
1754
1755 header = conf->upstream.hide_headers->elts;
1756
1757 for (i = 0; i < conf->upstream.hide_headers->nelts; i++) {
1758
1759 hk = hide_headers.elts;
1760
1761 for (j = 0; j < hide_headers.nelts; j++) {
1762 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) {
1763 goto exist;
1764 }
1765 }
1766
1767 hk = ngx_array_push(&hide_headers);
1768 if (hk == NULL) {
1769 return NGX_CONF_ERROR;
1770 }
1771
1772 hk->key = header[i];
1773 hk->key_hash = ngx_hash_key_lc(header[i].data, header[i].len);
1774 hk->value = (void *) 1;
1775
1776 exist:
1777
1778 continue;
1779 }
1780 }
1781
1782 if (conf->upstream.pass_headers) {
1783
1784 hk = hide_headers.elts;
1785 header = conf->upstream.pass_headers->elts;
1786
1787 for (i = 0; i < conf->upstream.pass_headers->nelts; i++) {
1788 for (j = 0; j < hide_headers.nelts; j++) {
1789
1790 if (hk[j].key.data == NULL) {
1791 continue;
1792 }
1793
1794 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) {
1795 hk[j].key.data = NULL;
1796 break;
1797 }
1798 }
1799 }
1800 }
1801
1802 hash.hash = &conf->upstream.hide_headers_hash;
1803 hash.key = ngx_hash_key_lc;
1804 hash.max_size = 512;
Igor Sysoevaec57e22006-10-09 14:03:16 +00001805 hash.bucket_size = ngx_align(64, ngx_cacheline_size);
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001806 hash.name = "proxy_hide_headers_hash";
1807 hash.pool = cf->pool;
1808 hash.temp_pool = NULL;
1809
1810 if (ngx_hash_init(&hash, hide_headers.elts, hide_headers.nelts) != NGX_OK) {
1811 return NGX_CONF_ERROR;
1812 }
1813
1814peers:
Igor Sysoev02f742b2005-04-08 15:18:55 +00001815
Igor Sysoev3d2fd182006-12-04 16:46:13 +00001816 if (conf->upstream.upstream == NULL) {
1817 conf->upstream.upstream = prev->upstream.upstream;
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00001818
1819 conf->host_header = prev->host_header;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00001820 conf->port = prev->port;
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00001821 conf->upstream.schema = prev->upstream.schema;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001822 }
1823
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001824
1825 if (conf->body_source.data == NULL) {
1826 conf->body_source = prev->body_source;
1827 conf->body_set_len = prev->body_set_len;
1828 conf->body_set = prev->body_set;
1829 }
1830
Igor Sysoevb141f2b2005-11-15 14:49:57 +00001831 if (conf->body_source.data && conf->body_set_len == NULL) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001832
1833 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1834
1835 sc.cf = cf;
1836 sc.source = &conf->body_source;
1837 sc.flushes = &conf->flushes;
1838 sc.lengths = &conf->body_set_len;
1839 sc.values = &conf->body_set;
1840 sc.complete_lengths = 1;
1841 sc.complete_values = 1;
1842
1843 if (ngx_http_script_compile(&sc) != NGX_OK) {
1844 return NGX_CONF_ERROR;
1845 }
1846
1847 if (conf->headers_source == NULL) {
1848 conf->headers_source = ngx_array_create(cf->pool, 4,
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001849 sizeof(ngx_keyval_t));
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001850 if (conf->headers_source == NULL) {
1851 return NGX_CONF_ERROR;
1852 }
1853 }
1854
1855 s = ngx_array_push(conf->headers_source);
1856 if (s == NULL) {
1857 return NGX_CONF_ERROR;
1858 }
1859
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001860 s->key.len = sizeof("Content-Length") - 1;
1861 s->key.data = (u_char *) "Content-Length";
1862 s->value.len = sizeof("$proxy_internal_body_length") - 1;
1863 s->value.data = (u_char *) "$proxy_internal_body_length";
1864 }
1865
1866
Igor Sysoev899b44e2005-05-12 14:58:06 +00001867 if (conf->headers_source == NULL) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00001868 conf->flushes = prev->flushes;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001869 conf->headers_set_len = prev->headers_set_len;
1870 conf->headers_set = prev->headers_set;
1871 conf->headers_set_hash = prev->headers_set_hash;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001872 conf->headers_source = prev->headers_source;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001873 }
1874
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001875 if (conf->headers_set_hash.buckets) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00001876 return NGX_CONF_OK;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001877 }
1878
Igor Sysoev899b44e2005-05-12 14:58:06 +00001879
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001880 conf->headers_names = ngx_array_create(cf->pool, 4, sizeof(ngx_hash_key_t));
Igor Sysoev899b44e2005-05-12 14:58:06 +00001881 if (conf->headers_names == NULL) {
1882 return NGX_CONF_ERROR;
1883 }
1884
1885 if (conf->headers_source == NULL) {
1886 conf->headers_source = ngx_array_create(cf->pool, 4,
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001887 sizeof(ngx_keyval_t));
Igor Sysoev899b44e2005-05-12 14:58:06 +00001888 if (conf->headers_source == NULL) {
1889 return NGX_CONF_ERROR;
1890 }
1891 }
1892
1893 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
1894 if (conf->headers_set_len == NULL) {
1895 return NGX_CONF_ERROR;
1896 }
1897
1898 conf->headers_set = ngx_array_create(cf->pool, 512, 1);
1899 if (conf->headers_set == NULL) {
1900 return NGX_CONF_ERROR;
1901 }
1902
1903
1904 src = conf->headers_source->elts;
1905
1906 for (h = ngx_http_proxy_headers; h->key.len; h++) {
1907
1908 for (i = 0; i < conf->headers_source->nelts; i++) {
1909 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
1910 goto next;
1911 }
1912 }
1913
1914 s = ngx_array_push(conf->headers_source);
1915 if (s == NULL) {
1916 return NGX_CONF_ERROR;
1917 }
1918
1919 *s = *h;
1920
Igor Sysoev4959ec42005-05-23 12:07:45 +00001921 src = conf->headers_source->elts;
1922
Igor Sysoev899b44e2005-05-12 14:58:06 +00001923 next:
1924
1925 continue;
1926 }
1927
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001928
1929 src = conf->headers_source->elts;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001930 for (i = 0; i < conf->headers_source->nelts; i++) {
1931
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001932 hk = ngx_array_push(conf->headers_names);
1933 if (hk == NULL) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00001934 return NGX_CONF_ERROR;
1935 }
1936
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001937 hk->key = src[i].key;
1938 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
1939 hk->value = (void *) 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001940
Igor Sysoev403d2442005-05-26 18:12:40 +00001941 if (src[i].value.len == 0) {
1942 continue;
1943 }
1944
Igor Sysoev899b44e2005-05-12 14:58:06 +00001945 if (ngx_http_script_variables_count(&src[i].value) == 0) {
1946 copy = ngx_array_push_n(conf->headers_set_len,
1947 sizeof(ngx_http_script_copy_code_t));
1948 if (copy == NULL) {
1949 return NGX_CONF_ERROR;
1950 }
1951
1952 copy->code = (ngx_http_script_code_pt)
1953 ngx_http_script_copy_len_code;
1954 copy->len = src[i].key.len + sizeof(": ") - 1
1955 + src[i].value.len + sizeof(CRLF) - 1;
1956
1957
1958 size = (sizeof(ngx_http_script_copy_code_t)
1959 + src[i].key.len + sizeof(": ") - 1
1960 + src[i].value.len + sizeof(CRLF) - 1
1961 + sizeof(uintptr_t) - 1)
1962 & ~(sizeof(uintptr_t) - 1);
1963
1964 copy = ngx_array_push_n(conf->headers_set, size);
1965 if (copy == NULL) {
1966 return NGX_CONF_ERROR;
1967 }
1968
1969 copy->code = ngx_http_script_copy_code;
1970 copy->len = src[i].key.len + sizeof(": ") - 1
1971 + src[i].value.len + sizeof(CRLF) - 1;
1972
1973 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
1974
1975 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
1976 *p++ = ':'; *p++ = ' ';
1977 p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
1978 *p++ = CR; *p = LF;
1979
1980 } else {
1981 copy = ngx_array_push_n(conf->headers_set_len,
1982 sizeof(ngx_http_script_copy_code_t));
1983 if (copy == NULL) {
1984 return NGX_CONF_ERROR;
1985 }
1986
1987 copy->code = (ngx_http_script_code_pt)
1988 ngx_http_script_copy_len_code;
1989 copy->len = src[i].key.len + sizeof(": ") - 1;
1990
1991
1992 size = (sizeof(ngx_http_script_copy_code_t)
1993 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
1994 & ~(sizeof(uintptr_t) - 1);
1995
1996 copy = ngx_array_push_n(conf->headers_set, size);
1997 if (copy == NULL) {
1998 return NGX_CONF_ERROR;
1999 }
2000
2001 copy->code = ngx_http_script_copy_code;
2002 copy->len = src[i].key.len + sizeof(": ") - 1;
2003
2004 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
2005 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
2006 *p++ = ':'; *p = ' ';
2007
2008
2009 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2010
2011 sc.cf = cf;
2012 sc.source = &src[i].value;
Igor Sysoev09c684b2005-11-09 17:25:55 +00002013 sc.flushes = &conf->flushes;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002014 sc.lengths = &conf->headers_set_len;
2015 sc.values = &conf->headers_set;
2016
2017 if (ngx_http_script_compile(&sc) != NGX_OK) {
2018 return NGX_CONF_ERROR;
2019 }
2020
2021
2022 copy = ngx_array_push_n(conf->headers_set_len,
2023 sizeof(ngx_http_script_copy_code_t));
2024 if (copy == NULL) {
2025 return NGX_CONF_ERROR;
2026 }
2027
2028 copy->code = (ngx_http_script_code_pt)
2029 ngx_http_script_copy_len_code;
2030 copy->len = sizeof(CRLF) - 1;
2031
2032
2033 size = (sizeof(ngx_http_script_copy_code_t)
2034 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
2035 & ~(sizeof(uintptr_t) - 1);
2036
2037 copy = ngx_array_push_n(conf->headers_set, size);
2038 if (copy == NULL) {
2039 return NGX_CONF_ERROR;
2040 }
2041
2042 copy->code = ngx_http_script_copy_code;
2043 copy->len = sizeof(CRLF) - 1;
2044
2045 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
2046 *p++ = CR; *p = LF;
2047 }
2048
2049 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
2050 if (code == NULL) {
2051 return NGX_CONF_ERROR;
2052 }
2053
2054 *code = (uintptr_t) NULL;
2055
2056 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
2057 if (code == NULL) {
2058 return NGX_CONF_ERROR;
2059 }
2060
2061 *code = (uintptr_t) NULL;
2062 }
2063
2064 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
2065 if (code == NULL) {
2066 return NGX_CONF_ERROR;
2067 }
2068
2069 *code = (uintptr_t) NULL;
2070
2071
Igor Sysoev3338cfd2006-05-11 14:43:47 +00002072 hash.hash = &conf->headers_set_hash;
2073 hash.key = ngx_hash_key_lc;
2074 hash.max_size = 512;
2075 hash.bucket_size = ngx_cacheline_size;
2076 hash.name = "proxy_set_header_hash";
2077 hash.pool = cf->pool;
2078 hash.temp_pool = NULL;
2079
2080 if (ngx_hash_init(&hash, conf->headers_names->elts,
2081 conf->headers_names->nelts)
2082 != NGX_OK)
2083 {
2084 return NGX_CONF_ERROR;
2085 }
2086
Igor Sysoev02f742b2005-04-08 15:18:55 +00002087 return NGX_CONF_OK;
2088}
2089
2090
Igor Sysoev02f742b2005-04-08 15:18:55 +00002091static char *
2092ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2093{
Igor Sysoev899b44e2005-05-12 14:58:06 +00002094 ngx_http_proxy_loc_conf_t *plcf = conf;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002095
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00002096 u_char *p;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002097 size_t add;
Igor Sysoev305a9d82005-12-26 17:07:48 +00002098 u_short port;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002099 ngx_str_t *value, *url;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002100 ngx_url_t u;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002101 ngx_http_core_loc_conf_t *clcf;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002102#if (NGX_HTTP_SSL)
2103 ngx_pool_cleanup_t *cln;
2104#endif
Igor Sysoev02f742b2005-04-08 15:18:55 +00002105
Igor Sysoeva2573672005-10-05 14:46:21 +00002106 if (plcf->upstream.schema.len) {
2107 return "is duplicate";
2108 }
2109
Igor Sysoev02f742b2005-04-08 15:18:55 +00002110 value = cf->args->elts;
2111
2112 url = &value[1];
2113
Igor Sysoev722231f2007-02-14 18:51:19 +00002114 if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002115 add = 7;
Igor Sysoev43f279d2005-12-18 16:02:44 +00002116 port = 80;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002117
Igor Sysoev722231f2007-02-14 18:51:19 +00002118 } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002119
2120#if (NGX_HTTP_SSL)
2121
2122 add = 8;
Igor Sysoev43f279d2005-12-18 16:02:44 +00002123 port = 443;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002124
2125 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
2126 if (plcf->upstream.ssl == NULL) {
2127 return NGX_CONF_ERROR;
2128 }
2129
2130 plcf->upstream.ssl->log = cf->log;
2131
2132 if (ngx_ssl_create(plcf->upstream.ssl,
Igor Sysoevebf2bbc2007-01-02 23:37:25 +00002133 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1, NULL)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002134 != NGX_OK)
2135 {
2136 return NGX_CONF_ERROR;
2137 }
2138
2139 cln = ngx_pool_cleanup_add(cf->pool, 0);
2140 if (cln == NULL) {
2141 return NGX_CONF_ERROR;
2142 }
2143
2144 cln->handler = ngx_ssl_cleanup_ctx;
2145 cln->data = plcf->upstream.ssl;
2146
2147#else
2148 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2149 "https protocol requires SSL support");
2150 return NGX_CONF_ERROR;
2151#endif
2152
2153 } else {
Igor Sysoev02f742b2005-04-08 15:18:55 +00002154 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
2155 return NGX_CONF_ERROR;
2156 }
2157
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002158 ngx_memzero(&u, sizeof(ngx_url_t));
Igor Sysoev02f742b2005-04-08 15:18:55 +00002159
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002160 u.url.len = url->len - add;
2161 u.url.data = url->data + add;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00002162 u.default_port = port;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002163 u.uri_part = 1;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00002164 u.no_resolve = 1;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002165
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002166 plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
2167 if (plcf->upstream.upstream == NULL) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00002168 return NGX_CONF_ERROR;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002169 }
2170
Igor Sysoevbf3aaac2006-12-12 16:46:16 +00002171 if (!u.unix_socket) {
2172 if (u.no_port || u.port == port) {
2173 plcf->host_header = u.host;
2174
2175 if (port == 80) {
2176 plcf->port.len = sizeof("80") - 1;
2177 plcf->port.data = (u_char *) "80";
2178 } else {
2179 plcf->port.len = sizeof("443") - 1;
2180 plcf->port.data = (u_char *) "443";
2181 }
2182
2183 } else {
2184 p = ngx_palloc(cf->pool, u.host.len + sizeof(":65536") - 1);
2185 if (p == NULL) {
2186 return NGX_CONF_ERROR;
2187 }
2188
2189 plcf->host_header.len = ngx_sprintf(p, "%V:%d", &u.host, u.port)
2190 - p;
2191 plcf->host_header.data = p;
2192
2193 plcf->port.len = plcf->host_header.len - u.host.len - 1;
2194 plcf->port.data = p + u.host.len + 1;
2195 }
2196
2197
2198 } else {
2199 plcf->host_header.len = sizeof("localhost") - 1;
2200 plcf->host_header.data = (u_char *) "localhost";
2201 plcf->port.len = 0;
2202 plcf->port.data = (u_char *) "";
2203 }
2204
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002205 plcf->upstream.uri = u.uri;
2206
Igor Sysoev43f279d2005-12-18 16:02:44 +00002207 plcf->upstream.schema.len = add;
2208 plcf->upstream.schema.data = url->data;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002209
Igor Sysoev02f742b2005-04-08 15:18:55 +00002210 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
2211
2212 clcf->handler = ngx_http_proxy_handler;
2213
Igor Sysoev09c684b2005-11-09 17:25:55 +00002214 plcf->upstream.location = clcf->name;
Igor Sysoeva2573672005-10-05 14:46:21 +00002215
2216#if (NGX_PCRE)
2217
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00002218 if (clcf->regex || clcf->noname) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00002219 if (plcf->upstream.uri.len) {
2220 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2221 "\"proxy_pass\" may not have URI part in "
Igor Sysoeve927b1e2006-11-14 12:43:14 +00002222 "location given by regular expression, "
2223 "or inside the \"if\" statement, "
2224 "or inside the \"limit_except\" block");
Igor Sysoev09c684b2005-11-09 17:25:55 +00002225 return NGX_CONF_ERROR;
2226 }
2227
2228 plcf->upstream.location.len = 0;
Igor Sysoeva2573672005-10-05 14:46:21 +00002229 }
2230
Igor Sysoev02f742b2005-04-08 15:18:55 +00002231#endif
2232
Igor Sysoev899b44e2005-05-12 14:58:06 +00002233 plcf->upstream.url = *url;
2234
Igor Sysoev02f742b2005-04-08 15:18:55 +00002235 if (clcf->name.data[clcf->name.len - 1] == '/') {
2236 clcf->auto_redirect = 1;
2237 }
2238
2239 return NGX_CONF_OK;
2240}
2241
2242
2243static char *
Igor Sysoev899b44e2005-05-12 14:58:06 +00002244ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev02f742b2005-04-08 15:18:55 +00002245{
Igor Sysoev899b44e2005-05-12 14:58:06 +00002246 ngx_http_proxy_loc_conf_t *plcf = conf;
2247
2248 ngx_str_t *value;
2249 ngx_array_t *vars_lengths, *vars_values;
2250 ngx_http_script_compile_t sc;
2251 ngx_http_proxy_redirect_t *pr;
2252
2253 if (plcf->redirect == 0) {
2254 return NGX_CONF_OK;
2255 }
2256
2257 value = cf->args->elts;
2258
2259 if (ngx_strcmp(value[1].data, "off") == 0) {
2260 plcf->redirect = 0;
2261 plcf->redirects = NULL;
2262 return NGX_CONF_OK;
2263 }
2264
2265 if (plcf->redirects == NULL) {
2266 plcf->redirects = ngx_array_create(cf->pool, 1,
2267 sizeof(ngx_http_proxy_redirect_t));
2268 if (plcf->redirects == NULL) {
2269 return NGX_CONF_ERROR;
2270 }
2271 }
2272
2273 pr = ngx_array_push(plcf->redirects);
2274 if (pr == NULL) {
2275 return NGX_CONF_ERROR;
2276 }
2277
2278 if (cf->args->nelts == 2 && ngx_strcmp(value[1].data, "default") == 0) {
2279 if (plcf->upstream.url.data == NULL) {
2280 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2281 "\"proxy_rewrite_location default\" must go "
2282 "after the \"proxy_pass\" directive");
2283 return NGX_CONF_ERROR;
2284 }
2285
2286 pr->handler = ngx_http_proxy_rewrite_redirect_text;
2287 pr->redirect = plcf->upstream.url;
Igor Sysoevafd7ec52006-05-29 17:28:12 +00002288
2289 if (plcf->upstream.uri.len) {
2290 pr->replacement.text = plcf->upstream.location;
2291
2292 } else {
2293 pr->replacement.text.len = 0;
2294 pr->replacement.text.data = NULL;
2295 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00002296
2297 return NGX_CONF_OK;
2298 }
2299
2300 if (ngx_http_script_variables_count(&value[2]) == 0) {
2301 pr->handler = ngx_http_proxy_rewrite_redirect_text;
2302 pr->redirect = value[1];
2303 pr->replacement.text = value[2];
2304
2305 return NGX_CONF_OK;
2306 }
2307
2308 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2309
2310 vars_lengths = NULL;
2311 vars_values = NULL;
2312
2313 sc.cf = cf;
2314 sc.source = &value[2];
2315 sc.lengths = &vars_lengths;
2316 sc.values = &vars_values;
2317 sc.complete_lengths = 1;
2318 sc.complete_values = 1;
2319
2320 if (ngx_http_script_compile(&sc) != NGX_OK) {
2321 return NGX_CONF_ERROR;
2322 }
2323
2324 pr->handler = ngx_http_proxy_rewrite_redirect_vars;
2325 pr->redirect = value[1];
2326 pr->replacement.vars.lengths = vars_lengths->elts;
2327 pr->replacement.vars.values = vars_values->elts;
2328
Igor Sysoev02f742b2005-04-08 15:18:55 +00002329 return NGX_CONF_OK;
2330}
2331
2332
2333static char *
2334ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
2335{
2336#if (NGX_FREEBSD)
2337 ssize_t *np = data;
2338
Igor Sysoev08e63d42006-08-14 15:09:38 +00002339 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00002340 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2341 "\"proxy_send_lowat\" must be less than %d "
2342 "(sysctl net.inet.tcp.sendspace)",
2343 ngx_freebsd_net_inet_tcp_sendspace);
2344
2345 return NGX_CONF_ERROR;
2346 }
2347
2348#elif !(NGX_HAVE_SO_SNDLOWAT)
2349 ssize_t *np = data;
2350
2351 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
2352 "\"proxy_send_lowat\" is not supported, ignored");
2353
2354 *np = 0;
2355
2356#endif
2357
2358 return NGX_CONF_OK;
2359}
Igor Sysoev3d2fd182006-12-04 16:46:13 +00002360
2361
2362static char *
2363ngx_http_proxy_upstream_max_fails_unsupported(ngx_conf_t *cf,
2364 ngx_command_t *cmd, void *conf)
2365{
2366 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2367 "\"proxy_upstream_max_fails\" is not supported, "
2368 "use the \"max_fails\" parameter of the \"server\" directive ",
2369 "inside the \"upstream\" block");
2370
2371 return NGX_CONF_ERROR;
2372}
2373
2374
2375static char *
2376ngx_http_proxy_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
2377 ngx_command_t *cmd, void *conf)
2378{
2379 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2380 "\"proxy_upstream_fail_timeout\" is not supported, "
2381 "use the \"fail_timeout\" parameter of the \"server\" directive ",
2382 "inside the \"upstream\" block");
2383
2384 return NGX_CONF_ERROR;
2385}