blob: 31a4355e50943245736f265e44d94619483af93a [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_http_upstream_srv_conf_t *upstream_peers;
Igor Sysoev02f742b2005-04-08 15:18:55 +000039
Igor Sysoev6f134cc2006-05-23 14:54:58 +000040 ngx_array_t *flushes;
41 ngx_array_t *body_set_len;
42 ngx_array_t *body_set;
43 ngx_array_t *headers_set_len;
44 ngx_array_t *headers_set;
45 ngx_hash_t headers_set_hash;
Igor Sysoev02f742b2005-04-08 15:18:55 +000046
Igor Sysoev6f134cc2006-05-23 14:54:58 +000047 ngx_array_t *headers_source;
48 ngx_array_t *headers_names;
Igor Sysoev02f742b2005-04-08 15:18:55 +000049
Igor Sysoev6f134cc2006-05-23 14:54:58 +000050 ngx_array_t *redirects;
Igor Sysoev02f742b2005-04-08 15:18:55 +000051
Igor Sysoev6f134cc2006-05-23 14:54:58 +000052 ngx_str_t body_source;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000053
Igor Sysoev6f134cc2006-05-23 14:54:58 +000054 ngx_str_t method;
55 ngx_str_t host_header;
56 ngx_str_t port_text;
Igor Sysoev02f742b2005-04-08 15:18:55 +000057
Igor Sysoev6f134cc2006-05-23 14:54:58 +000058 ngx_flag_t redirect;
Igor Sysoev02f742b2005-04-08 15:18:55 +000059} ngx_http_proxy_loc_conf_t;
60
61
62typedef struct {
Igor Sysoev6f134cc2006-05-23 14:54:58 +000063 ngx_uint_t status;
64 ngx_uint_t status_count;
65 u_char *status_start;
66 u_char *status_end;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000067
Igor Sysoev6f134cc2006-05-23 14:54:58 +000068 size_t internal_body_length;
Igor Sysoev899b44e2005-05-12 14:58:06 +000069} ngx_http_proxy_ctx_t;
Igor Sysoev02f742b2005-04-08 15:18:55 +000070
Igor Sysoev02f742b2005-04-08 15:18:55 +000071
Igor Sysoev899b44e2005-05-12 14:58:06 +000072#define NGX_HTTP_PROXY_PARSE_NO_HEADER 20
Igor Sysoev02f742b2005-04-08 15:18:55 +000073
74
75static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
76static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r);
Igor Sysoev899b44e2005-05-12 14:58:06 +000077static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r);
78static ngx_int_t ngx_http_proxy_parse_status_line(ngx_http_request_t *r,
79 ngx_http_proxy_ctx_t *p);
Igor Sysoev02f742b2005-04-08 15:18:55 +000080static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r);
Igor Sysoev02f742b2005-04-08 15:18:55 +000081static void ngx_http_proxy_abort_request(ngx_http_request_t *r);
82static void ngx_http_proxy_finalize_request(ngx_http_request_t *r,
83 ngx_int_t rc);
84
Igor Sysoev09c684b2005-11-09 17:25:55 +000085static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r,
86 ngx_http_variable_value_t *v, uintptr_t data);
87static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r,
88 ngx_http_variable_value_t *v, uintptr_t data);
89static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +000090 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
Igor Sysoev09c684b2005-11-09 17:25:55 +000091 ngx_http_variable_value_t *v, uintptr_t data);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +000092static ngx_int_t
93 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
94 ngx_http_variable_value_t *v, uintptr_t data);
Igor Sysoev899b44e2005-05-12 14:58:06 +000095static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
96 ngx_table_elt_t *h, size_t prefix);
Igor Sysoev02f742b2005-04-08 15:18:55 +000097
Igor Sysoev899b44e2005-05-12 14:58:06 +000098static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
Igor Sysoev02f742b2005-04-08 15:18:55 +000099static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
100static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
101 void *parent, void *child);
102
103static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
104 void *conf);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000105static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000106 void *conf);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000107
Igor Sysoev02f742b2005-04-08 15:18:55 +0000108static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
109
Igor Sysoev899b44e2005-05-12 14:58:06 +0000110
Igor Sysoev02f742b2005-04-08 15:18:55 +0000111static ngx_conf_post_t ngx_http_proxy_lowat_post =
Igor Sysoev899b44e2005-05-12 14:58:06 +0000112 { ngx_http_proxy_lowat_check };
113
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000114static ngx_conf_deprecated_t ngx_conf_deprecated_proxy_header_buffer_size = {
115 ngx_conf_deprecated, "proxy_header_buffer_size", "proxy_buffer_size"
116};
117
Igor Sysoev02f742b2005-04-08 15:18:55 +0000118
119static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = {
120 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
121 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
122 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
123 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
Igor Sysoev8fea8852006-03-15 09:53:04 +0000124 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
Igor Sysoev02f742b2005-04-08 15:18:55 +0000125 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
126 { ngx_null_string, 0 }
127};
128
129
130static ngx_command_t ngx_http_proxy_commands[] = {
131
132 { ngx_string("proxy_pass"),
Igor Sysoev6d16e1e2006-04-05 13:40:54 +0000133 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_CONF_TAKE1,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000134 ngx_http_proxy_pass,
135 NGX_HTTP_LOC_CONF_OFFSET,
136 0,
137 NULL },
138
Igor Sysoev899b44e2005-05-12 14:58:06 +0000139 { ngx_string("proxy_redirect"),
140 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
141 ngx_http_proxy_redirect,
142 NGX_HTTP_LOC_CONF_OFFSET,
143 0,
144 NULL },
145
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000146 { ngx_string("proxy_buffering"),
147 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
148 ngx_conf_set_flag_slot,
149 NGX_HTTP_LOC_CONF_OFFSET,
150 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffering),
151 NULL },
152
Igor Sysoev6d16e1e2006-04-05 13:40:54 +0000153 { ngx_string("proxy_ignore_client_abort"),
154 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
155 ngx_conf_set_flag_slot,
156 NGX_HTTP_LOC_CONF_OFFSET,
157 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort),
158 NULL },
159
Igor Sysoev02f742b2005-04-08 15:18:55 +0000160 { ngx_string("proxy_connect_timeout"),
161 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
162 ngx_conf_set_msec_slot,
163 NGX_HTTP_LOC_CONF_OFFSET,
164 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
165 NULL },
166
167 { ngx_string("proxy_send_timeout"),
168 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
169 ngx_conf_set_msec_slot,
170 NGX_HTTP_LOC_CONF_OFFSET,
171 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
172 NULL },
173
174 { ngx_string("proxy_send_lowat"),
175 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
176 ngx_conf_set_size_slot,
177 NGX_HTTP_LOC_CONF_OFFSET,
178 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
179 &ngx_http_proxy_lowat_post },
180
Igor Sysoev899b44e2005-05-12 14:58:06 +0000181 { ngx_string("proxy_redirect_errors"),
182 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
183 ngx_conf_set_flag_slot,
184 NGX_HTTP_LOC_CONF_OFFSET,
185 offsetof(ngx_http_proxy_loc_conf_t, upstream.redirect_errors),
186 NULL },
187
Igor Sysoev899b44e2005-05-12 14:58:06 +0000188 { ngx_string("proxy_set_header"),
189 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000190 ngx_conf_set_keyval_slot,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000191 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev899b44e2005-05-12 14:58:06 +0000192 offsetof(ngx_http_proxy_loc_conf_t, headers_source),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000193 NULL },
194
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000195 { ngx_string("proxy_set_body"),
196 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
197 ngx_conf_set_str_slot,
198 NGX_HTTP_LOC_CONF_OFFSET,
199 offsetof(ngx_http_proxy_loc_conf_t, body_source),
200 NULL },
201
Igor Sysoev899b44e2005-05-12 14:58:06 +0000202 { ngx_string("proxy_method"),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000203 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
Igor Sysoev78452232005-10-12 13:50:36 +0000204 ngx_conf_set_str_slot,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000205 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev78452232005-10-12 13:50:36 +0000206 offsetof(ngx_http_proxy_loc_conf_t, method),
207 NULL },
Igor Sysoev02f742b2005-04-08 15:18:55 +0000208
Igor Sysoev899b44e2005-05-12 14:58:06 +0000209 { ngx_string("proxy_pass_request_headers"),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000210 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,
Igor Sysoev899b44e2005-05-12 14:58:06 +0000213 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers),
214 NULL },
215
216 { ngx_string("proxy_pass_request_body"),
217 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
218 ngx_conf_set_flag_slot,
219 NGX_HTTP_LOC_CONF_OFFSET,
220 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000221 NULL },
222
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000223 { ngx_string("proxy_buffer_size"),
224 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
225 ngx_conf_set_size_slot,
226 NGX_HTTP_LOC_CONF_OFFSET,
227 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
228 NULL },
229
Igor Sysoev02f742b2005-04-08 15:18:55 +0000230 { ngx_string("proxy_header_buffer_size"),
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,
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000234 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
235 &ngx_conf_deprecated_proxy_header_buffer_size },
Igor Sysoev02f742b2005-04-08 15:18:55 +0000236
237 { ngx_string("proxy_read_timeout"),
238 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
239 ngx_conf_set_msec_slot,
240 NGX_HTTP_LOC_CONF_OFFSET,
241 offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout),
242 NULL },
243
244 { ngx_string("proxy_buffers"),
245 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
246 ngx_conf_set_bufs_slot,
247 NGX_HTTP_LOC_CONF_OFFSET,
248 offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
249 NULL },
250
251 { ngx_string("proxy_busy_buffers_size"),
252 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
253 ngx_conf_set_size_slot,
254 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev187b7d92005-07-14 12:51:53 +0000255 offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000256 NULL },
257
258 { ngx_string("proxy_temp_path"),
259 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
260 ngx_conf_set_path_slot,
261 NGX_HTTP_LOC_CONF_OFFSET,
262 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
263 (void *) ngx_garbage_collector_temp_handler },
264
265 { ngx_string("proxy_max_temp_file_size"),
266 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
267 ngx_conf_set_size_slot,
268 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev187b7d92005-07-14 12:51:53 +0000269 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000270 NULL },
271
272 { ngx_string("proxy_temp_file_write_size"),
273 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
274 ngx_conf_set_size_slot,
275 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev187b7d92005-07-14 12:51:53 +0000276 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000277 NULL },
278
279 { ngx_string("proxy_next_upstream"),
Igor Sysoev9fa5a822005-09-30 14:41:25 +0000280 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000281 ngx_conf_set_bitmask_slot,
282 NGX_HTTP_LOC_CONF_OFFSET,
283 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
284 &ngx_http_proxy_next_upstream_masks },
285
Igor Sysoev78452232005-10-12 13:50:36 +0000286 { ngx_string("proxy_upstream_max_fails"),
287 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
288 ngx_conf_set_num_slot,
289 NGX_HTTP_LOC_CONF_OFFSET,
290 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_fails),
291 NULL },
292
293 { ngx_string("proxy_upstream_fail_timeout"),
294 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
295 ngx_conf_set_sec_slot,
296 NGX_HTTP_LOC_CONF_OFFSET,
297 offsetof(ngx_http_proxy_loc_conf_t, upstream.fail_timeout),
298 NULL },
299
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000300 { ngx_string("proxy_pass_header"),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000301 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000302 ngx_conf_set_str_array_slot,
Igor Sysoev899b44e2005-05-12 14:58:06 +0000303 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000304 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers),
Igor Sysoev899b44e2005-05-12 14:58:06 +0000305 NULL },
306
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000307 { ngx_string("proxy_hide_header"),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000308 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000309 ngx_conf_set_str_array_slot,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000310 NGX_HTTP_LOC_CONF_OFFSET,
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000311 offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers),
Igor Sysoev02f742b2005-04-08 15:18:55 +0000312 NULL },
313
314 ngx_null_command
315};
316
317
318ngx_http_module_t ngx_http_proxy_module_ctx = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000319 ngx_http_proxy_add_variables, /* preconfiguration */
320 NULL, /* postconfiguration */
Igor Sysoev02f742b2005-04-08 15:18:55 +0000321
322 NULL, /* create main configuration */
323 NULL, /* init main configuration */
324
325 NULL, /* create server configuration */
326 NULL, /* merge server configuration */
327
328 ngx_http_proxy_create_loc_conf, /* create location configration */
329 ngx_http_proxy_merge_loc_conf /* merge location configration */
330};
331
332
333ngx_module_t ngx_http_proxy_module = {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000334 NGX_MODULE_V1,
Igor Sysoev02f742b2005-04-08 15:18:55 +0000335 &ngx_http_proxy_module_ctx, /* module context */
336 ngx_http_proxy_commands, /* module directives */
337 NGX_HTTP_MODULE, /* module type */
Igor Sysoeve5733802005-09-08 14:36:09 +0000338 NULL, /* init master */
Igor Sysoev899b44e2005-05-12 14:58:06 +0000339 NULL, /* init module */
Igor Sysoeve5733802005-09-08 14:36:09 +0000340 NULL, /* init process */
341 NULL, /* init thread */
342 NULL, /* exit thread */
343 NULL, /* exit process */
344 NULL, /* exit master */
345 NGX_MODULE_V1_PADDING
Igor Sysoev02f742b2005-04-08 15:18:55 +0000346};
347
348
Igor Sysoev02f742b2005-04-08 15:18:55 +0000349static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
350
Igor Sysoev899b44e2005-05-12 14:58:06 +0000351
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000352static ngx_keyval_t ngx_http_proxy_headers[] = {
353 { ngx_string("Host"), ngx_string("$proxy_host") },
354 { ngx_string("Connection"), ngx_string("close") },
355 { ngx_string("Keep-Alive"), ngx_string("") },
356 { ngx_null_string, ngx_null_string }
357};
358
359
360static ngx_str_t ngx_http_proxy_hide_headers[] = {
361 ngx_string("Date"),
362 ngx_string("Server"),
363 ngx_string("X-Pad"),
364 ngx_string("X-Accel-Expires"),
365 ngx_string("X-Accel-Redirect"),
366 ngx_string("X-Accel-Limit-Rate"),
367 ngx_string("X-Accel-Buffer"),
368 ngx_null_string
Igor Sysoev899b44e2005-05-12 14:58:06 +0000369};
370
371
372static ngx_http_variable_t ngx_http_proxy_vars[] = {
373
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000374 { ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000375 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000376
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000377 { ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0,
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000378 NGX_HTTP_VAR_CHANGABLE|NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000379
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000380 { ngx_string("proxy_add_x_forwarded_for"), NULL,
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000381 ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000382
383#if 0
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000384 { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev899b44e2005-05-12 14:58:06 +0000385#endif
386
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000387 { ngx_string("proxy_internal_body_length"), NULL,
Igor Sysoevd3283ff2005-12-05 13:18:09 +0000388 ngx_http_proxy_internal_body_length_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000389
Igor Sysoev7bdb7202006-04-19 15:30:56 +0000390 { ngx_null_string, NULL, NULL, 0, 0, 0 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000391};
Igor Sysoev02f742b2005-04-08 15:18:55 +0000392
393
Igor Sysoev02f742b2005-04-08 15:18:55 +0000394static ngx_int_t
395ngx_http_proxy_handler(ngx_http_request_t *r)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000396{
Igor Sysoev02f742b2005-04-08 15:18:55 +0000397 ngx_int_t rc;
398 ngx_http_upstream_t *u;
399 ngx_http_proxy_loc_conf_t *plcf;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000400
Igor Sysoev02f742b2005-04-08 15:18:55 +0000401 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000402
Igor Sysoev02f742b2005-04-08 15:18:55 +0000403 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
404 if (u == NULL) {
405 return NGX_HTTP_INTERNAL_SERVER_ERROR;
406 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000407
Igor Sysoev02f742b2005-04-08 15:18:55 +0000408 u->peer.log = r->connection->log;
409 u->peer.log_error = NGX_ERROR_ERR;
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000410 u->peer.peers = plcf->upstream_peers->peers;
411 u->peer.tries = plcf->upstream_peers->peers->number;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000412#if (NGX_THREADS)
413 u->peer.lock = &r->connection->lock;
414#endif
415
416 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
417
418 u->conf = &plcf->upstream;
419
420 u->create_request = ngx_http_proxy_create_request;
421 u->reinit_request = ngx_http_proxy_reinit_request;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000422 u->process_header = ngx_http_proxy_process_status_line;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000423 u->abort_request = ngx_http_proxy_abort_request;
424 u->finalize_request = ngx_http_proxy_finalize_request;
425
Igor Sysoev899b44e2005-05-12 14:58:06 +0000426 if (plcf->redirects) {
427 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
428 }
429
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000430 u->buffering = plcf->upstream.buffering;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000431
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000432 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
433 if (u->pipe == NULL) {
434 return NGX_HTTP_INTERNAL_SERVER_ERROR;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000435 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000436
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000437 u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
438
Igor Sysoev899b44e2005-05-12 14:58:06 +0000439 u->accel = 1;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000440
Igor Sysoev02f742b2005-04-08 15:18:55 +0000441 r->upstream = u;
442
443 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
444
445 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
446 return rc;
447 }
448
449 return NGX_DONE;
450}
451
452
453static ngx_int_t
454ngx_http_proxy_create_request(ngx_http_request_t *r)
455{
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000456 size_t len, loc_len, body_len;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000457 uintptr_t escape;
458 ngx_buf_t *b;
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000459 ngx_str_t method;
460 ngx_uint_t i, unparsed_uri;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000461 ngx_chain_t *cl, *body;
462 ngx_list_part_t *part;
463 ngx_table_elt_t *header;
464 ngx_http_upstream_t *u;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000465 ngx_http_proxy_ctx_t *p;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000466 ngx_http_script_code_pt code;
467 ngx_http_script_engine_t e, le;
468 ngx_http_proxy_loc_conf_t *plcf;
469 ngx_http_script_len_code_pt lcode;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000470
471 u = r->upstream;
472
473 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
474
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000475 p = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
476 if (p == NULL) {
477 return NGX_HTTP_INTERNAL_SERVER_ERROR;
478 }
479
480 ngx_http_set_ctx(r, p, ngx_http_proxy_module);
481
Igor Sysoev02f742b2005-04-08 15:18:55 +0000482 len = sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;
483
Igor Sysoev78452232005-10-12 13:50:36 +0000484 if (u->method.len) {
485 /* HEAD was changed to GET to cache response */
486 method = u->method;
487 method.len++;
488
489 } else if (plcf->method.len) {
490 method = plcf->method;
491
Igor Sysoev02f742b2005-04-08 15:18:55 +0000492 } else {
Igor Sysoev78452232005-10-12 13:50:36 +0000493 method = r->method_name;
494 method.len++;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000495 }
496
Igor Sysoev78452232005-10-12 13:50:36 +0000497 len += method.len + u->conf->uri.len;
498
Igor Sysoev02f742b2005-04-08 15:18:55 +0000499 escape = 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000500
Igor Sysoev6275f562006-03-28 12:24:47 +0000501 loc_len = (r->valid_location && u->conf->uri.len) ? u->conf->location.len:
502 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000503
Igor Sysoev8290d282006-02-03 12:58:48 +0000504 if (u->conf->uri.len == 0 && r->valid_unparsed_uri && r == r->main) {
505 unparsed_uri = 1;
Igor Sysoevceb99292005-09-06 16:09:32 +0000506 len += r->unparsed_uri.len;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000507
508 } else {
Igor Sysoev8290d282006-02-03 12:58:48 +0000509 unparsed_uri = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000510 if (r->quoted_uri) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000511 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
512 r->uri.len - loc_len, NGX_ESCAPE_URI);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000513 }
514
Igor Sysoev899b44e2005-05-12 14:58:06 +0000515 len += r->uri.len - loc_len + escape + sizeof("?") - 1 + r->args.len;
516 }
517
Igor Sysoev09c684b2005-11-09 17:25:55 +0000518 ngx_http_script_flush_no_cachable_variables(r, plcf->flushes);
519
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000520 if (plcf->body_set_len) {
521 le.ip = plcf->body_set_len->elts;
522 le.request = r;
523 le.flushed = 1;
524 body_len = 0;
525
526 while (*(uintptr_t *) le.ip) {
527 lcode = *(ngx_http_script_len_code_pt *) le.ip;
528 body_len += lcode(&le);
529 }
530
531 p->internal_body_length = body_len;
532 len += body_len;
533 }
534
Igor Sysoev899b44e2005-05-12 14:58:06 +0000535 le.ip = plcf->headers_set_len->elts;
536 le.request = r;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000537 le.flushed = 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000538
539 while (*(uintptr_t *) le.ip) {
540 while (*(uintptr_t *) le.ip) {
541 lcode = *(ngx_http_script_len_code_pt *) le.ip;
542 len += lcode(&le);
543 }
544 le.ip += sizeof(uintptr_t);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000545 }
546
547
Igor Sysoev899b44e2005-05-12 14:58:06 +0000548 if (plcf->upstream.pass_request_headers) {
549 part = &r->headers_in.headers.part;
550 header = part->elts;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000551
Igor Sysoev899b44e2005-05-12 14:58:06 +0000552 for (i = 0; /* void */; i++) {
553
554 if (i >= part->nelts) {
555 if (part->next == NULL) {
556 break;
557 }
558
559 part = part->next;
560 header = part->elts;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000561 i = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000562 }
563
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000564 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
565 header[i].lowcase_key, header[i].key.len))
Igor Sysoev899b44e2005-05-12 14:58:06 +0000566 {
567 continue;
568 }
569
570 len += header[i].key.len + sizeof(": ") - 1
571 + header[i].value.len + sizeof(CRLF) - 1;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000572 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000573 }
574
Igor Sysoev899b44e2005-05-12 14:58:06 +0000575
Igor Sysoev02f742b2005-04-08 15:18:55 +0000576 b = ngx_create_temp_buf(r->pool, len);
577 if (b == NULL) {
578 return NGX_ERROR;
579 }
580
581 cl = ngx_alloc_chain_link(r->pool);
582 if (cl == NULL) {
583 return NGX_ERROR;
584 }
585
586 cl->buf = b;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000587
588
589 /* the request line */
590
Igor Sysoev09c684b2005-11-09 17:25:55 +0000591 b->last = ngx_copy(b->last, method.data, method.len);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000592
Igor Sysoeva2573672005-10-05 14:46:21 +0000593 u->uri.data = b->last;
594
Igor Sysoev8290d282006-02-03 12:58:48 +0000595 if (unparsed_uri) {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000596 b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);
597
Igor Sysoev02f742b2005-04-08 15:18:55 +0000598 } else {
Igor Sysoeva2573672005-10-05 14:46:21 +0000599 if (r->valid_location) {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000600 b->last = ngx_copy(b->last, u->conf->uri.data, u->conf->uri.len);
Igor Sysoeva2573672005-10-05 14:46:21 +0000601 }
Igor Sysoev82dc95e2005-10-03 12:53:14 +0000602
Igor Sysoev02f742b2005-04-08 15:18:55 +0000603 if (escape) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000604 ngx_escape_uri(b->last, r->uri.data + loc_len,
605 r->uri.len - loc_len, NGX_ESCAPE_URI);
606 b->last += r->uri.len - loc_len + escape;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000607
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000608 } else {
Igor Sysoev09c684b2005-11-09 17:25:55 +0000609 b->last = ngx_copy(b->last, r->uri.data + loc_len,
610 r->uri.len - loc_len);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000611 }
612
613 if (r->args.len > 0) {
614 *b->last++ = '?';
Igor Sysoev09c684b2005-11-09 17:25:55 +0000615 b->last = ngx_copy(b->last, r->args.data, r->args.len);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000616 }
617 }
618
Igor Sysoeva2573672005-10-05 14:46:21 +0000619 u->uri.len = b->last - u->uri.data;
620
Igor Sysoev02f742b2005-04-08 15:18:55 +0000621 b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
622 sizeof(ngx_http_proxy_version) - 1);
623
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000624 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
Igor Sysoev02f742b2005-04-08 15:18:55 +0000625
626 e.ip = plcf->headers_set->elts;
627 e.pos = b->last;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000628 e.request = r;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000629 e.flushed = 1;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000630
Igor Sysoev899b44e2005-05-12 14:58:06 +0000631 le.ip = plcf->headers_set_len->elts;
632
633 while (*(uintptr_t *) le.ip) {
634 lcode = *(ngx_http_script_len_code_pt *) le.ip;
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000635
636 /* skip the header line name length */
637 (void) lcode(&le);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000638
639 if (*(ngx_http_script_len_code_pt *) le.ip) {
640
641 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
642 lcode = *(ngx_http_script_len_code_pt *) le.ip;
643 }
644
645 e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0;
646
647 } else {
648 e.skip = 0;
649 }
650
651 le.ip += sizeof(uintptr_t);
652
653 while (*(uintptr_t *) e.ip) {
654 code = *(ngx_http_script_code_pt *) e.ip;
655 code((ngx_http_script_engine_t *) &e);
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000656 }
Igor Sysoev899b44e2005-05-12 14:58:06 +0000657 e.ip += sizeof(uintptr_t);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000658 }
659
660 b->last = e.pos;
661
662
Igor Sysoev899b44e2005-05-12 14:58:06 +0000663 if (plcf->upstream.pass_request_headers) {
664 part = &r->headers_in.headers.part;
665 header = part->elts;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000666
Igor Sysoev899b44e2005-05-12 14:58:06 +0000667 for (i = 0; /* void */; i++) {
Igor Sysoev02f742b2005-04-08 15:18:55 +0000668
Igor Sysoev899b44e2005-05-12 14:58:06 +0000669 if (i >= part->nelts) {
670 if (part->next == NULL) {
671 break;
672 }
673
674 part = part->next;
675 header = part->elts;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000676 i = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +0000677 }
678
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000679 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
680 header[i].lowcase_key, header[i].key.len))
Igor Sysoev899b44e2005-05-12 14:58:06 +0000681 {
682 continue;
683 }
684
Igor Sysoev09c684b2005-11-09 17:25:55 +0000685 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000686
687 *b->last++ = ':'; *b->last++ = ' ';
688
Igor Sysoev09c684b2005-11-09 17:25:55 +0000689 b->last = ngx_copy(b->last, header[i].value.data,
690 header[i].value.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000691
692 *b->last++ = CR; *b->last++ = LF;
693
694 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
695 "http proxy header: \"%V: %V\"",
696 &header[i].key, &header[i].value);
Igor Sysoev02f742b2005-04-08 15:18:55 +0000697 }
Igor Sysoev02f742b2005-04-08 15:18:55 +0000698 }
699
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000700
Igor Sysoev02f742b2005-04-08 15:18:55 +0000701 /* add "\r\n" at the header end */
702 *b->last++ = CR; *b->last++ = LF;
703
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000704 if (plcf->body_set) {
705 e.ip = plcf->body_set->elts;
706 e.pos = b->last;
707
708 while (*(uintptr_t *) e.ip) {
709 code = *(ngx_http_script_code_pt *) e.ip;
710 code((ngx_http_script_engine_t *) &e);
711 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000712
Igor Sysoevb141f2b2005-11-15 14:49:57 +0000713 b->last = e.pos;
714 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000715
Igor Sysoev02f742b2005-04-08 15:18:55 +0000716#if (NGX_DEBUG)
717 {
718 ngx_str_t s;
719
720 s.len = b->last - b->pos;
721 s.data = b->pos;
722 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
723 "http proxy header:\n\"%V\"", &s);
724 }
725#endif
726
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000727 if (plcf->body_set == NULL && plcf->upstream.pass_request_body) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000728
729 body = u->request_bufs;
730 u->request_bufs = cl;
731
732 while (body) {
733 b = ngx_alloc_buf(r->pool);
734 if (b == NULL) {
735 return NGX_ERROR;
736 }
737
738 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
739
740 cl->next = ngx_alloc_chain_link(r->pool);
741 if (cl->next == NULL) {
742 return NGX_ERROR;
743 }
744
745 cl = cl->next;
746 cl->buf = b;
747
748 body = body->next;
749 }
750
Igor Sysoevffe71442006-02-08 15:33:12 +0000751 b->flush = 1;
752
Igor Sysoev899b44e2005-05-12 14:58:06 +0000753 } else {
754 u->request_bufs = cl;
755 }
756
757 cl->next = NULL;
758
Igor Sysoev02f742b2005-04-08 15:18:55 +0000759 return NGX_OK;
760}
761
762
763static ngx_int_t
764ngx_http_proxy_reinit_request(ngx_http_request_t *r)
765{
Igor Sysoev899b44e2005-05-12 14:58:06 +0000766 ngx_http_proxy_ctx_t *p;
767
768 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
769
770 if (p == NULL) {
771 return NGX_OK;
772 }
773
774 p->status = 0;
775 p->status_count = 0;
776 p->status_start = NULL;
777 p->status_end = NULL;
778
779 r->upstream->process_header = ngx_http_proxy_process_status_line;
780
781 return NGX_OK;
782}
783
784
785static ngx_int_t
786ngx_http_proxy_process_status_line(ngx_http_request_t *r)
787{
788 ngx_int_t rc;
789 ngx_http_upstream_t *u;
790 ngx_http_proxy_ctx_t *p;
791
792 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
793
794 if (p == NULL) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000795 return NGX_HTTP_INTERNAL_SERVER_ERROR;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000796 }
797
798 rc = ngx_http_proxy_parse_status_line(r, p);
799
800 if (rc == NGX_AGAIN) {
801 return rc;
802 }
803
804 u = r->upstream;
805
806 if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
807 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000808 "upstream sent no valid HTTP/1.0 header");
Igor Sysoev899b44e2005-05-12 14:58:06 +0000809
Igor Sysoev13c68742006-03-10 12:51:52 +0000810#if 0
Igor Sysoev899b44e2005-05-12 14:58:06 +0000811 if (u->accel) {
812 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
813 }
Igor Sysoev13c68742006-03-10 12:51:52 +0000814#endif
Igor Sysoev899b44e2005-05-12 14:58:06 +0000815
816 r->http_version = NGX_HTTP_VERSION_9;
817 p->status = NGX_HTTP_OK;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000818
Igor Sysoev899b44e2005-05-12 14:58:06 +0000819 return NGX_OK;
820 }
821
Igor Sysoev187b7d92005-07-14 12:51:53 +0000822 u->headers_in.status_n = p->status;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000823 u->state->status = p->status;
824
Igor Sysoev187b7d92005-07-14 12:51:53 +0000825 u->headers_in.status_line.len = p->status_end - p->status_start;
826 u->headers_in.status_line.data = ngx_palloc(r->pool,
827 u->headers_in.status_line.len);
828 if (u->headers_in.status_line.data == NULL) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000829 return NGX_HTTP_INTERNAL_SERVER_ERROR;
830 }
Igor Sysoev09c684b2005-11-09 17:25:55 +0000831
Igor Sysoev187b7d92005-07-14 12:51:53 +0000832 ngx_memcpy(u->headers_in.status_line.data, p->status_start,
833 u->headers_in.status_line.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000834
835 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
836 "http proxy status %ui \"%V\"",
Igor Sysoev187b7d92005-07-14 12:51:53 +0000837 u->headers_in.status, &u->headers_in.status_line);
Igor Sysoev899b44e2005-05-12 14:58:06 +0000838
839 u->process_header = ngx_http_proxy_process_header;
840
841 return ngx_http_proxy_process_header(r);
842}
843
844
845static ngx_int_t
846ngx_http_proxy_parse_status_line(ngx_http_request_t *r, ngx_http_proxy_ctx_t *p)
847{
848 u_char ch;
849 u_char *pos;
850 ngx_http_upstream_t *u;
851 enum {
852 sw_start = 0,
853 sw_H,
854 sw_HT,
855 sw_HTT,
856 sw_HTTP,
857 sw_first_major_digit,
858 sw_major_digit,
859 sw_first_minor_digit,
860 sw_minor_digit,
861 sw_status,
862 sw_space_after_status,
863 sw_status_text,
864 sw_almost_done
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000865 } state;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000866
867 u = r->upstream;
868
869 state = r->state;
870
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000871 for (pos = u->buffer.pos; pos < u->buffer.last; pos++) {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000872 ch = *pos;
873
874 switch (state) {
875
876 /* "HTTP/" */
877 case sw_start:
878 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000879 case 'H':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000880 state = sw_H;
881 break;
882 default:
883 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
884 }
885 break;
886
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000887 case sw_H:
Igor Sysoev899b44e2005-05-12 14:58:06 +0000888 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000889 case 'T':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000890 state = sw_HT;
891 break;
892 default:
893 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
894 }
895 break;
896
897 case sw_HT:
898 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000899 case 'T':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000900 state = sw_HTT;
901 break;
902 default:
903 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
904 }
905 break;
906
907 case sw_HTT:
908 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000909 case 'P':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000910 state = sw_HTTP;
911 break;
912 default:
913 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
914 }
915 break;
916
917 case sw_HTTP:
918 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000919 case '/':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000920 state = sw_first_major_digit;
921 break;
922 default:
923 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
924 }
925 break;
926
927 /* the first digit of major HTTP version */
928 case sw_first_major_digit:
929 if (ch < '1' || ch > '9') {
930 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
931 }
932
933 state = sw_major_digit;
934 break;
935
936 /* the major HTTP version or dot */
937 case sw_major_digit:
938 if (ch == '.') {
939 state = sw_first_minor_digit;
940 break;
941 }
942
943 if (ch < '0' || ch > '9') {
944 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
945 }
946
947 break;
948
949 /* the first digit of minor HTTP version */
950 case sw_first_minor_digit:
951 if (ch < '0' || ch > '9') {
952 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
953 }
954
955 state = sw_minor_digit;
956 break;
957
958 /* the minor HTTP version or the end of the request line */
959 case sw_minor_digit:
960 if (ch == ' ') {
961 state = sw_status;
962 break;
963 }
964
965 if (ch < '0' || ch > '9') {
966 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
967 }
968
969 break;
970
971 /* HTTP status code */
972 case sw_status:
Igor Sysoev13c68742006-03-10 12:51:52 +0000973 if (ch == ' ') {
974 break;
975 }
976
Igor Sysoev899b44e2005-05-12 14:58:06 +0000977 if (ch < '0' || ch > '9') {
978 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
979 }
980
981 p->status = p->status * 10 + ch - '0';
982
983 if (++p->status_count == 3) {
984 state = sw_space_after_status;
985 p->status_start = pos - 2;
986 }
987
988 break;
989
990 /* space or end of line */
991 case sw_space_after_status:
992 switch (ch) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000993 case ' ':
Igor Sysoev899b44e2005-05-12 14:58:06 +0000994 state = sw_status_text;
995 break;
996 case '.': /* IIS may send 403.1, 403.2, etc */
997 state = sw_status_text;
998 break;
999 case CR:
1000 state = sw_almost_done;
1001 break;
1002 case LF:
1003 goto done;
1004 default:
1005 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
1006 }
1007 break;
1008
1009 /* any text until end of line */
1010 case sw_status_text:
1011 switch (ch) {
1012 case CR:
1013 state = sw_almost_done;
1014
1015 break;
1016 case LF:
1017 goto done;
1018 }
1019 break;
1020
Igor Sysoev5192b362005-07-08 14:34:20 +00001021 /* end of status line */
Igor Sysoev899b44e2005-05-12 14:58:06 +00001022 case sw_almost_done:
1023 p->status_end = pos - 1;
1024 switch (ch) {
1025 case LF:
1026 goto done;
1027 default:
1028 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
1029 }
1030 }
1031 }
1032
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001033 u->buffer.pos = pos;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001034 r->state = state;
1035
1036 return NGX_AGAIN;
1037
1038done:
1039
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001040 u->buffer.pos = pos + 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001041
1042 if (p->status_end == NULL) {
1043 p->status_end = pos;
1044 }
1045
1046 r->state = sw_start;
1047
Igor Sysoev02f742b2005-04-08 15:18:55 +00001048 return NGX_OK;
1049}
1050
1051
1052static ngx_int_t
1053ngx_http_proxy_process_header(ngx_http_request_t *r)
1054{
Igor Sysoev899b44e2005-05-12 14:58:06 +00001055 ngx_int_t rc;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001056 ngx_uint_t i;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001057 ngx_table_elt_t *h;
1058 ngx_http_upstream_header_t *hh;
1059 ngx_http_upstream_main_conf_t *umcf;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001060
Igor Sysoev899b44e2005-05-12 14:58:06 +00001061 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
Igor Sysoev02f742b2005-04-08 15:18:55 +00001062
Igor Sysoev899b44e2005-05-12 14:58:06 +00001063 for ( ;; ) {
1064
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001065 rc = ngx_http_parse_header_line(r, &r->upstream->buffer);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001066
1067 if (rc == NGX_OK) {
1068
1069 /* a header line has been parsed successfully */
1070
1071 h = ngx_list_push(&r->upstream->headers_in.headers);
1072 if (h == NULL) {
1073 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1074 }
1075
1076 h->hash = r->header_hash;
1077
1078 h->key.len = r->header_name_end - r->header_name_start;
1079 h->value.len = r->header_end - r->header_start;
1080
1081 h->key.data = ngx_palloc(r->pool,
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001082 h->key.len + 1 + h->value.len + 1 + h->key.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001083 if (h->key.data == NULL) {
1084 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1085 }
1086
1087 h->value.data = h->key.data + h->key.len + 1;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001088 h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001089
1090 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
1091 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
1092
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001093 if (h->key.len == r->lowcase_index) {
1094 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001095
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001096 } else {
1097 for (i = 0; i < h->key.len; i++) {
1098 h->lowcase_key[i] = ngx_tolower(h->lowcase_key[i]);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001099 }
1100 }
1101
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001102 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
1103 h->lowcase_key, h->key.len);
1104
1105 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
1106 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1107 }
1108
Igor Sysoev899b44e2005-05-12 14:58:06 +00001109 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1110 "http proxy header: \"%V: %V\"",
1111 &h->key, &h->value);
1112
1113 continue;
1114 }
1115
1116 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
1117
1118 /* a whole header has been parsed successfully */
1119
1120 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1121 "http proxy header done");
1122
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001123 /*
1124 * if no "Server" and "Date" in header line,
1125 * then add the special empty headers
1126 */
1127
1128 if (r->upstream->headers_in.server == NULL) {
1129 h = ngx_list_push(&r->upstream->headers_in.headers);
1130 if (h == NULL) {
1131 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1132 }
1133
1134 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
1135 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
1136
1137 h->key.len = sizeof("Server") - 1;
1138 h->key.data = (u_char *) "Server";
1139 h->value.len = 0;
1140 h->value.data = NULL;
1141 h->lowcase_key = (u_char *) "server";
1142 }
1143
1144 if (r->upstream->headers_in.date == NULL) {
1145 h = ngx_list_push(&r->upstream->headers_in.headers);
1146 if (h == NULL) {
1147 return NGX_HTTP_INTERNAL_SERVER_ERROR;
1148 }
1149
1150 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');
1151
1152 h->key.len = sizeof("Date") - 1;
1153 h->key.data = (u_char *) "Date";
1154 h->value.len = 0;
1155 h->value.data = NULL;
1156 h->lowcase_key = (u_char *) "date";
1157 }
1158
Igor Sysoev899b44e2005-05-12 14:58:06 +00001159 return NGX_OK;
1160 }
1161
Igor Sysoev7b190b42005-06-07 15:56:31 +00001162 if (rc == NGX_AGAIN) {
1163 return NGX_AGAIN;
1164 }
1165
Igor Sysoev899b44e2005-05-12 14:58:06 +00001166 /* there was error while a header line parsing */
1167
1168 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
Igor Sysoev13c68742006-03-10 12:51:52 +00001169 "upstream sent invalid header");
Igor Sysoev899b44e2005-05-12 14:58:06 +00001170
1171 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
1172 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001173}
1174
1175
1176static void
1177ngx_http_proxy_abort_request(ngx_http_request_t *r)
1178{
1179 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1180 "abort http proxy request");
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001181
Igor Sysoev02f742b2005-04-08 15:18:55 +00001182 return;
1183}
1184
1185
1186static void
1187ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001188{
Igor Sysoev02f742b2005-04-08 15:18:55 +00001189 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1190 "finalize http proxy request");
1191
1192 return;
1193}
1194
1195
Igor Sysoev09c684b2005-11-09 17:25:55 +00001196static ngx_int_t
1197ngx_http_proxy_host_variable(ngx_http_request_t *r,
1198 ngx_http_variable_value_t *v, uintptr_t data)
Igor Sysoev02f742b2005-04-08 15:18:55 +00001199{
Igor Sysoev02f742b2005-04-08 15:18:55 +00001200 ngx_http_proxy_loc_conf_t *plcf;
1201
Igor Sysoev02f742b2005-04-08 15:18:55 +00001202 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
1203
Igor Sysoev09c684b2005-11-09 17:25:55 +00001204 v->len = plcf->host_header.len;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001205 v->valid = 1;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001206 v->no_cachable = 0;
1207 v->not_found = 0;
1208 v->data = plcf->host_header.data;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001209
Igor Sysoev09c684b2005-11-09 17:25:55 +00001210 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001211}
1212
1213
Igor Sysoev09c684b2005-11-09 17:25:55 +00001214static ngx_int_t
1215ngx_http_proxy_port_variable(ngx_http_request_t *r,
1216 ngx_http_variable_value_t *v, uintptr_t data)
Igor Sysoev899b44e2005-05-12 14:58:06 +00001217{
Igor Sysoev899b44e2005-05-12 14:58:06 +00001218 ngx_http_proxy_loc_conf_t *plcf;
1219
Igor Sysoev899b44e2005-05-12 14:58:06 +00001220 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
1221
Igor Sysoev09c684b2005-11-09 17:25:55 +00001222 v->len = plcf->port_text.len;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001223 v->valid = 1;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001224 v->no_cachable = 0;
1225 v->not_found = 0;
1226 v->data = plcf->port_text.data;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001227
Igor Sysoev09c684b2005-11-09 17:25:55 +00001228 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001229}
1230
1231
Igor Sysoev09c684b2005-11-09 17:25:55 +00001232static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00001233ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
Igor Sysoev09c684b2005-11-09 17:25:55 +00001234 ngx_http_variable_value_t *v, uintptr_t data)
Igor Sysoev899b44e2005-05-12 14:58:06 +00001235{
Igor Sysoev09c684b2005-11-09 17:25:55 +00001236 u_char *p;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001237
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001238 v->valid = 1;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001239 v->no_cachable = 0;
1240 v->not_found = 0;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001241
1242 if (r->headers_in.x_forwarded_for == NULL) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00001243 v->len = r->connection->addr_text.len;
1244 v->data = r->connection->addr_text.data;
1245 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001246 }
1247
Igor Sysoev09c684b2005-11-09 17:25:55 +00001248 v->len = r->headers_in.x_forwarded_for->value.len
1249 + sizeof(", ") - 1 + r->connection->addr_text.len;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001250
Igor Sysoev09c684b2005-11-09 17:25:55 +00001251 p = ngx_palloc(r->pool, v->len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001252 if (p == NULL) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00001253 return NGX_ERROR;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001254 }
1255
Igor Sysoev09c684b2005-11-09 17:25:55 +00001256 v->data = p;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001257
Igor Sysoev09c684b2005-11-09 17:25:55 +00001258 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
1259 r->headers_in.x_forwarded_for->value.len);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001260
1261 *p++ = ','; *p++ = ' ';
1262
1263 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
1264
Igor Sysoev09c684b2005-11-09 17:25:55 +00001265 return NGX_OK;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001266}
1267
1268
1269static ngx_int_t
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001270ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
1271 ngx_http_variable_value_t *v, uintptr_t data)
1272{
1273 ngx_http_proxy_ctx_t *p;
1274
1275 p = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
1276
1277 if (p == NULL) {
1278 v->not_found = 1;
1279 return NGX_OK;
1280 }
1281
1282 v->valid = 1;
1283 v->no_cachable = 0;
1284 v->not_found = 0;
1285
1286 v->data = ngx_palloc(r->connection->pool, NGX_SIZE_T_LEN);
1287
1288 if (v->data == NULL) {
1289 return NGX_ERROR;
1290 }
1291
1292 v->len = ngx_sprintf(v->data, "%uz", p->internal_body_length) - v->data;
1293
1294 return NGX_OK;
1295}
1296
1297
1298static ngx_int_t
Igor Sysoev899b44e2005-05-12 14:58:06 +00001299ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
1300 size_t prefix)
1301{
1302 ngx_int_t rc;
1303 ngx_uint_t i;
1304 ngx_http_proxy_loc_conf_t *plcf;
1305 ngx_http_proxy_redirect_t *pr;
1306
1307 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
1308
1309 pr = plcf->redirects->elts;
1310
1311 if (pr == NULL) {
1312 return NGX_DECLINED;
1313 }
1314
1315 for (i = 0; i < plcf->redirects->nelts; i++) {
Igor Sysoev9e580192006-02-01 18:22:15 +00001316 rc = pr[i].handler(r, h, prefix, &pr[i]);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001317
1318 if (rc != NGX_DECLINED) {
1319 return rc;
1320 }
1321 }
1322
1323 return NGX_DECLINED;
1324}
1325
1326
1327static ngx_int_t
1328ngx_http_proxy_rewrite_redirect_text(ngx_http_request_t *r, ngx_table_elt_t *h,
1329 size_t prefix, ngx_http_proxy_redirect_t *pr)
1330{
1331 size_t len;
1332 u_char *data, *p;
1333
1334 if (pr->redirect.len > h->value.len - prefix
1335 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
1336 pr->redirect.len) != 0)
1337 {
1338 return NGX_DECLINED;
1339 }
1340
1341 len = prefix + pr->replacement.text.len + h->value.len - pr->redirect.len;
1342
1343 data = ngx_palloc(r->pool, len);
1344 if (data == NULL) {
1345 return NGX_ERROR;
1346 }
1347
1348 p = data;
1349
Igor Sysoev09c684b2005-11-09 17:25:55 +00001350 p = ngx_copy(p, h->value.data, prefix);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001351
Igor Sysoevafd7ec52006-05-29 17:28:12 +00001352 if (pr->replacement.text.len) {
1353 p = ngx_copy(p, pr->replacement.text.data, pr->replacement.text.len);
1354 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00001355
1356 ngx_memcpy(p, h->value.data + prefix + pr->redirect.len,
1357 h->value.len - pr->redirect.len - prefix);
1358
1359 h->value.len = len;
1360 h->value.data = data;
1361
1362 return NGX_OK;
1363}
1364
1365
1366static ngx_int_t
1367ngx_http_proxy_rewrite_redirect_vars(ngx_http_request_t *r, ngx_table_elt_t *h,
1368 size_t prefix, ngx_http_proxy_redirect_t *pr)
1369{
1370 size_t len;
1371 u_char *data, *p;
1372 ngx_http_script_code_pt code;
1373 ngx_http_script_engine_t e;
1374 ngx_http_script_len_code_pt lcode;
1375
1376 if (pr->redirect.len > h->value.len - prefix
1377 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
1378 pr->redirect.len) != 0)
1379 {
1380 return NGX_DECLINED;
1381 }
1382
1383 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
1384
1385 e.ip = pr->replacement.vars.lengths;
1386 e.request = r;
1387
1388 for (len = prefix; *(uintptr_t *) e.ip; len += lcode(&e)) {
1389 lcode = *(ngx_http_script_len_code_pt *) e.ip;
1390 }
1391
1392 data = ngx_palloc(r->pool, len);
1393 if (data == NULL) {
1394 return NGX_ERROR;
1395 }
1396
1397 p = data;
1398
Igor Sysoev09c684b2005-11-09 17:25:55 +00001399 p = ngx_copy(p, h->value.data, prefix);
Igor Sysoev899b44e2005-05-12 14:58:06 +00001400
1401 e.ip = pr->replacement.vars.values;
1402 e.pos = p;
1403
1404 while (*(uintptr_t *) e.ip) {
1405 code = *(ngx_http_script_code_pt *) e.ip;
1406 code(&e);
1407 }
1408
1409 h->value.len = len;
1410 h->value.data = data;
1411
1412 return NGX_OK;
1413}
1414
1415
1416static ngx_int_t
1417ngx_http_proxy_add_variables(ngx_conf_t *cf)
1418{
1419 ngx_http_variable_t *var, *v;
1420
1421 for (v = ngx_http_proxy_vars; v->name.len; v++) {
1422 var = ngx_http_add_variable(cf, &v->name, v->flags);
1423 if (var == NULL) {
1424 return NGX_ERROR;
1425 }
1426
Igor Sysoev7bdb7202006-04-19 15:30:56 +00001427 var->get_handler = v->get_handler;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001428 var->data = v->data;
1429 }
1430
1431 return NGX_OK;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001432}
1433
1434
1435static void *
1436ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
1437{
1438 ngx_http_proxy_loc_conf_t *conf;
1439
1440 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
1441 if (conf == NULL) {
1442 return NGX_CONF_ERROR;
1443 }
1444
1445 /*
1446 * set by ngx_pcalloc():
1447 *
1448 * conf->upstream.bufs.num = 0;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001449 * conf->upstream.next_upstream = 0;
1450 * conf->upstream.temp_path = NULL;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001451 * conf->upstream.hide_headers_hash = { NULL, 0 };
1452 * conf->upstream.hide_headers = NULL;
1453 * conf->upstream.pass_headers = NULL;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001454 * conf->upstream.schema = { 0, NULL };
1455 * conf->upstream.uri = { 0, NULL };
1456 * conf->upstream.location = NULL;
1457 *
Igor Sysoev78452232005-10-12 13:50:36 +00001458 * conf->method = NULL;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001459 * conf->headers_source = NULL;
1460 * conf->headers_set_len = NULL;
1461 * conf->headers_set = NULL;
1462 * conf->headers_set_hash = NULL;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001463 * conf->body_set_len = NULL;
1464 * conf->body_set = NULL;
1465 * conf->body_source = { 0, NULL };
Igor Sysoev899b44e2005-05-12 14:58:06 +00001466 * conf->rewrite_locations = NULL;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001467 */
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001468
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001469 conf->upstream.buffering = NGX_CONF_UNSET;
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00001470 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001471
Igor Sysoev02f742b2005-04-08 15:18:55 +00001472 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
1473 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
1474 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
1475
1476 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001477 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001478
1479 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001480 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001481 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001482
Igor Sysoev78452232005-10-12 13:50:36 +00001483 conf->upstream.max_fails = NGX_CONF_UNSET_UINT;
1484 conf->upstream.fail_timeout = NGX_CONF_UNSET;
1485
Igor Sysoev899b44e2005-05-12 14:58:06 +00001486 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
1487 conf->upstream.pass_request_body = NGX_CONF_UNSET;
Igor Sysoev78452232005-10-12 13:50:36 +00001488
Igor Sysoev02f742b2005-04-08 15:18:55 +00001489 conf->upstream.redirect_errors = NGX_CONF_UNSET;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001490
1491 /* "proxy_cyclic_temp_file" is disabled */
1492 conf->upstream.cyclic_temp_file = 0;
1493
Igor Sysoev899b44e2005-05-12 14:58:06 +00001494 conf->redirect = NGX_CONF_UNSET;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001495 conf->upstream.change_buffering = 1;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001496
1497 return conf;
1498}
1499
1500
1501static char *
1502ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
1503{
1504 ngx_http_proxy_loc_conf_t *prev = parent;
1505 ngx_http_proxy_loc_conf_t *conf = child;
1506
Igor Sysoev899b44e2005-05-12 14:58:06 +00001507 u_char *p;
1508 size_t size;
1509 uintptr_t *code;
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001510 ngx_str_t *header;
1511 ngx_uint_t i, j;
1512 ngx_array_t hide_headers;
1513 ngx_keyval_t *src, *s, *h;
1514 ngx_hash_key_t *hk;
1515 ngx_hash_init_t hash;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001516 ngx_http_proxy_redirect_t *pr;
1517 ngx_http_script_compile_t sc;
1518 ngx_http_script_copy_code_t *copy;
1519
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001520 ngx_conf_merge_value(conf->upstream.buffering,
1521 prev->upstream.buffering, 1);
1522
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00001523 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
1524 prev->upstream.ignore_client_abort, 0);
1525
Igor Sysoev02f742b2005-04-08 15:18:55 +00001526 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
1527 prev->upstream.connect_timeout, 60000);
1528
1529 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
1530 prev->upstream.send_timeout, 60000);
1531
1532 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
1533 prev->upstream.read_timeout, 60000);
1534
1535 ngx_conf_merge_size_value(conf->upstream.send_lowat,
1536 prev->upstream.send_lowat, 0);
1537
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001538 ngx_conf_merge_size_value(conf->upstream.buffer_size,
1539 prev->upstream.buffer_size,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001540 (size_t) ngx_pagesize);
1541
1542 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
1543 8, ngx_pagesize);
1544
1545 if (conf->upstream.bufs.num < 2) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001546 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001547 "there must be at least 2 \"proxy_buffers\"");
1548 return NGX_CONF_ERROR;
1549 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001550
Igor Sysoev02f742b2005-04-08 15:18:55 +00001551
Igor Sysoevc31a9bb2005-11-26 10:11:11 +00001552 size = conf->upstream.buffer_size;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001553 if (size < conf->upstream.bufs.size) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001554 size = conf->upstream.bufs.size;
1555 }
1556
1557
Igor Sysoev187b7d92005-07-14 12:51:53 +00001558 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
1559 prev->upstream.busy_buffers_size_conf,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001560 NGX_CONF_UNSET_SIZE);
1561
Igor Sysoev187b7d92005-07-14 12:51:53 +00001562 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001563 conf->upstream.busy_buffers_size = 2 * size;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001564 } else {
1565 conf->upstream.busy_buffers_size =
1566 conf->upstream.busy_buffers_size_conf;
1567 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001568
Igor Sysoev187b7d92005-07-14 12:51:53 +00001569 if (conf->upstream.busy_buffers_size < size) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001570 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1571 "\"proxy_busy_buffers_size\" must be equal or bigger than "
1572 "maximum of the value of \"proxy_header_buffer_size\" and "
1573 "one of the \"proxy_buffers\"");
1574
1575 return NGX_CONF_ERROR;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001576 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001577
Igor Sysoev187b7d92005-07-14 12:51:53 +00001578 if (conf->upstream.busy_buffers_size
1579 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
Igor Sysoev02f742b2005-04-08 15:18:55 +00001580 {
1581 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1582 "\"proxy_busy_buffers_size\" must be less than "
1583 "the size of all \"proxy_buffers\" minus one buffer");
1584
1585 return NGX_CONF_ERROR;
1586 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001587
Igor Sysoev187b7d92005-07-14 12:51:53 +00001588
1589 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
1590 prev->upstream.temp_file_write_size_conf,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001591 NGX_CONF_UNSET_SIZE);
1592
Igor Sysoev187b7d92005-07-14 12:51:53 +00001593 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001594 conf->upstream.temp_file_write_size = 2 * size;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001595 } else {
1596 conf->upstream.temp_file_write_size =
1597 conf->upstream.temp_file_write_size_conf;
1598 }
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001599
Igor Sysoev187b7d92005-07-14 12:51:53 +00001600 if (conf->upstream.temp_file_write_size < size) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001601 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1602 "\"proxy_temp_file_write_size\" must be equal or bigger than "
1603 "maximum of the value of \"proxy_header_buffer_size\" and "
1604 "one of the \"proxy_buffers\"");
1605
1606 return NGX_CONF_ERROR;
1607 }
1608
Igor Sysoev187b7d92005-07-14 12:51:53 +00001609 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
1610 prev->upstream.max_temp_file_size_conf,
Igor Sysoev02f742b2005-04-08 15:18:55 +00001611 NGX_CONF_UNSET_SIZE);
1612
Igor Sysoev187b7d92005-07-14 12:51:53 +00001613 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00001614 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
Igor Sysoev187b7d92005-07-14 12:51:53 +00001615 } else {
1616 conf->upstream.max_temp_file_size =
1617 conf->upstream.max_temp_file_size_conf;
1618 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001619
Igor Sysoev187b7d92005-07-14 12:51:53 +00001620 if (conf->upstream.max_temp_file_size != 0
1621 && conf->upstream.max_temp_file_size < size)
Igor Sysoev02f742b2005-04-08 15:18:55 +00001622 {
1623 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
Igor Sysoev78452232005-10-12 13:50:36 +00001624 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
Igor Sysoev02f742b2005-04-08 15:18:55 +00001625 "the temporary files usage or must be equal or bigger than "
Igor Sysoev78452232005-10-12 13:50:36 +00001626 "maximum of the value of \"proxy_header_buffer_size\" and "
1627 "one of the \"proxy_buffers\"");
Igor Sysoev02f742b2005-04-08 15:18:55 +00001628
1629 return NGX_CONF_ERROR;
1630 }
1631
Igor Sysoev187b7d92005-07-14 12:51:53 +00001632
Igor Sysoev02f742b2005-04-08 15:18:55 +00001633 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
1634 prev->upstream.next_upstream,
1635 (NGX_CONF_BITMASK_SET
1636 |NGX_HTTP_UPSTREAM_FT_ERROR
1637 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
1638
Igor Sysoev78452232005-10-12 13:50:36 +00001639 ngx_conf_merge_unsigned_value(conf->upstream.max_fails,
1640 prev->upstream.max_fails, 1);
1641
1642 ngx_conf_merge_sec_value(conf->upstream.fail_timeout,
1643 prev->upstream.fail_timeout, 10);
1644
Igor Sysoev6f134cc2006-05-23 14:54:58 +00001645 if (conf->upstream_peers && !conf->upstream_peers->balanced) {
1646 for (i = 0; i < conf->upstream_peers->peers->number; i++) {
1647 conf->upstream_peers->peers->peer[i].weight = 1;
1648 conf->upstream_peers->peers->peer[i].max_fails =
1649 conf->upstream.max_fails;
1650 conf->upstream_peers->peers->peer[i].fail_timeout =
1651 conf->upstream.fail_timeout;
Igor Sysoev78452232005-10-12 13:50:36 +00001652 }
1653 }
1654
Igor Sysoev899b44e2005-05-12 14:58:06 +00001655 ngx_conf_merge_path_value(conf->upstream.temp_path,
1656 prev->upstream.temp_path,
1657 NGX_HTTP_PROXY_TEMP_PATH, 1, 2, 0,
1658 ngx_garbage_collector_temp_handler, cf);
Igor Sysoev02f742b2005-04-08 15:18:55 +00001659
Igor Sysoev78452232005-10-12 13:50:36 +00001660 if (conf->method.len == 0) {
1661 conf->method = prev->method;
1662
1663 } else {
1664 conf->method.data[conf->method.len] = ' ';
1665 conf->method.len++;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001666 }
Igor Sysoev02f742b2005-04-08 15:18:55 +00001667
Igor Sysoev899b44e2005-05-12 14:58:06 +00001668 ngx_conf_merge_value(conf->upstream.pass_request_headers,
1669 prev->upstream.pass_request_headers, 1);
1670 ngx_conf_merge_value(conf->upstream.pass_request_body,
1671 prev->upstream.pass_request_body, 1);
1672
Igor Sysoev187b7d92005-07-14 12:51:53 +00001673 ngx_conf_merge_value(conf->upstream.redirect_errors,
Igor Sysoev899b44e2005-05-12 14:58:06 +00001674 prev->upstream.redirect_errors, 0);
1675
Igor Sysoev899b44e2005-05-12 14:58:06 +00001676 ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
1677
1678 if (conf->redirect) {
1679
1680 if (conf->redirects == NULL) {
1681 conf->redirects = prev->redirects;
1682 }
1683
1684 if (conf->redirects == NULL && conf->upstream.url.data) {
1685
1686 conf->redirects = ngx_array_create(cf->pool, 1,
1687 sizeof(ngx_http_proxy_redirect_t));
1688 if (conf->redirects == NULL) {
1689 return NGX_CONF_ERROR;
1690 }
1691
1692 pr = ngx_array_push(conf->redirects);
1693 if (pr == NULL) {
1694 return NGX_CONF_ERROR;
1695 }
1696
1697 pr->handler = ngx_http_proxy_rewrite_redirect_text;
1698 pr->redirect = conf->upstream.url;
Igor Sysoevafd7ec52006-05-29 17:28:12 +00001699
1700 if (conf->upstream.uri.len) {
1701 pr->replacement.text = conf->upstream.location;
1702
1703 } else {
1704 pr->replacement.text.len = 0;
1705 pr->replacement.text.data = NULL;
1706 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00001707 }
1708 }
1709
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001710 if (conf->upstream.hide_headers == NULL
1711 && conf->upstream.pass_headers == NULL)
1712 {
1713 conf->upstream.hide_headers = prev->upstream.hide_headers;
1714 conf->upstream.pass_headers = prev->upstream.pass_headers;
1715 conf->upstream.hide_headers_hash = prev->upstream.hide_headers_hash;
1716
1717 if (conf->upstream.hide_headers_hash.buckets) {
1718 goto peers;
1719 }
1720
1721 } else {
1722 if (conf->upstream.hide_headers == NULL) {
1723 conf->upstream.hide_headers = prev->upstream.hide_headers;
1724 }
1725
1726 if (conf->upstream.pass_headers == NULL) {
1727 conf->upstream.pass_headers = prev->upstream.pass_headers;
1728 }
1729 }
1730
1731 if (ngx_array_init(&hide_headers, cf->temp_pool, 4, sizeof(ngx_hash_key_t))
1732 != NGX_OK)
1733 {
1734 return NGX_CONF_ERROR;
1735 }
1736
1737 for (header = ngx_http_proxy_hide_headers; header->len; header++) {
1738 hk = ngx_array_push(&hide_headers);
1739 if (hk == NULL) {
1740 return NGX_CONF_ERROR;
1741 }
1742
1743 hk->key = *header;
1744 hk->key_hash = ngx_hash_key_lc(header->data, header->len);
1745 hk->value = (void *) 1;
1746 }
1747
1748 if (conf->upstream.hide_headers) {
1749
1750 header = conf->upstream.hide_headers->elts;
1751
1752 for (i = 0; i < conf->upstream.hide_headers->nelts; i++) {
1753
1754 hk = hide_headers.elts;
1755
1756 for (j = 0; j < hide_headers.nelts; j++) {
1757 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) {
1758 goto exist;
1759 }
1760 }
1761
1762 hk = ngx_array_push(&hide_headers);
1763 if (hk == NULL) {
1764 return NGX_CONF_ERROR;
1765 }
1766
1767 hk->key = header[i];
1768 hk->key_hash = ngx_hash_key_lc(header[i].data, header[i].len);
1769 hk->value = (void *) 1;
1770
1771 exist:
1772
1773 continue;
1774 }
1775 }
1776
1777 if (conf->upstream.pass_headers) {
1778
1779 hk = hide_headers.elts;
1780 header = conf->upstream.pass_headers->elts;
1781
1782 for (i = 0; i < conf->upstream.pass_headers->nelts; i++) {
1783 for (j = 0; j < hide_headers.nelts; j++) {
1784
1785 if (hk[j].key.data == NULL) {
1786 continue;
1787 }
1788
1789 if (ngx_strcasecmp(header[i].data, hk[j].key.data) == 0) {
1790 hk[j].key.data = NULL;
1791 break;
1792 }
1793 }
1794 }
1795 }
1796
1797 hash.hash = &conf->upstream.hide_headers_hash;
1798 hash.key = ngx_hash_key_lc;
1799 hash.max_size = 512;
1800 hash.bucket_size = ngx_cacheline_size;
1801 hash.name = "proxy_hide_headers_hash";
1802 hash.pool = cf->pool;
1803 hash.temp_pool = NULL;
1804
1805 if (ngx_hash_init(&hash, hide_headers.elts, hide_headers.nelts) != NGX_OK) {
1806 return NGX_CONF_ERROR;
1807 }
1808
1809peers:
Igor Sysoev02f742b2005-04-08 15:18:55 +00001810
Igor Sysoev6f134cc2006-05-23 14:54:58 +00001811 if (conf->upstream_peers == NULL) {
1812 conf->upstream_peers = prev->upstream_peers;
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00001813
1814 conf->host_header = prev->host_header;
1815 conf->port_text = prev->port_text;
1816 conf->upstream.schema = prev->upstream.schema;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001817 }
1818
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001819
1820 if (conf->body_source.data == NULL) {
1821 conf->body_source = prev->body_source;
1822 conf->body_set_len = prev->body_set_len;
1823 conf->body_set = prev->body_set;
1824 }
1825
Igor Sysoevb141f2b2005-11-15 14:49:57 +00001826 if (conf->body_source.data && conf->body_set_len == NULL) {
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001827
1828 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
1829
1830 sc.cf = cf;
1831 sc.source = &conf->body_source;
1832 sc.flushes = &conf->flushes;
1833 sc.lengths = &conf->body_set_len;
1834 sc.values = &conf->body_set;
1835 sc.complete_lengths = 1;
1836 sc.complete_values = 1;
1837
1838 if (ngx_http_script_compile(&sc) != NGX_OK) {
1839 return NGX_CONF_ERROR;
1840 }
1841
1842 if (conf->headers_source == NULL) {
1843 conf->headers_source = ngx_array_create(cf->pool, 4,
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001844 sizeof(ngx_keyval_t));
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001845 if (conf->headers_source == NULL) {
1846 return NGX_CONF_ERROR;
1847 }
1848 }
1849
1850 s = ngx_array_push(conf->headers_source);
1851 if (s == NULL) {
1852 return NGX_CONF_ERROR;
1853 }
1854
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001855 s->key.len = sizeof("Content-Length") - 1;
1856 s->key.data = (u_char *) "Content-Length";
1857 s->value.len = sizeof("$proxy_internal_body_length") - 1;
1858 s->value.data = (u_char *) "$proxy_internal_body_length";
1859 }
1860
1861
Igor Sysoev899b44e2005-05-12 14:58:06 +00001862 if (conf->headers_source == NULL) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00001863 conf->flushes = prev->flushes;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001864 conf->headers_set_len = prev->headers_set_len;
1865 conf->headers_set = prev->headers_set;
1866 conf->headers_set_hash = prev->headers_set_hash;
Igor Sysoev09c684b2005-11-09 17:25:55 +00001867 conf->headers_source = prev->headers_source;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001868 }
1869
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001870 if (conf->headers_set_hash.buckets) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00001871 return NGX_CONF_OK;
Igor Sysoev02f742b2005-04-08 15:18:55 +00001872 }
1873
Igor Sysoev899b44e2005-05-12 14:58:06 +00001874
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001875 conf->headers_names = ngx_array_create(cf->pool, 4, sizeof(ngx_hash_key_t));
Igor Sysoev899b44e2005-05-12 14:58:06 +00001876 if (conf->headers_names == NULL) {
1877 return NGX_CONF_ERROR;
1878 }
1879
1880 if (conf->headers_source == NULL) {
1881 conf->headers_source = ngx_array_create(cf->pool, 4,
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001882 sizeof(ngx_keyval_t));
Igor Sysoev899b44e2005-05-12 14:58:06 +00001883 if (conf->headers_source == NULL) {
1884 return NGX_CONF_ERROR;
1885 }
1886 }
1887
1888 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
1889 if (conf->headers_set_len == NULL) {
1890 return NGX_CONF_ERROR;
1891 }
1892
1893 conf->headers_set = ngx_array_create(cf->pool, 512, 1);
1894 if (conf->headers_set == NULL) {
1895 return NGX_CONF_ERROR;
1896 }
1897
1898
1899 src = conf->headers_source->elts;
1900
1901 for (h = ngx_http_proxy_headers; h->key.len; h++) {
1902
1903 for (i = 0; i < conf->headers_source->nelts; i++) {
1904 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
1905 goto next;
1906 }
1907 }
1908
1909 s = ngx_array_push(conf->headers_source);
1910 if (s == NULL) {
1911 return NGX_CONF_ERROR;
1912 }
1913
1914 *s = *h;
1915
Igor Sysoev4959ec42005-05-23 12:07:45 +00001916 src = conf->headers_source->elts;
1917
Igor Sysoev899b44e2005-05-12 14:58:06 +00001918 next:
1919
1920 continue;
1921 }
1922
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00001923
1924 src = conf->headers_source->elts;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001925 for (i = 0; i < conf->headers_source->nelts; i++) {
1926
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001927 hk = ngx_array_push(conf->headers_names);
1928 if (hk == NULL) {
Igor Sysoev899b44e2005-05-12 14:58:06 +00001929 return NGX_CONF_ERROR;
1930 }
1931
Igor Sysoev3338cfd2006-05-11 14:43:47 +00001932 hk->key = src[i].key;
1933 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
1934 hk->value = (void *) 1;
Igor Sysoev899b44e2005-05-12 14:58:06 +00001935
Igor Sysoev403d2442005-05-26 18:12:40 +00001936 if (src[i].value.len == 0) {
1937 continue;
1938 }
1939
Igor Sysoev899b44e2005-05-12 14:58:06 +00001940 if (ngx_http_script_variables_count(&src[i].value) == 0) {
1941 copy = ngx_array_push_n(conf->headers_set_len,
1942 sizeof(ngx_http_script_copy_code_t));
1943 if (copy == NULL) {
1944 return NGX_CONF_ERROR;
1945 }
1946
1947 copy->code = (ngx_http_script_code_pt)
1948 ngx_http_script_copy_len_code;
1949 copy->len = src[i].key.len + sizeof(": ") - 1
1950 + src[i].value.len + sizeof(CRLF) - 1;
1951
1952
1953 size = (sizeof(ngx_http_script_copy_code_t)
1954 + src[i].key.len + sizeof(": ") - 1
1955 + src[i].value.len + sizeof(CRLF) - 1
1956 + sizeof(uintptr_t) - 1)
1957 & ~(sizeof(uintptr_t) - 1);
1958
1959 copy = ngx_array_push_n(conf->headers_set, size);
1960 if (copy == NULL) {
1961 return NGX_CONF_ERROR;
1962 }
1963
1964 copy->code = ngx_http_script_copy_code;
1965 copy->len = src[i].key.len + sizeof(": ") - 1
1966 + src[i].value.len + sizeof(CRLF) - 1;
1967
1968 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
1969
1970 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
1971 *p++ = ':'; *p++ = ' ';
1972 p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
1973 *p++ = CR; *p = LF;
1974
1975 } else {
1976 copy = ngx_array_push_n(conf->headers_set_len,
1977 sizeof(ngx_http_script_copy_code_t));
1978 if (copy == NULL) {
1979 return NGX_CONF_ERROR;
1980 }
1981
1982 copy->code = (ngx_http_script_code_pt)
1983 ngx_http_script_copy_len_code;
1984 copy->len = src[i].key.len + sizeof(": ") - 1;
1985
1986
1987 size = (sizeof(ngx_http_script_copy_code_t)
1988 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
1989 & ~(sizeof(uintptr_t) - 1);
1990
1991 copy = ngx_array_push_n(conf->headers_set, size);
1992 if (copy == NULL) {
1993 return NGX_CONF_ERROR;
1994 }
1995
1996 copy->code = ngx_http_script_copy_code;
1997 copy->len = src[i].key.len + sizeof(": ") - 1;
1998
1999 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
2000 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
2001 *p++ = ':'; *p = ' ';
2002
2003
2004 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2005
2006 sc.cf = cf;
2007 sc.source = &src[i].value;
Igor Sysoev09c684b2005-11-09 17:25:55 +00002008 sc.flushes = &conf->flushes;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002009 sc.lengths = &conf->headers_set_len;
2010 sc.values = &conf->headers_set;
2011
2012 if (ngx_http_script_compile(&sc) != NGX_OK) {
2013 return NGX_CONF_ERROR;
2014 }
2015
2016
2017 copy = ngx_array_push_n(conf->headers_set_len,
2018 sizeof(ngx_http_script_copy_code_t));
2019 if (copy == NULL) {
2020 return NGX_CONF_ERROR;
2021 }
2022
2023 copy->code = (ngx_http_script_code_pt)
2024 ngx_http_script_copy_len_code;
2025 copy->len = sizeof(CRLF) - 1;
2026
2027
2028 size = (sizeof(ngx_http_script_copy_code_t)
2029 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
2030 & ~(sizeof(uintptr_t) - 1);
2031
2032 copy = ngx_array_push_n(conf->headers_set, size);
2033 if (copy == NULL) {
2034 return NGX_CONF_ERROR;
2035 }
2036
2037 copy->code = ngx_http_script_copy_code;
2038 copy->len = sizeof(CRLF) - 1;
2039
2040 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
2041 *p++ = CR; *p = LF;
2042 }
2043
2044 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
2045 if (code == NULL) {
2046 return NGX_CONF_ERROR;
2047 }
2048
2049 *code = (uintptr_t) NULL;
2050
2051 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
2052 if (code == NULL) {
2053 return NGX_CONF_ERROR;
2054 }
2055
2056 *code = (uintptr_t) NULL;
2057 }
2058
2059 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
2060 if (code == NULL) {
2061 return NGX_CONF_ERROR;
2062 }
2063
2064 *code = (uintptr_t) NULL;
2065
2066
Igor Sysoev3338cfd2006-05-11 14:43:47 +00002067 hash.hash = &conf->headers_set_hash;
2068 hash.key = ngx_hash_key_lc;
2069 hash.max_size = 512;
2070 hash.bucket_size = ngx_cacheline_size;
2071 hash.name = "proxy_set_header_hash";
2072 hash.pool = cf->pool;
2073 hash.temp_pool = NULL;
2074
2075 if (ngx_hash_init(&hash, conf->headers_names->elts,
2076 conf->headers_names->nelts)
2077 != NGX_OK)
2078 {
2079 return NGX_CONF_ERROR;
2080 }
2081
2082
2083#if 0
Igor Sysoev24025022005-12-16 15:07:08 +00002084 conf->headers_set_hash = ngx_pcalloc(cf->pool, sizeof(ngx_hash0_t));
Igor Sysoev899b44e2005-05-12 14:58:06 +00002085 if (conf->headers_set_hash == NULL) {
2086 return NGX_CONF_ERROR;
2087 }
2088
2089 conf->headers_set_hash->max_size = 100;
2090 conf->headers_set_hash->bucket_limit = 1;
2091 conf->headers_set_hash->bucket_size = sizeof(ngx_str_t);
2092 conf->headers_set_hash->name = "proxy_headers";
2093
Igor Sysoev24025022005-12-16 15:07:08 +00002094 if (ngx_hash0_init(conf->headers_set_hash, cf->pool,
2095 conf->headers_names->elts, conf->headers_names->nelts)
2096 != NGX_OK)
Igor Sysoev899b44e2005-05-12 14:58:06 +00002097 {
2098 return NGX_CONF_ERROR;
2099 }
2100
2101 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, cf->log, 0,
2102 "proxy_headers hash size: %ui, "
2103 "max buckets per entry: %ui",
2104 conf->headers_set_hash->hash_size,
2105 conf->headers_set_hash->min_buckets);
Igor Sysoev3338cfd2006-05-11 14:43:47 +00002106#endif
Igor Sysoev899b44e2005-05-12 14:58:06 +00002107
Igor Sysoev02f742b2005-04-08 15:18:55 +00002108 return NGX_CONF_OK;
2109}
2110
2111
Igor Sysoev02f742b2005-04-08 15:18:55 +00002112static char *
2113ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2114{
Igor Sysoev899b44e2005-05-12 14:58:06 +00002115 ngx_http_proxy_loc_conf_t *plcf = conf;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002116
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002117 size_t add;
Igor Sysoev305a9d82005-12-26 17:07:48 +00002118 u_short port;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002119 ngx_str_t *value, *url;
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002120 ngx_url_t u;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002121 ngx_http_core_loc_conf_t *clcf;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002122#if (NGX_HTTP_SSL)
2123 ngx_pool_cleanup_t *cln;
2124#endif
Igor Sysoev02f742b2005-04-08 15:18:55 +00002125
Igor Sysoeva2573672005-10-05 14:46:21 +00002126 if (plcf->upstream.schema.len) {
2127 return "is duplicate";
2128 }
2129
Igor Sysoev02f742b2005-04-08 15:18:55 +00002130 value = cf->args->elts;
2131
2132 url = &value[1];
2133
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002134 if (ngx_strncasecmp(url->data, "http://", 7) == 0) {
2135 add = 7;
Igor Sysoev43f279d2005-12-18 16:02:44 +00002136 port = 80;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002137
2138 } else if (ngx_strncasecmp(url->data, "https://", 8) == 0) {
2139
2140#if (NGX_HTTP_SSL)
2141
2142 add = 8;
Igor Sysoev43f279d2005-12-18 16:02:44 +00002143 port = 443;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +00002144
2145 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
2146 if (plcf->upstream.ssl == NULL) {
2147 return NGX_CONF_ERROR;
2148 }
2149
2150 plcf->upstream.ssl->log = cf->log;
2151
2152 if (ngx_ssl_create(plcf->upstream.ssl,
2153 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1)
2154 != NGX_OK)
2155 {
2156 return NGX_CONF_ERROR;
2157 }
2158
2159 cln = ngx_pool_cleanup_add(cf->pool, 0);
2160 if (cln == NULL) {
2161 return NGX_CONF_ERROR;
2162 }
2163
2164 cln->handler = ngx_ssl_cleanup_ctx;
2165 cln->data = plcf->upstream.ssl;
2166
2167#else
2168 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2169 "https protocol requires SSL support");
2170 return NGX_CONF_ERROR;
2171#endif
2172
2173 } else {
Igor Sysoev02f742b2005-04-08 15:18:55 +00002174 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
2175 return NGX_CONF_ERROR;
2176 }
2177
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002178 ngx_memzero(&u, sizeof(ngx_url_t));
Igor Sysoev02f742b2005-04-08 15:18:55 +00002179
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002180 u.url.len = url->len - add;
2181 u.url.data = url->data + add;
2182 u.default_portn = port;
2183 u.uri_part = 1;
2184 u.upstream = 1;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002185
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002186 plcf->upstream_peers = ngx_http_upstream_add(cf, &u);
2187 if (plcf->upstream_peers == NULL) {
Igor Sysoev02f742b2005-04-08 15:18:55 +00002188 return NGX_CONF_ERROR;
Igor Sysoev02f742b2005-04-08 15:18:55 +00002189 }
2190
Igor Sysoev6f134cc2006-05-23 14:54:58 +00002191 plcf->host_header = u.host_header;
2192 plcf->port_text = u.port;
2193 plcf->upstream.uri = u.uri;
2194
Igor Sysoev43f279d2005-12-18 16:02:44 +00002195 plcf->upstream.schema.len = add;
2196 plcf->upstream.schema.data = url->data;
Igor Sysoev899b44e2005-05-12 14:58:06 +00002197
Igor Sysoev02f742b2005-04-08 15:18:55 +00002198 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
2199
2200 clcf->handler = ngx_http_proxy_handler;
2201
Igor Sysoev09c684b2005-11-09 17:25:55 +00002202 plcf->upstream.location = clcf->name;
Igor Sysoeva2573672005-10-05 14:46:21 +00002203
2204#if (NGX_PCRE)
2205
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00002206 if (clcf->regex || clcf->noname) {
Igor Sysoev09c684b2005-11-09 17:25:55 +00002207 if (plcf->upstream.uri.len) {
2208 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2209 "\"proxy_pass\" may not have URI part in "
Igor Sysoev6d16e1e2006-04-05 13:40:54 +00002210 "location given by regular expression or "
2211 "inside the \"if\" statement");
Igor Sysoev09c684b2005-11-09 17:25:55 +00002212 return NGX_CONF_ERROR;
2213 }
2214
2215 plcf->upstream.location.len = 0;
Igor Sysoeva2573672005-10-05 14:46:21 +00002216 }
2217
Igor Sysoev02f742b2005-04-08 15:18:55 +00002218#endif
2219
Igor Sysoev899b44e2005-05-12 14:58:06 +00002220 plcf->upstream.url = *url;
2221
Igor Sysoev02f742b2005-04-08 15:18:55 +00002222 if (clcf->name.data[clcf->name.len - 1] == '/') {
2223 clcf->auto_redirect = 1;
2224 }
2225
2226 return NGX_CONF_OK;
2227}
2228
2229
2230static char *
Igor Sysoev899b44e2005-05-12 14:58:06 +00002231ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
Igor Sysoev02f742b2005-04-08 15:18:55 +00002232{
Igor Sysoev899b44e2005-05-12 14:58:06 +00002233 ngx_http_proxy_loc_conf_t *plcf = conf;
2234
2235 ngx_str_t *value;
2236 ngx_array_t *vars_lengths, *vars_values;
2237 ngx_http_script_compile_t sc;
2238 ngx_http_proxy_redirect_t *pr;
2239
2240 if (plcf->redirect == 0) {
2241 return NGX_CONF_OK;
2242 }
2243
2244 value = cf->args->elts;
2245
2246 if (ngx_strcmp(value[1].data, "off") == 0) {
2247 plcf->redirect = 0;
2248 plcf->redirects = NULL;
2249 return NGX_CONF_OK;
2250 }
2251
2252 if (plcf->redirects == NULL) {
2253 plcf->redirects = ngx_array_create(cf->pool, 1,
2254 sizeof(ngx_http_proxy_redirect_t));
2255 if (plcf->redirects == NULL) {
2256 return NGX_CONF_ERROR;
2257 }
2258 }
2259
2260 pr = ngx_array_push(plcf->redirects);
2261 if (pr == NULL) {
2262 return NGX_CONF_ERROR;
2263 }
2264
2265 if (cf->args->nelts == 2 && ngx_strcmp(value[1].data, "default") == 0) {
2266 if (plcf->upstream.url.data == NULL) {
2267 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2268 "\"proxy_rewrite_location default\" must go "
2269 "after the \"proxy_pass\" directive");
2270 return NGX_CONF_ERROR;
2271 }
2272
2273 pr->handler = ngx_http_proxy_rewrite_redirect_text;
2274 pr->redirect = plcf->upstream.url;
Igor Sysoevafd7ec52006-05-29 17:28:12 +00002275
2276 if (plcf->upstream.uri.len) {
2277 pr->replacement.text = plcf->upstream.location;
2278
2279 } else {
2280 pr->replacement.text.len = 0;
2281 pr->replacement.text.data = NULL;
2282 }
Igor Sysoev899b44e2005-05-12 14:58:06 +00002283
2284 return NGX_CONF_OK;
2285 }
2286
2287 if (ngx_http_script_variables_count(&value[2]) == 0) {
2288 pr->handler = ngx_http_proxy_rewrite_redirect_text;
2289 pr->redirect = value[1];
2290 pr->replacement.text = value[2];
2291
2292 return NGX_CONF_OK;
2293 }
2294
2295 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
2296
2297 vars_lengths = NULL;
2298 vars_values = NULL;
2299
2300 sc.cf = cf;
2301 sc.source = &value[2];
2302 sc.lengths = &vars_lengths;
2303 sc.values = &vars_values;
2304 sc.complete_lengths = 1;
2305 sc.complete_values = 1;
2306
2307 if (ngx_http_script_compile(&sc) != NGX_OK) {
2308 return NGX_CONF_ERROR;
2309 }
2310
2311 pr->handler = ngx_http_proxy_rewrite_redirect_vars;
2312 pr->redirect = value[1];
2313 pr->replacement.vars.lengths = vars_lengths->elts;
2314 pr->replacement.vars.values = vars_values->elts;
2315
Igor Sysoev02f742b2005-04-08 15:18:55 +00002316 return NGX_CONF_OK;
2317}
2318
2319
2320static char *
2321ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
2322{
2323#if (NGX_FREEBSD)
2324 ssize_t *np = data;
2325
2326 if (*np >= ngx_freebsd_net_inet_tcp_sendspace) {
2327 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2328 "\"proxy_send_lowat\" must be less than %d "
2329 "(sysctl net.inet.tcp.sendspace)",
2330 ngx_freebsd_net_inet_tcp_sendspace);
2331
2332 return NGX_CONF_ERROR;
2333 }
2334
2335#elif !(NGX_HAVE_SO_SNDLOWAT)
2336 ssize_t *np = data;
2337
2338 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
2339 "\"proxy_send_lowat\" is not supported, ignored");
2340
2341 *np = 0;
2342
2343#endif
2344
2345 return NGX_CONF_OK;
2346}