58
|
1
|
126
|
2 /*
|
58
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_http.h>
|
|
10
|
|
11
|
|
12 typedef struct ngx_http_proxy_redirect_s ngx_http_proxy_redirect_t;
|
|
13
|
|
14 typedef ngx_int_t (*ngx_http_proxy_redirect_pt)(ngx_http_request_t *r,
|
|
15 ngx_table_elt_t *h, size_t prefix, ngx_http_proxy_redirect_t *pr);
|
|
16
|
|
17 struct ngx_http_proxy_redirect_s {
|
200
|
18 ngx_http_proxy_redirect_pt handler;
|
|
19 ngx_str_t redirect;
|
58
|
20
|
|
21 union {
|
200
|
22 ngx_str_t text;
|
58
|
23
|
|
24 struct {
|
200
|
25 void *lengths;
|
|
26 void *values;
|
58
|
27 } vars;
|
|
28
|
200
|
29 void *regex;
|
58
|
30 } replacement;
|
|
31 };
|
|
32
|
|
33
|
|
34 typedef struct {
|
348
|
35 ngx_str_t host_header;
|
|
36 ngx_str_t port;
|
|
37 ngx_str_t uri;
|
|
38 } ngx_http_proxy_vars_t;
|
|
39
|
|
40
|
|
41 typedef struct {
|
200
|
42 ngx_http_upstream_conf_t upstream;
|
|
43
|
|
44 ngx_array_t *flushes;
|
|
45 ngx_array_t *body_set_len;
|
|
46 ngx_array_t *body_set;
|
|
47 ngx_array_t *headers_set_len;
|
|
48 ngx_array_t *headers_set;
|
|
49 ngx_hash_t headers_set_hash;
|
|
50
|
|
51 ngx_array_t *headers_source;
|
|
52 ngx_array_t *headers_names;
|
|
53
|
348
|
54 ngx_array_t *proxy_lengths;
|
|
55 ngx_array_t *proxy_values;
|
|
56
|
200
|
57 ngx_array_t *redirects;
|
|
58
|
|
59 ngx_str_t body_source;
|
|
60
|
|
61 ngx_str_t method;
|
348
|
62 ngx_str_t location;
|
|
63 ngx_str_t url;
|
|
64
|
|
65 ngx_http_proxy_vars_t vars;
|
200
|
66
|
|
67 ngx_flag_t redirect;
|
298
|
68
|
|
69 ngx_uint_t headers_hash_max_size;
|
|
70 ngx_uint_t headers_hash_bucket_size;
|
58
|
71 } ngx_http_proxy_loc_conf_t;
|
|
72
|
|
73
|
|
74 typedef struct {
|
200
|
75 ngx_uint_t status;
|
|
76 ngx_uint_t status_count;
|
|
77 u_char *status_start;
|
|
78 u_char *status_end;
|
|
79
|
348
|
80 ngx_http_proxy_vars_t vars;
|
|
81
|
200
|
82 size_t internal_body_length;
|
58
|
83 } ngx_http_proxy_ctx_t;
|
|
84
|
|
85
|
|
86 #define NGX_HTTP_PROXY_PARSE_NO_HEADER 20
|
|
87
|
|
88
|
348
|
89 static ngx_int_t ngx_http_proxy_eval(ngx_http_request_t *r,
|
|
90 ngx_http_proxy_ctx_t *ctx, ngx_http_proxy_loc_conf_t *plcf);
|
58
|
91 static ngx_int_t ngx_http_proxy_create_request(ngx_http_request_t *r);
|
|
92 static ngx_int_t ngx_http_proxy_reinit_request(ngx_http_request_t *r);
|
|
93 static ngx_int_t ngx_http_proxy_process_status_line(ngx_http_request_t *r);
|
|
94 static ngx_int_t ngx_http_proxy_parse_status_line(ngx_http_request_t *r,
|
348
|
95 ngx_http_proxy_ctx_t *ctx);
|
58
|
96 static ngx_int_t ngx_http_proxy_process_header(ngx_http_request_t *r);
|
|
97 static void ngx_http_proxy_abort_request(ngx_http_request_t *r);
|
|
98 static void ngx_http_proxy_finalize_request(ngx_http_request_t *r,
|
|
99 ngx_int_t rc);
|
|
100
|
122
|
101 static ngx_int_t ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
102 ngx_http_variable_value_t *v, uintptr_t data);
|
|
103 static ngx_int_t ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
104 ngx_http_variable_value_t *v, uintptr_t data);
|
|
105 static ngx_int_t
|
58
|
106 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
107 ngx_http_variable_value_t *v, uintptr_t data);
|
126
|
108 static ngx_int_t
|
|
109 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
110 ngx_http_variable_value_t *v, uintptr_t data);
|
58
|
111 static ngx_int_t ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r,
|
|
112 ngx_table_elt_t *h, size_t prefix);
|
|
113
|
|
114 static ngx_int_t ngx_http_proxy_add_variables(ngx_conf_t *cf);
|
|
115 static void *ngx_http_proxy_create_loc_conf(ngx_conf_t *cf);
|
|
116 static char *ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf,
|
|
117 void *parent, void *child);
|
|
118
|
|
119 static char *ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
120 void *conf);
|
|
121 static char *ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
122 void *conf);
|
320
|
123 static char *ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
124 void *conf);
|
58
|
125
|
|
126 static char *ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data);
|
|
127
|
260
|
128 static char *ngx_http_proxy_upstream_max_fails_unsupported(ngx_conf_t *cf,
|
|
129 ngx_command_t *cmd, void *conf);
|
|
130 static char *ngx_http_proxy_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
|
|
131 ngx_command_t *cmd, void *conf);
|
|
132
|
348
|
133 #if (NGX_HTTP_SSL)
|
|
134 static ngx_int_t ngx_http_proxy_set_ssl(ngx_conf_t *cf,
|
|
135 ngx_http_proxy_loc_conf_t *plcf);
|
|
136 #endif
|
|
137 static ngx_int_t ngx_http_proxy_set_vars(ngx_pool_t *pool, ngx_url_t *u,
|
|
138 ngx_http_proxy_vars_t *v);
|
|
139
|
58
|
140
|
|
141 static ngx_conf_post_t ngx_http_proxy_lowat_post =
|
|
142 { ngx_http_proxy_lowat_check };
|
|
143
|
|
144
|
|
145 static ngx_conf_bitmask_t ngx_http_proxy_next_upstream_masks[] = {
|
|
146 { ngx_string("error"), NGX_HTTP_UPSTREAM_FT_ERROR },
|
|
147 { ngx_string("timeout"), NGX_HTTP_UPSTREAM_FT_TIMEOUT },
|
|
148 { ngx_string("invalid_header"), NGX_HTTP_UPSTREAM_FT_INVALID_HEADER },
|
|
149 { ngx_string("http_500"), NGX_HTTP_UPSTREAM_FT_HTTP_500 },
|
172
|
150 { ngx_string("http_503"), NGX_HTTP_UPSTREAM_FT_HTTP_503 },
|
58
|
151 { ngx_string("http_404"), NGX_HTTP_UPSTREAM_FT_HTTP_404 },
|
214
|
152 { ngx_string("off"), NGX_HTTP_UPSTREAM_FT_OFF },
|
58
|
153 { ngx_null_string, 0 }
|
|
154 };
|
|
155
|
|
156
|
|
157 static ngx_command_t ngx_http_proxy_commands[] = {
|
|
158
|
|
159 { ngx_string("proxy_pass"),
|
256
|
160 NGX_HTTP_LOC_CONF|NGX_HTTP_LIF_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_TAKE1,
|
58
|
161 ngx_http_proxy_pass,
|
|
162 NGX_HTTP_LOC_CONF_OFFSET,
|
|
163 0,
|
|
164 NULL },
|
|
165
|
|
166 { ngx_string("proxy_redirect"),
|
|
167 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE12,
|
|
168 ngx_http_proxy_redirect,
|
|
169 NGX_HTTP_LOC_CONF_OFFSET,
|
|
170 0,
|
|
171 NULL },
|
|
172
|
318
|
173 { ngx_string("proxy_store"),
|
320
|
174 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
175 ngx_http_proxy_store,
|
|
176 NGX_HTTP_LOC_CONF_OFFSET,
|
|
177 0,
|
|
178 NULL },
|
|
179
|
|
180 { ngx_string("proxy_store_access"),
|
318
|
181 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
|
|
182 ngx_conf_set_access_slot,
|
|
183 NGX_HTTP_LOC_CONF_OFFSET,
|
320
|
184 offsetof(ngx_http_proxy_loc_conf_t, upstream.store_access),
|
318
|
185 NULL },
|
|
186
|
130
|
187 { ngx_string("proxy_buffering"),
|
|
188 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
189 ngx_conf_set_flag_slot,
|
|
190 NGX_HTTP_LOC_CONF_OFFSET,
|
|
191 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffering),
|
|
192 NULL },
|
|
193
|
178
|
194 { ngx_string("proxy_ignore_client_abort"),
|
|
195 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
196 ngx_conf_set_flag_slot,
|
|
197 NGX_HTTP_LOC_CONF_OFFSET,
|
|
198 offsetof(ngx_http_proxy_loc_conf_t, upstream.ignore_client_abort),
|
|
199 NULL },
|
|
200
|
58
|
201 { ngx_string("proxy_connect_timeout"),
|
|
202 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
203 ngx_conf_set_msec_slot,
|
|
204 NGX_HTTP_LOC_CONF_OFFSET,
|
|
205 offsetof(ngx_http_proxy_loc_conf_t, upstream.connect_timeout),
|
|
206 NULL },
|
|
207
|
|
208 { ngx_string("proxy_send_timeout"),
|
|
209 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
210 ngx_conf_set_msec_slot,
|
|
211 NGX_HTTP_LOC_CONF_OFFSET,
|
|
212 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_timeout),
|
|
213 NULL },
|
|
214
|
|
215 { ngx_string("proxy_send_lowat"),
|
|
216 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
217 ngx_conf_set_size_slot,
|
|
218 NGX_HTTP_LOC_CONF_OFFSET,
|
|
219 offsetof(ngx_http_proxy_loc_conf_t, upstream.send_lowat),
|
|
220 &ngx_http_proxy_lowat_post },
|
|
221
|
206
|
222 { ngx_string("proxy_intercept_errors"),
|
|
223 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
224 ngx_conf_set_flag_slot,
|
|
225 NGX_HTTP_LOC_CONF_OFFSET,
|
|
226 offsetof(ngx_http_proxy_loc_conf_t, upstream.intercept_errors),
|
|
227 NULL },
|
|
228
|
58
|
229 { ngx_string("proxy_set_header"),
|
|
230 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
198
|
231 ngx_conf_set_keyval_slot,
|
58
|
232 NGX_HTTP_LOC_CONF_OFFSET,
|
|
233 offsetof(ngx_http_proxy_loc_conf_t, headers_source),
|
|
234 NULL },
|
|
235
|
298
|
236 { ngx_string("proxy_headers_hash_max_size"),
|
|
237 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
238 ngx_conf_set_num_slot,
|
|
239 NGX_HTTP_LOC_CONF_OFFSET,
|
|
240 offsetof(ngx_http_proxy_loc_conf_t, headers_hash_max_size),
|
|
241 NULL },
|
|
242
|
|
243 { ngx_string("proxy_headers_hash_bucket_size"),
|
|
244 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
245 ngx_conf_set_num_slot,
|
|
246 NGX_HTTP_LOC_CONF_OFFSET,
|
|
247 offsetof(ngx_http_proxy_loc_conf_t, headers_hash_bucket_size),
|
|
248 NULL },
|
|
249
|
126
|
250 { ngx_string("proxy_set_body"),
|
|
251 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
252 ngx_conf_set_str_slot,
|
|
253 NGX_HTTP_LOC_CONF_OFFSET,
|
|
254 offsetof(ngx_http_proxy_loc_conf_t, body_source),
|
|
255 NULL },
|
|
256
|
58
|
257 { ngx_string("proxy_method"),
|
|
258 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
110
|
259 ngx_conf_set_str_slot,
|
58
|
260 NGX_HTTP_LOC_CONF_OFFSET,
|
110
|
261 offsetof(ngx_http_proxy_loc_conf_t, method),
|
|
262 NULL },
|
58
|
263
|
|
264 { ngx_string("proxy_pass_request_headers"),
|
|
265 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
266 ngx_conf_set_flag_slot,
|
|
267 NGX_HTTP_LOC_CONF_OFFSET,
|
|
268 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_headers),
|
|
269 NULL },
|
|
270
|
|
271 { ngx_string("proxy_pass_request_body"),
|
|
272 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
273 ngx_conf_set_flag_slot,
|
|
274 NGX_HTTP_LOC_CONF_OFFSET,
|
|
275 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_request_body),
|
|
276 NULL },
|
|
277
|
130
|
278 { ngx_string("proxy_buffer_size"),
|
|
279 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
280 ngx_conf_set_size_slot,
|
|
281 NGX_HTTP_LOC_CONF_OFFSET,
|
|
282 offsetof(ngx_http_proxy_loc_conf_t, upstream.buffer_size),
|
|
283 NULL },
|
|
284
|
58
|
285 { ngx_string("proxy_read_timeout"),
|
|
286 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
287 ngx_conf_set_msec_slot,
|
|
288 NGX_HTTP_LOC_CONF_OFFSET,
|
|
289 offsetof(ngx_http_proxy_loc_conf_t, upstream.read_timeout),
|
|
290 NULL },
|
|
291
|
|
292 { ngx_string("proxy_buffers"),
|
|
293 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
|
294 ngx_conf_set_bufs_slot,
|
|
295 NGX_HTTP_LOC_CONF_OFFSET,
|
|
296 offsetof(ngx_http_proxy_loc_conf_t, upstream.bufs),
|
|
297 NULL },
|
|
298
|
|
299 { ngx_string("proxy_busy_buffers_size"),
|
|
300 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
301 ngx_conf_set_size_slot,
|
|
302 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
303 offsetof(ngx_http_proxy_loc_conf_t, upstream.busy_buffers_size_conf),
|
58
|
304 NULL },
|
|
305
|
|
306 { ngx_string("proxy_temp_path"),
|
|
307 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1234,
|
|
308 ngx_conf_set_path_slot,
|
|
309 NGX_HTTP_LOC_CONF_OFFSET,
|
|
310 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_path),
|
|
311 (void *) ngx_garbage_collector_temp_handler },
|
|
312
|
|
313 { ngx_string("proxy_max_temp_file_size"),
|
|
314 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
315 ngx_conf_set_size_slot,
|
|
316 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
317 offsetof(ngx_http_proxy_loc_conf_t, upstream.max_temp_file_size_conf),
|
58
|
318 NULL },
|
|
319
|
|
320 { ngx_string("proxy_temp_file_write_size"),
|
|
321 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
322 ngx_conf_set_size_slot,
|
|
323 NGX_HTTP_LOC_CONF_OFFSET,
|
78
|
324 offsetof(ngx_http_proxy_loc_conf_t, upstream.temp_file_write_size_conf),
|
58
|
325 NULL },
|
|
326
|
|
327 { ngx_string("proxy_next_upstream"),
|
96
|
328 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
|
58
|
329 ngx_conf_set_bitmask_slot,
|
|
330 NGX_HTTP_LOC_CONF_OFFSET,
|
|
331 offsetof(ngx_http_proxy_loc_conf_t, upstream.next_upstream),
|
|
332 &ngx_http_proxy_next_upstream_masks },
|
|
333
|
110
|
334 { ngx_string("proxy_upstream_max_fails"),
|
|
335 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
260
|
336 ngx_http_proxy_upstream_max_fails_unsupported,
|
|
337 0,
|
|
338 0,
|
110
|
339 NULL },
|
|
340
|
|
341 { ngx_string("proxy_upstream_fail_timeout"),
|
|
342 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
260
|
343 ngx_http_proxy_upstream_fail_timeout_unsupported,
|
|
344 0,
|
|
345 0,
|
110
|
346 NULL },
|
|
347
|
198
|
348 { ngx_string("proxy_pass_header"),
|
58
|
349 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
198
|
350 ngx_conf_set_str_array_slot,
|
58
|
351 NGX_HTTP_LOC_CONF_OFFSET,
|
198
|
352 offsetof(ngx_http_proxy_loc_conf_t, upstream.pass_headers),
|
58
|
353 NULL },
|
|
354
|
198
|
355 { ngx_string("proxy_hide_header"),
|
58
|
356 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
198
|
357 ngx_conf_set_str_array_slot,
|
58
|
358 NGX_HTTP_LOC_CONF_OFFSET,
|
198
|
359 offsetof(ngx_http_proxy_loc_conf_t, upstream.hide_headers),
|
58
|
360 NULL },
|
|
361
|
398
|
362 #if (NGX_HTTP_SSL)
|
|
363
|
|
364 { ngx_string("proxy_ssl_session_reuse"),
|
|
365 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
|
|
366 ngx_conf_set_flag_slot,
|
|
367 NGX_HTTP_LOC_CONF_OFFSET,
|
|
368 offsetof(ngx_http_proxy_loc_conf_t, upstream.ssl_session_reuse),
|
|
369 NULL },
|
|
370
|
|
371 #endif
|
|
372
|
58
|
373 ngx_null_command
|
|
374 };
|
|
375
|
|
376
|
216
|
377 static ngx_http_module_t ngx_http_proxy_module_ctx = {
|
58
|
378 ngx_http_proxy_add_variables, /* preconfiguration */
|
|
379 NULL, /* postconfiguration */
|
|
380
|
|
381 NULL, /* create main configuration */
|
|
382 NULL, /* init main configuration */
|
|
383
|
|
384 NULL, /* create server configuration */
|
|
385 NULL, /* merge server configuration */
|
|
386
|
|
387 ngx_http_proxy_create_loc_conf, /* create location configration */
|
|
388 ngx_http_proxy_merge_loc_conf /* merge location configration */
|
|
389 };
|
|
390
|
|
391
|
|
392 ngx_module_t ngx_http_proxy_module = {
|
|
393 NGX_MODULE_V1,
|
|
394 &ngx_http_proxy_module_ctx, /* module context */
|
|
395 ngx_http_proxy_commands, /* module directives */
|
|
396 NGX_HTTP_MODULE, /* module type */
|
90
|
397 NULL, /* init master */
|
58
|
398 NULL, /* init module */
|
90
|
399 NULL, /* init process */
|
|
400 NULL, /* init thread */
|
|
401 NULL, /* exit thread */
|
|
402 NULL, /* exit process */
|
|
403 NULL, /* exit master */
|
|
404 NGX_MODULE_V1_PADDING
|
58
|
405 };
|
|
406
|
|
407
|
|
408 static char ngx_http_proxy_version[] = " HTTP/1.0" CRLF;
|
|
409
|
|
410
|
198
|
411 static ngx_keyval_t ngx_http_proxy_headers[] = {
|
|
412 { ngx_string("Host"), ngx_string("$proxy_host") },
|
|
413 { ngx_string("Connection"), ngx_string("close") },
|
|
414 { ngx_string("Keep-Alive"), ngx_string("") },
|
384
|
415 { ngx_string("Expect"), ngx_string("") },
|
198
|
416 { ngx_null_string, ngx_null_string }
|
|
417 };
|
|
418
|
|
419
|
|
420 static ngx_str_t ngx_http_proxy_hide_headers[] = {
|
|
421 ngx_string("Date"),
|
|
422 ngx_string("Server"),
|
|
423 ngx_string("X-Pad"),
|
|
424 ngx_string("X-Accel-Expires"),
|
|
425 ngx_string("X-Accel-Redirect"),
|
|
426 ngx_string("X-Accel-Limit-Rate"),
|
364
|
427 ngx_string("X-Accel-Buffering"),
|
|
428 ngx_string("X-Accel-Charset"),
|
198
|
429 ngx_null_string
|
58
|
430 };
|
|
431
|
|
432
|
|
433 static ngx_http_variable_t ngx_http_proxy_vars[] = {
|
|
434
|
186
|
435 { ngx_string("proxy_host"), NULL, ngx_http_proxy_host_variable, 0,
|
366
|
436 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
437
|
186
|
438 { ngx_string("proxy_port"), NULL, ngx_http_proxy_port_variable, 0,
|
366
|
439 NGX_HTTP_VAR_CHANGEABLE|NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
440
|
186
|
441 { ngx_string("proxy_add_x_forwarded_for"), NULL,
|
132
|
442 ngx_http_proxy_add_x_forwarded_for_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
443
|
|
444 #if 0
|
186
|
445 { ngx_string("proxy_add_via"), NULL, NULL, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
58
|
446 #endif
|
|
447
|
186
|
448 { ngx_string("proxy_internal_body_length"), NULL,
|
132
|
449 ngx_http_proxy_internal_body_length_variable, 0, NGX_HTTP_VAR_NOHASH, 0 },
|
126
|
450
|
186
|
451 { ngx_null_string, NULL, NULL, 0, 0, 0 }
|
58
|
452 };
|
|
453
|
|
454
|
|
455 static ngx_int_t
|
|
456 ngx_http_proxy_handler(ngx_http_request_t *r)
|
126
|
457 {
|
58
|
458 ngx_int_t rc;
|
|
459 ngx_http_upstream_t *u;
|
348
|
460 ngx_http_proxy_ctx_t *ctx;
|
58
|
461 ngx_http_proxy_loc_conf_t *plcf;
|
126
|
462
|
58
|
463 u = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_t));
|
|
464 if (u == NULL) {
|
|
465 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
466 }
|
126
|
467
|
348
|
468 r->upstream = u;
|
|
469
|
|
470 ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_proxy_ctx_t));
|
|
471 if (ctx == NULL) {
|
|
472 return NGX_ERROR;
|
|
473 }
|
|
474
|
|
475 ngx_http_set_ctx(r, ctx, ngx_http_proxy_module);
|
|
476
|
|
477 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
478
|
|
479 if (plcf->proxy_lengths == 0) {
|
|
480 ctx->vars = plcf->vars;
|
|
481 u->schema = plcf->upstream.schema;
|
|
482 #if (NGX_HTTP_SSL)
|
|
483 u->ssl = (plcf->upstream.ssl != NULL);
|
|
484 #endif
|
|
485
|
|
486 } else {
|
|
487 if (ngx_http_proxy_eval(r, ctx, plcf) != NGX_OK) {
|
|
488 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
489 }
|
|
490 }
|
|
491
|
58
|
492 u->peer.log = r->connection->log;
|
|
493 u->peer.log_error = NGX_ERROR_ERR;
|
|
494 #if (NGX_THREADS)
|
|
495 u->peer.lock = &r->connection->lock;
|
|
496 #endif
|
|
497
|
|
498 u->output.tag = (ngx_buf_tag_t) &ngx_http_proxy_module;
|
|
499
|
|
500 u->conf = &plcf->upstream;
|
|
501
|
|
502 u->create_request = ngx_http_proxy_create_request;
|
|
503 u->reinit_request = ngx_http_proxy_reinit_request;
|
|
504 u->process_header = ngx_http_proxy_process_status_line;
|
|
505 u->abort_request = ngx_http_proxy_abort_request;
|
|
506 u->finalize_request = ngx_http_proxy_finalize_request;
|
|
507
|
|
508 if (plcf->redirects) {
|
|
509 u->rewrite_redirect = ngx_http_proxy_rewrite_redirect;
|
|
510 }
|
|
511
|
198
|
512 u->buffering = plcf->upstream.buffering;
|
|
513
|
|
514 u->pipe = ngx_pcalloc(r->pool, sizeof(ngx_event_pipe_t));
|
|
515 if (u->pipe == NULL) {
|
|
516 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
130
|
517 }
|
58
|
518
|
198
|
519 u->pipe->input_filter = ngx_event_pipe_copy_input_filter;
|
|
520
|
58
|
521 u->accel = 1;
|
126
|
522
|
58
|
523 rc = ngx_http_read_client_request_body(r, ngx_http_upstream_init);
|
|
524
|
|
525 if (rc >= NGX_HTTP_SPECIAL_RESPONSE) {
|
|
526 return rc;
|
|
527 }
|
|
528
|
|
529 return NGX_DONE;
|
|
530 }
|
|
531
|
|
532
|
|
533 static ngx_int_t
|
348
|
534 ngx_http_proxy_eval(ngx_http_request_t *r, ngx_http_proxy_ctx_t *ctx,
|
|
535 ngx_http_proxy_loc_conf_t *plcf)
|
|
536 {
|
|
537 size_t add;
|
|
538 u_short port;
|
|
539 ngx_str_t proxy;
|
|
540 ngx_url_t u;
|
|
541
|
|
542 if (ngx_http_script_run(r, &proxy, plcf->proxy_lengths->elts, 0,
|
|
543 plcf->proxy_values->elts)
|
|
544 == NULL)
|
|
545 {
|
|
546 return NGX_ERROR;
|
|
547 }
|
|
548
|
|
549 if (ngx_strncasecmp(proxy.data, (u_char *) "http://", 7) == 0) {
|
|
550
|
|
551 add = 7;
|
|
552 port = 80;
|
|
553
|
|
554 #if (NGX_HTTP_SSL)
|
|
555
|
|
556 } else if (ngx_strncasecmp(proxy.data, (u_char *) "https://", 8) == 0) {
|
|
557
|
|
558 add = 8;
|
|
559 port = 443;
|
|
560 r->upstream->ssl = 1;
|
|
561
|
|
562 #endif
|
|
563
|
|
564 } else {
|
|
565 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
566 "invalid URL prefix in \"%V\"", &proxy);
|
|
567 return NGX_ERROR;
|
|
568 }
|
|
569
|
|
570 r->upstream->schema.len = add;
|
|
571 r->upstream->schema.data = proxy.data;
|
|
572
|
|
573 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
574
|
|
575 u.url.len = proxy.len - add;
|
|
576 u.url.data = proxy.data + add;
|
|
577 u.default_port = port;
|
|
578 u.uri_part = 1;
|
|
579 u.no_resolve = 1;
|
|
580
|
|
581 if (ngx_parse_url(r->pool, &u) != NGX_OK) {
|
|
582 if (u.err) {
|
|
583 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
584 "%s in upstream \"%V\"", u.err, &u.url);
|
|
585 }
|
|
586
|
|
587 return NGX_ERROR;
|
|
588 }
|
|
589
|
|
590 if (ngx_http_proxy_set_vars(r->pool, &u, &ctx->vars) != NGX_OK) {
|
|
591 return NGX_ERROR;
|
|
592 }
|
|
593
|
|
594 r->upstream->resolved = ngx_pcalloc(r->pool,
|
|
595 sizeof(ngx_http_upstream_resolved_t));
|
|
596 if (r->upstream->resolved == NULL) {
|
|
597 return NGX_ERROR;
|
|
598 }
|
|
599
|
|
600 r->upstream->resolved->host = u.host;
|
|
601 r->upstream->resolved->port = (in_port_t) (u.no_port ? u.default_port:
|
|
602 u.port);
|
366
|
603 r->upstream->resolved->default_port = u.default_port;
|
348
|
604
|
|
605 return NGX_OK;
|
|
606 }
|
|
607
|
|
608
|
|
609 static ngx_int_t
|
58
|
610 ngx_http_proxy_create_request(ngx_http_request_t *r)
|
|
611 {
|
126
|
612 size_t len, loc_len, body_len;
|
58
|
613 uintptr_t escape;
|
|
614 ngx_buf_t *b;
|
198
|
615 ngx_str_t method;
|
|
616 ngx_uint_t i, unparsed_uri;
|
58
|
617 ngx_chain_t *cl, *body;
|
|
618 ngx_list_part_t *part;
|
|
619 ngx_table_elt_t *header;
|
|
620 ngx_http_upstream_t *u;
|
348
|
621 ngx_http_proxy_ctx_t *ctx;
|
58
|
622 ngx_http_script_code_pt code;
|
|
623 ngx_http_script_engine_t e, le;
|
|
624 ngx_http_proxy_loc_conf_t *plcf;
|
|
625 ngx_http_script_len_code_pt lcode;
|
|
626
|
|
627 u = r->upstream;
|
|
628
|
|
629 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
630
|
110
|
631 if (u->method.len) {
|
|
632 /* HEAD was changed to GET to cache response */
|
|
633 method = u->method;
|
|
634 method.len++;
|
|
635
|
|
636 } else if (plcf->method.len) {
|
|
637 method = plcf->method;
|
|
638
|
58
|
639 } else {
|
110
|
640 method = r->method_name;
|
|
641 method.len++;
|
58
|
642 }
|
|
643
|
352
|
644 len = method.len + sizeof(ngx_http_proxy_version) - 1 + sizeof(CRLF) - 1;
|
|
645
|
58
|
646 escape = 0;
|
348
|
647 loc_len = 0;
|
|
648 unparsed_uri = 0;
|
|
649
|
|
650 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
651
|
|
652 if (plcf->proxy_lengths) {
|
352
|
653 len += ctx->vars.uri.len;
|
348
|
654
|
|
655 } else if (ctx->vars.uri.len == 0 && r->valid_unparsed_uri && r == r->main)
|
|
656 {
|
158
|
657 unparsed_uri = 1;
|
352
|
658 len += r->unparsed_uri.len;
|
58
|
659
|
|
660 } else {
|
348
|
661 loc_len = (r->valid_location && ctx->vars.uri.len) ?
|
|
662 plcf->location.len : 0;
|
|
663
|
336
|
664 if (r->quoted_uri || r->internal) {
|
58
|
665 escape = 2 * ngx_escape_uri(NULL, r->uri.data + loc_len,
|
|
666 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
667 }
|
|
668
|
354
|
669 len += ctx->vars.uri.len + r->uri.len - loc_len + escape
|
|
670 + sizeof("?") - 1 + r->args.len;
|
58
|
671 }
|
|
672
|
340
|
673 ngx_http_script_flush_no_cacheable_variables(r, plcf->flushes);
|
122
|
674
|
126
|
675 if (plcf->body_set_len) {
|
|
676 le.ip = plcf->body_set_len->elts;
|
|
677 le.request = r;
|
|
678 le.flushed = 1;
|
|
679 body_len = 0;
|
|
680
|
|
681 while (*(uintptr_t *) le.ip) {
|
|
682 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
683 body_len += lcode(&le);
|
|
684 }
|
|
685
|
348
|
686 ctx->internal_body_length = body_len;
|
126
|
687 len += body_len;
|
|
688 }
|
|
689
|
58
|
690 le.ip = plcf->headers_set_len->elts;
|
|
691 le.request = r;
|
122
|
692 le.flushed = 1;
|
58
|
693
|
|
694 while (*(uintptr_t *) le.ip) {
|
|
695 while (*(uintptr_t *) le.ip) {
|
|
696 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
697 len += lcode(&le);
|
|
698 }
|
|
699 le.ip += sizeof(uintptr_t);
|
|
700 }
|
|
701
|
|
702
|
|
703 if (plcf->upstream.pass_request_headers) {
|
|
704 part = &r->headers_in.headers.part;
|
|
705 header = part->elts;
|
|
706
|
|
707 for (i = 0; /* void */; i++) {
|
|
708
|
|
709 if (i >= part->nelts) {
|
|
710 if (part->next == NULL) {
|
|
711 break;
|
|
712 }
|
|
713
|
|
714 part = part->next;
|
|
715 header = part->elts;
|
126
|
716 i = 0;
|
58
|
717 }
|
|
718
|
198
|
719 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
|
|
720 header[i].lowcase_key, header[i].key.len))
|
58
|
721 {
|
|
722 continue;
|
|
723 }
|
|
724
|
|
725 len += header[i].key.len + sizeof(": ") - 1
|
|
726 + header[i].value.len + sizeof(CRLF) - 1;
|
|
727 }
|
|
728 }
|
|
729
|
|
730
|
|
731 b = ngx_create_temp_buf(r->pool, len);
|
|
732 if (b == NULL) {
|
|
733 return NGX_ERROR;
|
|
734 }
|
|
735
|
|
736 cl = ngx_alloc_chain_link(r->pool);
|
|
737 if (cl == NULL) {
|
|
738 return NGX_ERROR;
|
|
739 }
|
|
740
|
|
741 cl->buf = b;
|
|
742
|
|
743
|
|
744 /* the request line */
|
|
745
|
122
|
746 b->last = ngx_copy(b->last, method.data, method.len);
|
58
|
747
|
104
|
748 u->uri.data = b->last;
|
|
749
|
348
|
750 if (plcf->proxy_lengths) {
|
|
751 b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
|
|
752
|
|
753 } else if (unparsed_uri) {
|
122
|
754 b->last = ngx_copy(b->last, r->unparsed_uri.data, r->unparsed_uri.len);
|
|
755
|
58
|
756 } else {
|
104
|
757 if (r->valid_location) {
|
348
|
758 b->last = ngx_copy(b->last, ctx->vars.uri.data, ctx->vars.uri.len);
|
104
|
759 }
|
100
|
760
|
58
|
761 if (escape) {
|
|
762 ngx_escape_uri(b->last, r->uri.data + loc_len,
|
|
763 r->uri.len - loc_len, NGX_ESCAPE_URI);
|
|
764 b->last += r->uri.len - loc_len + escape;
|
|
765
|
126
|
766 } else {
|
122
|
767 b->last = ngx_copy(b->last, r->uri.data + loc_len,
|
|
768 r->uri.len - loc_len);
|
58
|
769 }
|
|
770
|
|
771 if (r->args.len > 0) {
|
|
772 *b->last++ = '?';
|
122
|
773 b->last = ngx_copy(b->last, r->args.data, r->args.len);
|
58
|
774 }
|
|
775 }
|
|
776
|
104
|
777 u->uri.len = b->last - u->uri.data;
|
|
778
|
58
|
779 b->last = ngx_cpymem(b->last, ngx_http_proxy_version,
|
|
780 sizeof(ngx_http_proxy_version) - 1);
|
|
781
|
126
|
782 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
58
|
783
|
|
784 e.ip = plcf->headers_set->elts;
|
|
785 e.pos = b->last;
|
|
786 e.request = r;
|
122
|
787 e.flushed = 1;
|
58
|
788
|
|
789 le.ip = plcf->headers_set_len->elts;
|
|
790
|
|
791 while (*(uintptr_t *) le.ip) {
|
|
792 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
64
|
793
|
|
794 /* skip the header line name length */
|
|
795 (void) lcode(&le);
|
58
|
796
|
|
797 if (*(ngx_http_script_len_code_pt *) le.ip) {
|
|
798
|
|
799 for (len = 0; *(uintptr_t *) le.ip; len += lcode(&le)) {
|
|
800 lcode = *(ngx_http_script_len_code_pt *) le.ip;
|
|
801 }
|
|
802
|
|
803 e.skip = (len == sizeof(CRLF) - 1) ? 1 : 0;
|
|
804
|
|
805 } else {
|
|
806 e.skip = 0;
|
|
807 }
|
|
808
|
|
809 le.ip += sizeof(uintptr_t);
|
|
810
|
|
811 while (*(uintptr_t *) e.ip) {
|
|
812 code = *(ngx_http_script_code_pt *) e.ip;
|
|
813 code((ngx_http_script_engine_t *) &e);
|
126
|
814 }
|
58
|
815 e.ip += sizeof(uintptr_t);
|
|
816 }
|
|
817
|
|
818 b->last = e.pos;
|
|
819
|
|
820
|
|
821 if (plcf->upstream.pass_request_headers) {
|
|
822 part = &r->headers_in.headers.part;
|
|
823 header = part->elts;
|
126
|
824
|
58
|
825 for (i = 0; /* void */; i++) {
|
|
826
|
|
827 if (i >= part->nelts) {
|
|
828 if (part->next == NULL) {
|
|
829 break;
|
|
830 }
|
|
831
|
|
832 part = part->next;
|
|
833 header = part->elts;
|
126
|
834 i = 0;
|
58
|
835 }
|
|
836
|
198
|
837 if (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
|
|
838 header[i].lowcase_key, header[i].key.len))
|
58
|
839 {
|
|
840 continue;
|
|
841 }
|
|
842
|
122
|
843 b->last = ngx_copy(b->last, header[i].key.data, header[i].key.len);
|
58
|
844
|
|
845 *b->last++ = ':'; *b->last++ = ' ';
|
|
846
|
122
|
847 b->last = ngx_copy(b->last, header[i].value.data,
|
|
848 header[i].value.len);
|
58
|
849
|
|
850 *b->last++ = CR; *b->last++ = LF;
|
|
851
|
|
852 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
853 "http proxy header: \"%V: %V\"",
|
|
854 &header[i].key, &header[i].value);
|
|
855 }
|
|
856 }
|
|
857
|
126
|
858
|
58
|
859 /* add "\r\n" at the header end */
|
|
860 *b->last++ = CR; *b->last++ = LF;
|
|
861
|
126
|
862 if (plcf->body_set) {
|
|
863 e.ip = plcf->body_set->elts;
|
|
864 e.pos = b->last;
|
|
865
|
|
866 while (*(uintptr_t *) e.ip) {
|
|
867 code = *(ngx_http_script_code_pt *) e.ip;
|
|
868 code((ngx_http_script_engine_t *) &e);
|
|
869 }
|
128
|
870
|
|
871 b->last = e.pos;
|
126
|
872 }
|
|
873
|
358
|
874 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
875 "http proxy header:\n\"%*s\"",
|
|
876 (size_t) (b->last - b->pos), b->pos);
|
58
|
877
|
126
|
878 if (plcf->body_set == NULL && plcf->upstream.pass_request_body) {
|
58
|
879
|
|
880 body = u->request_bufs;
|
|
881 u->request_bufs = cl;
|
|
882
|
|
883 while (body) {
|
|
884 b = ngx_alloc_buf(r->pool);
|
|
885 if (b == NULL) {
|
|
886 return NGX_ERROR;
|
|
887 }
|
|
888
|
|
889 ngx_memcpy(b, body->buf, sizeof(ngx_buf_t));
|
|
890
|
|
891 cl->next = ngx_alloc_chain_link(r->pool);
|
|
892 if (cl->next == NULL) {
|
|
893 return NGX_ERROR;
|
|
894 }
|
|
895
|
|
896 cl = cl->next;
|
|
897 cl->buf = b;
|
|
898
|
|
899 body = body->next;
|
|
900 }
|
|
901
|
160
|
902 b->flush = 1;
|
|
903
|
58
|
904 } else {
|
|
905 u->request_bufs = cl;
|
|
906 }
|
|
907
|
|
908 cl->next = NULL;
|
|
909
|
|
910 return NGX_OK;
|
|
911 }
|
|
912
|
|
913
|
|
914 static ngx_int_t
|
|
915 ngx_http_proxy_reinit_request(ngx_http_request_t *r)
|
|
916 {
|
348
|
917 ngx_http_proxy_ctx_t *ctx;
|
|
918
|
|
919 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
920
|
|
921 if (ctx == NULL) {
|
58
|
922 return NGX_OK;
|
|
923 }
|
|
924
|
348
|
925 ctx->status = 0;
|
|
926 ctx->status_count = 0;
|
|
927 ctx->status_start = NULL;
|
|
928 ctx->status_end = NULL;
|
58
|
929
|
|
930 r->upstream->process_header = ngx_http_proxy_process_status_line;
|
|
931
|
|
932 return NGX_OK;
|
|
933 }
|
|
934
|
|
935
|
|
936 static ngx_int_t
|
|
937 ngx_http_proxy_process_status_line(ngx_http_request_t *r)
|
|
938 {
|
|
939 ngx_int_t rc;
|
|
940 ngx_http_upstream_t *u;
|
348
|
941 ngx_http_proxy_ctx_t *ctx;
|
|
942
|
|
943 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
944
|
|
945 if (ctx == NULL) {
|
356
|
946 return NGX_ERROR;
|
58
|
947 }
|
|
948
|
348
|
949 rc = ngx_http_proxy_parse_status_line(r, ctx);
|
58
|
950
|
|
951 if (rc == NGX_AGAIN) {
|
|
952 return rc;
|
|
953 }
|
|
954
|
|
955 u = r->upstream;
|
|
956
|
|
957 if (rc == NGX_HTTP_PROXY_PARSE_NO_HEADER) {
|
|
958 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
126
|
959 "upstream sent no valid HTTP/1.0 header");
|
58
|
960
|
168
|
961 #if 0
|
58
|
962 if (u->accel) {
|
|
963 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
964 }
|
168
|
965 #endif
|
58
|
966
|
|
967 r->http_version = NGX_HTTP_VERSION_9;
|
346
|
968 u->headers_in.status_n = NGX_HTTP_OK;
|
|
969 u->state->status = NGX_HTTP_OK;
|
126
|
970
|
58
|
971 return NGX_OK;
|
|
972 }
|
|
973
|
348
|
974 u->headers_in.status_n = ctx->status;
|
|
975 u->state->status = ctx->status;
|
|
976
|
|
977 u->headers_in.status_line.len = ctx->status_end - ctx->status_start;
|
382
|
978 u->headers_in.status_line.data = ngx_pnalloc(r->pool,
|
|
979 u->headers_in.status_line.len);
|
78
|
980 if (u->headers_in.status_line.data == NULL) {
|
356
|
981 return NGX_ERROR;
|
58
|
982 }
|
122
|
983
|
348
|
984 ngx_memcpy(u->headers_in.status_line.data, ctx->status_start,
|
78
|
985 u->headers_in.status_line.len);
|
58
|
986
|
|
987 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
988 "http proxy status %ui \"%V\"",
|
270
|
989 u->headers_in.status_n, &u->headers_in.status_line);
|
58
|
990
|
|
991 u->process_header = ngx_http_proxy_process_header;
|
|
992
|
|
993 return ngx_http_proxy_process_header(r);
|
|
994 }
|
|
995
|
|
996
|
|
997 static ngx_int_t
|
348
|
998 ngx_http_proxy_parse_status_line(ngx_http_request_t *r,
|
|
999 ngx_http_proxy_ctx_t *ctx)
|
58
|
1000 {
|
|
1001 u_char ch;
|
348
|
1002 u_char *p;
|
58
|
1003 ngx_http_upstream_t *u;
|
|
1004 enum {
|
|
1005 sw_start = 0,
|
|
1006 sw_H,
|
|
1007 sw_HT,
|
|
1008 sw_HTT,
|
|
1009 sw_HTTP,
|
|
1010 sw_first_major_digit,
|
|
1011 sw_major_digit,
|
|
1012 sw_first_minor_digit,
|
|
1013 sw_minor_digit,
|
|
1014 sw_status,
|
|
1015 sw_space_after_status,
|
|
1016 sw_status_text,
|
|
1017 sw_almost_done
|
126
|
1018 } state;
|
58
|
1019
|
|
1020 u = r->upstream;
|
|
1021
|
|
1022 state = r->state;
|
|
1023
|
348
|
1024 for (p = u->buffer.pos; p < u->buffer.last; p++) {
|
|
1025 ch = *p;
|
58
|
1026
|
|
1027 switch (state) {
|
|
1028
|
|
1029 /* "HTTP/" */
|
|
1030 case sw_start:
|
|
1031 switch (ch) {
|
126
|
1032 case 'H':
|
58
|
1033 state = sw_H;
|
|
1034 break;
|
|
1035 default:
|
|
1036 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1037 }
|
|
1038 break;
|
|
1039
|
126
|
1040 case sw_H:
|
58
|
1041 switch (ch) {
|
126
|
1042 case 'T':
|
58
|
1043 state = sw_HT;
|
|
1044 break;
|
|
1045 default:
|
|
1046 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1047 }
|
|
1048 break;
|
|
1049
|
|
1050 case sw_HT:
|
|
1051 switch (ch) {
|
126
|
1052 case 'T':
|
58
|
1053 state = sw_HTT;
|
|
1054 break;
|
|
1055 default:
|
|
1056 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1057 }
|
|
1058 break;
|
|
1059
|
|
1060 case sw_HTT:
|
|
1061 switch (ch) {
|
126
|
1062 case 'P':
|
58
|
1063 state = sw_HTTP;
|
|
1064 break;
|
|
1065 default:
|
|
1066 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1067 }
|
|
1068 break;
|
|
1069
|
|
1070 case sw_HTTP:
|
|
1071 switch (ch) {
|
126
|
1072 case '/':
|
58
|
1073 state = sw_first_major_digit;
|
|
1074 break;
|
|
1075 default:
|
|
1076 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1077 }
|
|
1078 break;
|
|
1079
|
|
1080 /* the first digit of major HTTP version */
|
|
1081 case sw_first_major_digit:
|
|
1082 if (ch < '1' || ch > '9') {
|
|
1083 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1084 }
|
|
1085
|
|
1086 state = sw_major_digit;
|
|
1087 break;
|
|
1088
|
|
1089 /* the major HTTP version or dot */
|
|
1090 case sw_major_digit:
|
|
1091 if (ch == '.') {
|
|
1092 state = sw_first_minor_digit;
|
|
1093 break;
|
|
1094 }
|
|
1095
|
|
1096 if (ch < '0' || ch > '9') {
|
|
1097 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1098 }
|
|
1099
|
|
1100 break;
|
|
1101
|
|
1102 /* the first digit of minor HTTP version */
|
|
1103 case sw_first_minor_digit:
|
|
1104 if (ch < '0' || ch > '9') {
|
|
1105 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1106 }
|
|
1107
|
|
1108 state = sw_minor_digit;
|
|
1109 break;
|
|
1110
|
|
1111 /* the minor HTTP version or the end of the request line */
|
|
1112 case sw_minor_digit:
|
|
1113 if (ch == ' ') {
|
|
1114 state = sw_status;
|
|
1115 break;
|
|
1116 }
|
|
1117
|
|
1118 if (ch < '0' || ch > '9') {
|
|
1119 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1120 }
|
|
1121
|
|
1122 break;
|
|
1123
|
|
1124 /* HTTP status code */
|
|
1125 case sw_status:
|
168
|
1126 if (ch == ' ') {
|
|
1127 break;
|
|
1128 }
|
|
1129
|
58
|
1130 if (ch < '0' || ch > '9') {
|
|
1131 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1132 }
|
|
1133
|
348
|
1134 ctx->status = ctx->status * 10 + ch - '0';
|
|
1135
|
|
1136 if (++ctx->status_count == 3) {
|
58
|
1137 state = sw_space_after_status;
|
348
|
1138 ctx->status_start = p - 2;
|
58
|
1139 }
|
|
1140
|
|
1141 break;
|
|
1142
|
|
1143 /* space or end of line */
|
|
1144 case sw_space_after_status:
|
|
1145 switch (ch) {
|
126
|
1146 case ' ':
|
58
|
1147 state = sw_status_text;
|
|
1148 break;
|
|
1149 case '.': /* IIS may send 403.1, 403.2, etc */
|
|
1150 state = sw_status_text;
|
|
1151 break;
|
|
1152 case CR:
|
|
1153 state = sw_almost_done;
|
|
1154 break;
|
|
1155 case LF:
|
|
1156 goto done;
|
|
1157 default:
|
|
1158 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1159 }
|
|
1160 break;
|
|
1161
|
|
1162 /* any text until end of line */
|
|
1163 case sw_status_text:
|
|
1164 switch (ch) {
|
|
1165 case CR:
|
|
1166 state = sw_almost_done;
|
|
1167
|
|
1168 break;
|
|
1169 case LF:
|
|
1170 goto done;
|
|
1171 }
|
|
1172 break;
|
|
1173
|
76
|
1174 /* end of status line */
|
58
|
1175 case sw_almost_done:
|
348
|
1176 ctx->status_end = p - 1;
|
58
|
1177 switch (ch) {
|
|
1178 case LF:
|
|
1179 goto done;
|
|
1180 default:
|
|
1181 return NGX_HTTP_PROXY_PARSE_NO_HEADER;
|
|
1182 }
|
|
1183 }
|
|
1184 }
|
|
1185
|
348
|
1186 u->buffer.pos = p;
|
58
|
1187 r->state = state;
|
|
1188
|
|
1189 return NGX_AGAIN;
|
|
1190
|
|
1191 done:
|
|
1192
|
348
|
1193 u->buffer.pos = p + 1;
|
|
1194
|
|
1195 if (ctx->status_end == NULL) {
|
|
1196 ctx->status_end = p;
|
58
|
1197 }
|
|
1198
|
|
1199 r->state = sw_start;
|
|
1200
|
|
1201 return NGX_OK;
|
|
1202 }
|
|
1203
|
|
1204
|
|
1205 static ngx_int_t
|
|
1206 ngx_http_proxy_process_header(ngx_http_request_t *r)
|
|
1207 {
|
|
1208 ngx_int_t rc;
|
|
1209 ngx_table_elt_t *h;
|
|
1210 ngx_http_upstream_header_t *hh;
|
|
1211 ngx_http_upstream_main_conf_t *umcf;
|
|
1212
|
|
1213 umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
|
|
1214
|
378
|
1215 for ( ;; ) {
|
58
|
1216
|
130
|
1217 rc = ngx_http_parse_header_line(r, &r->upstream->buffer);
|
58
|
1218
|
|
1219 if (rc == NGX_OK) {
|
|
1220
|
|
1221 /* a header line has been parsed successfully */
|
|
1222
|
|
1223 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1224 if (h == NULL) {
|
356
|
1225 return NGX_ERROR;
|
58
|
1226 }
|
|
1227
|
|
1228 h->hash = r->header_hash;
|
|
1229
|
|
1230 h->key.len = r->header_name_end - r->header_name_start;
|
|
1231 h->value.len = r->header_end - r->header_start;
|
|
1232
|
382
|
1233 h->key.data = ngx_pnalloc(r->pool,
|
198
|
1234 h->key.len + 1 + h->value.len + 1 + h->key.len);
|
58
|
1235 if (h->key.data == NULL) {
|
356
|
1236 return NGX_ERROR;
|
58
|
1237 }
|
|
1238
|
|
1239 h->value.data = h->key.data + h->key.len + 1;
|
198
|
1240 h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;
|
58
|
1241
|
|
1242 ngx_cpystrn(h->key.data, r->header_name_start, h->key.len + 1);
|
|
1243 ngx_cpystrn(h->value.data, r->header_start, h->value.len + 1);
|
|
1244
|
198
|
1245 if (h->key.len == r->lowcase_index) {
|
|
1246 ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
|
|
1247
|
|
1248 } else {
|
392
|
1249 ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
|
58
|
1250 }
|
|
1251
|
198
|
1252 hh = ngx_hash_find(&umcf->headers_in_hash, h->hash,
|
|
1253 h->lowcase_key, h->key.len);
|
|
1254
|
|
1255 if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
|
356
|
1256 return NGX_ERROR;
|
198
|
1257 }
|
|
1258
|
58
|
1259 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1260 "http proxy header: \"%V: %V\"",
|
|
1261 &h->key, &h->value);
|
|
1262
|
|
1263 continue;
|
|
1264 }
|
|
1265
|
|
1266 if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
|
|
1267
|
|
1268 /* a whole header has been parsed successfully */
|
|
1269
|
|
1270 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1271 "http proxy header done");
|
|
1272
|
198
|
1273 /*
|
|
1274 * if no "Server" and "Date" in header line,
|
|
1275 * then add the special empty headers
|
|
1276 */
|
|
1277
|
|
1278 if (r->upstream->headers_in.server == NULL) {
|
|
1279 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1280 if (h == NULL) {
|
356
|
1281 return NGX_ERROR;
|
198
|
1282 }
|
|
1283
|
|
1284 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(
|
|
1285 ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
|
|
1286
|
|
1287 h->key.len = sizeof("Server") - 1;
|
|
1288 h->key.data = (u_char *) "Server";
|
|
1289 h->value.len = 0;
|
|
1290 h->value.data = NULL;
|
|
1291 h->lowcase_key = (u_char *) "server";
|
|
1292 }
|
|
1293
|
|
1294 if (r->upstream->headers_in.date == NULL) {
|
|
1295 h = ngx_list_push(&r->upstream->headers_in.headers);
|
|
1296 if (h == NULL) {
|
356
|
1297 return NGX_ERROR;
|
198
|
1298 }
|
|
1299
|
|
1300 h->hash = ngx_hash(ngx_hash(ngx_hash('d', 'a'), 't'), 'e');
|
|
1301
|
|
1302 h->key.len = sizeof("Date") - 1;
|
|
1303 h->key.data = (u_char *) "Date";
|
|
1304 h->value.len = 0;
|
|
1305 h->value.data = NULL;
|
|
1306 h->lowcase_key = (u_char *) "date";
|
|
1307 }
|
|
1308
|
58
|
1309 return NGX_OK;
|
|
1310 }
|
|
1311
|
70
|
1312 if (rc == NGX_AGAIN) {
|
|
1313 return NGX_AGAIN;
|
|
1314 }
|
|
1315
|
58
|
1316 /* there was error while a header line parsing */
|
|
1317
|
|
1318 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
168
|
1319 "upstream sent invalid header");
|
58
|
1320
|
|
1321 return NGX_HTTP_UPSTREAM_INVALID_HEADER;
|
|
1322 }
|
|
1323 }
|
|
1324
|
|
1325
|
|
1326 static void
|
|
1327 ngx_http_proxy_abort_request(ngx_http_request_t *r)
|
|
1328 {
|
|
1329 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1330 "abort http proxy request");
|
126
|
1331
|
58
|
1332 return;
|
|
1333 }
|
|
1334
|
|
1335
|
|
1336 static void
|
|
1337 ngx_http_proxy_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
|
126
|
1338 {
|
58
|
1339 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
1340 "finalize http proxy request");
|
|
1341
|
|
1342 return;
|
|
1343 }
|
|
1344
|
|
1345
|
122
|
1346 static ngx_int_t
|
|
1347 ngx_http_proxy_host_variable(ngx_http_request_t *r,
|
|
1348 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1349 {
|
348
|
1350 ngx_http_proxy_ctx_t *ctx;
|
|
1351
|
|
1352 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1353
|
|
1354 if (ctx == NULL) {
|
|
1355 v->not_found = 1;
|
|
1356 return NGX_OK;
|
|
1357 }
|
|
1358
|
|
1359 v->len = ctx->vars.host_header.len;
|
126
|
1360 v->valid = 1;
|
340
|
1361 v->no_cacheable = 0;
|
122
|
1362 v->not_found = 0;
|
348
|
1363 v->data = ctx->vars.host_header.data;
|
58
|
1364
|
122
|
1365 return NGX_OK;
|
58
|
1366 }
|
|
1367
|
|
1368
|
122
|
1369 static ngx_int_t
|
|
1370 ngx_http_proxy_port_variable(ngx_http_request_t *r,
|
|
1371 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1372 {
|
348
|
1373 ngx_http_proxy_ctx_t *ctx;
|
|
1374
|
|
1375 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1376
|
|
1377 if (ctx == NULL) {
|
|
1378 v->not_found = 1;
|
|
1379 return NGX_OK;
|
|
1380 }
|
|
1381
|
|
1382 v->len = ctx->vars.port.len;
|
126
|
1383 v->valid = 1;
|
340
|
1384 v->no_cacheable = 0;
|
122
|
1385 v->not_found = 0;
|
348
|
1386 v->data = ctx->vars.port.data;
|
58
|
1387
|
122
|
1388 return NGX_OK;
|
58
|
1389 }
|
|
1390
|
|
1391
|
122
|
1392 static ngx_int_t
|
58
|
1393 ngx_http_proxy_add_x_forwarded_for_variable(ngx_http_request_t *r,
|
122
|
1394 ngx_http_variable_value_t *v, uintptr_t data)
|
58
|
1395 {
|
122
|
1396 u_char *p;
|
58
|
1397
|
126
|
1398 v->valid = 1;
|
340
|
1399 v->no_cacheable = 0;
|
122
|
1400 v->not_found = 0;
|
58
|
1401
|
|
1402 if (r->headers_in.x_forwarded_for == NULL) {
|
122
|
1403 v->len = r->connection->addr_text.len;
|
|
1404 v->data = r->connection->addr_text.data;
|
|
1405 return NGX_OK;
|
58
|
1406 }
|
|
1407
|
122
|
1408 v->len = r->headers_in.x_forwarded_for->value.len
|
|
1409 + sizeof(", ") - 1 + r->connection->addr_text.len;
|
58
|
1410
|
382
|
1411 p = ngx_pnalloc(r->pool, v->len);
|
58
|
1412 if (p == NULL) {
|
122
|
1413 return NGX_ERROR;
|
58
|
1414 }
|
|
1415
|
122
|
1416 v->data = p;
|
58
|
1417
|
122
|
1418 p = ngx_copy(p, r->headers_in.x_forwarded_for->value.data,
|
|
1419 r->headers_in.x_forwarded_for->value.len);
|
58
|
1420
|
|
1421 *p++ = ','; *p++ = ' ';
|
|
1422
|
|
1423 ngx_memcpy(p, r->connection->addr_text.data, r->connection->addr_text.len);
|
|
1424
|
122
|
1425 return NGX_OK;
|
58
|
1426 }
|
|
1427
|
|
1428
|
|
1429 static ngx_int_t
|
126
|
1430 ngx_http_proxy_internal_body_length_variable(ngx_http_request_t *r,
|
|
1431 ngx_http_variable_value_t *v, uintptr_t data)
|
|
1432 {
|
348
|
1433 ngx_http_proxy_ctx_t *ctx;
|
|
1434
|
|
1435 ctx = ngx_http_get_module_ctx(r, ngx_http_proxy_module);
|
|
1436
|
|
1437 if (ctx == NULL) {
|
126
|
1438 v->not_found = 1;
|
|
1439 return NGX_OK;
|
|
1440 }
|
|
1441
|
|
1442 v->valid = 1;
|
340
|
1443 v->no_cacheable = 0;
|
126
|
1444 v->not_found = 0;
|
|
1445
|
382
|
1446 v->data = ngx_pnalloc(r->connection->pool, NGX_SIZE_T_LEN);
|
126
|
1447
|
|
1448 if (v->data == NULL) {
|
|
1449 return NGX_ERROR;
|
|
1450 }
|
|
1451
|
348
|
1452 v->len = ngx_sprintf(v->data, "%uz", ctx->internal_body_length) - v->data;
|
126
|
1453
|
|
1454 return NGX_OK;
|
|
1455 }
|
|
1456
|
|
1457
|
|
1458 static ngx_int_t
|
58
|
1459 ngx_http_proxy_rewrite_redirect(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1460 size_t prefix)
|
|
1461 {
|
|
1462 ngx_int_t rc;
|
|
1463 ngx_uint_t i;
|
|
1464 ngx_http_proxy_loc_conf_t *plcf;
|
|
1465 ngx_http_proxy_redirect_t *pr;
|
|
1466
|
|
1467 plcf = ngx_http_get_module_loc_conf(r, ngx_http_proxy_module);
|
|
1468
|
|
1469 pr = plcf->redirects->elts;
|
|
1470
|
|
1471 if (pr == NULL) {
|
|
1472 return NGX_DECLINED;
|
|
1473 }
|
|
1474
|
|
1475 for (i = 0; i < plcf->redirects->nelts; i++) {
|
154
|
1476 rc = pr[i].handler(r, h, prefix, &pr[i]);
|
58
|
1477
|
|
1478 if (rc != NGX_DECLINED) {
|
|
1479 return rc;
|
|
1480 }
|
|
1481 }
|
|
1482
|
|
1483 return NGX_DECLINED;
|
|
1484 }
|
|
1485
|
|
1486
|
|
1487 static ngx_int_t
|
|
1488 ngx_http_proxy_rewrite_redirect_text(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1489 size_t prefix, ngx_http_proxy_redirect_t *pr)
|
|
1490 {
|
|
1491 size_t len;
|
|
1492 u_char *data, *p;
|
|
1493
|
|
1494 if (pr->redirect.len > h->value.len - prefix
|
|
1495 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
|
|
1496 pr->redirect.len) != 0)
|
|
1497 {
|
|
1498 return NGX_DECLINED;
|
|
1499 }
|
|
1500
|
|
1501 len = prefix + pr->replacement.text.len + h->value.len - pr->redirect.len;
|
|
1502
|
382
|
1503 data = ngx_pnalloc(r->pool, len);
|
58
|
1504 if (data == NULL) {
|
|
1505 return NGX_ERROR;
|
|
1506 }
|
|
1507
|
|
1508 p = data;
|
|
1509
|
122
|
1510 p = ngx_copy(p, h->value.data, prefix);
|
58
|
1511
|
202
|
1512 if (pr->replacement.text.len) {
|
|
1513 p = ngx_copy(p, pr->replacement.text.data, pr->replacement.text.len);
|
|
1514 }
|
58
|
1515
|
|
1516 ngx_memcpy(p, h->value.data + prefix + pr->redirect.len,
|
|
1517 h->value.len - pr->redirect.len - prefix);
|
|
1518
|
|
1519 h->value.len = len;
|
|
1520 h->value.data = data;
|
|
1521
|
|
1522 return NGX_OK;
|
|
1523 }
|
|
1524
|
|
1525
|
|
1526 static ngx_int_t
|
|
1527 ngx_http_proxy_rewrite_redirect_vars(ngx_http_request_t *r, ngx_table_elt_t *h,
|
|
1528 size_t prefix, ngx_http_proxy_redirect_t *pr)
|
|
1529 {
|
|
1530 size_t len;
|
|
1531 u_char *data, *p;
|
|
1532 ngx_http_script_code_pt code;
|
|
1533 ngx_http_script_engine_t e;
|
|
1534 ngx_http_script_len_code_pt lcode;
|
|
1535
|
|
1536 if (pr->redirect.len > h->value.len - prefix
|
|
1537 || ngx_rstrncmp(h->value.data + prefix, pr->redirect.data,
|
|
1538 pr->redirect.len) != 0)
|
|
1539 {
|
|
1540 return NGX_DECLINED;
|
|
1541 }
|
|
1542
|
|
1543 ngx_memzero(&e, sizeof(ngx_http_script_engine_t));
|
|
1544
|
|
1545 e.ip = pr->replacement.vars.lengths;
|
|
1546 e.request = r;
|
|
1547
|
238
|
1548 len = prefix + h->value.len - pr->redirect.len;
|
|
1549
|
|
1550 while (*(uintptr_t *) e.ip) {
|
58
|
1551 lcode = *(ngx_http_script_len_code_pt *) e.ip;
|
238
|
1552 len += lcode(&e);
|
58
|
1553 }
|
|
1554
|
382
|
1555 data = ngx_pnalloc(r->pool, len);
|
58
|
1556 if (data == NULL) {
|
|
1557 return NGX_ERROR;
|
|
1558 }
|
|
1559
|
|
1560 p = data;
|
|
1561
|
122
|
1562 p = ngx_copy(p, h->value.data, prefix);
|
58
|
1563
|
|
1564 e.ip = pr->replacement.vars.values;
|
|
1565 e.pos = p;
|
|
1566
|
|
1567 while (*(uintptr_t *) e.ip) {
|
|
1568 code = *(ngx_http_script_code_pt *) e.ip;
|
|
1569 code(&e);
|
|
1570 }
|
|
1571
|
238
|
1572 ngx_memcpy(e.pos, h->value.data + prefix + pr->redirect.len,
|
|
1573 h->value.len - pr->redirect.len - prefix);
|
|
1574
|
58
|
1575 h->value.len = len;
|
|
1576 h->value.data = data;
|
|
1577
|
|
1578 return NGX_OK;
|
|
1579 }
|
|
1580
|
|
1581
|
|
1582 static ngx_int_t
|
|
1583 ngx_http_proxy_add_variables(ngx_conf_t *cf)
|
|
1584 {
|
|
1585 ngx_http_variable_t *var, *v;
|
|
1586
|
|
1587 for (v = ngx_http_proxy_vars; v->name.len; v++) {
|
|
1588 var = ngx_http_add_variable(cf, &v->name, v->flags);
|
|
1589 if (var == NULL) {
|
|
1590 return NGX_ERROR;
|
|
1591 }
|
|
1592
|
186
|
1593 var->get_handler = v->get_handler;
|
58
|
1594 var->data = v->data;
|
|
1595 }
|
|
1596
|
|
1597 return NGX_OK;
|
|
1598 }
|
|
1599
|
|
1600
|
|
1601 static void *
|
|
1602 ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
|
|
1603 {
|
|
1604 ngx_http_proxy_loc_conf_t *conf;
|
|
1605
|
|
1606 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_proxy_loc_conf_t));
|
|
1607 if (conf == NULL) {
|
|
1608 return NGX_CONF_ERROR;
|
|
1609 }
|
|
1610
|
|
1611 /*
|
|
1612 * set by ngx_pcalloc():
|
|
1613 *
|
|
1614 * conf->upstream.bufs.num = 0;
|
|
1615 * conf->upstream.next_upstream = 0;
|
|
1616 * conf->upstream.temp_path = NULL;
|
198
|
1617 * conf->upstream.hide_headers_hash = { NULL, 0 };
|
58
|
1618 * conf->upstream.schema = { 0, NULL };
|
|
1619 * conf->upstream.uri = { 0, NULL };
|
|
1620 * conf->upstream.location = NULL;
|
320
|
1621 * conf->upstream.store_lengths = NULL;
|
|
1622 * conf->upstream.store_values = NULL;
|
58
|
1623 *
|
110
|
1624 * conf->method = NULL;
|
58
|
1625 * conf->headers_source = NULL;
|
|
1626 * conf->headers_set_len = NULL;
|
|
1627 * conf->headers_set = NULL;
|
|
1628 * conf->headers_set_hash = NULL;
|
126
|
1629 * conf->body_set_len = NULL;
|
|
1630 * conf->body_set = NULL;
|
|
1631 * conf->body_source = { 0, NULL };
|
58
|
1632 * conf->rewrite_locations = NULL;
|
|
1633 */
|
126
|
1634
|
320
|
1635 conf->upstream.store = NGX_CONF_UNSET;
|
|
1636 conf->upstream.store_access = NGX_CONF_UNSET_UINT;
|
130
|
1637 conf->upstream.buffering = NGX_CONF_UNSET;
|
178
|
1638 conf->upstream.ignore_client_abort = NGX_CONF_UNSET;
|
130
|
1639
|
58
|
1640 conf->upstream.connect_timeout = NGX_CONF_UNSET_MSEC;
|
|
1641 conf->upstream.send_timeout = NGX_CONF_UNSET_MSEC;
|
|
1642 conf->upstream.read_timeout = NGX_CONF_UNSET_MSEC;
|
|
1643
|
|
1644 conf->upstream.send_lowat = NGX_CONF_UNSET_SIZE;
|
130
|
1645 conf->upstream.buffer_size = NGX_CONF_UNSET_SIZE;
|
78
|
1646
|
|
1647 conf->upstream.busy_buffers_size_conf = NGX_CONF_UNSET_SIZE;
|
126
|
1648 conf->upstream.max_temp_file_size_conf = NGX_CONF_UNSET_SIZE;
|
78
|
1649 conf->upstream.temp_file_write_size_conf = NGX_CONF_UNSET_SIZE;
|
58
|
1650
|
|
1651 conf->upstream.pass_request_headers = NGX_CONF_UNSET;
|
|
1652 conf->upstream.pass_request_body = NGX_CONF_UNSET;
|
110
|
1653
|
356
|
1654 conf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
|
|
1655 conf->upstream.pass_headers = NGX_CONF_UNSET_PTR;
|
|
1656
|
206
|
1657 conf->upstream.intercept_errors = NGX_CONF_UNSET;
|
398
|
1658 #if (NGX_HTTP_SSL)
|
|
1659 conf->upstream.ssl_session_reuse = NGX_CONF_UNSET;
|
|
1660 #endif
|
58
|
1661
|
|
1662 /* "proxy_cyclic_temp_file" is disabled */
|
|
1663 conf->upstream.cyclic_temp_file = 0;
|
|
1664
|
|
1665 conf->redirect = NGX_CONF_UNSET;
|
198
|
1666 conf->upstream.change_buffering = 1;
|
58
|
1667
|
298
|
1668 conf->headers_hash_max_size = NGX_CONF_UNSET_UINT;
|
|
1669 conf->headers_hash_bucket_size = NGX_CONF_UNSET_UINT;
|
|
1670
|
58
|
1671 return conf;
|
|
1672 }
|
|
1673
|
|
1674
|
|
1675 static char *
|
|
1676 ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
1677 {
|
|
1678 ngx_http_proxy_loc_conf_t *prev = parent;
|
|
1679 ngx_http_proxy_loc_conf_t *conf = child;
|
|
1680
|
|
1681 u_char *p;
|
|
1682 size_t size;
|
|
1683 uintptr_t *code;
|
356
|
1684 ngx_uint_t i;
|
198
|
1685 ngx_keyval_t *src, *s, *h;
|
|
1686 ngx_hash_key_t *hk;
|
|
1687 ngx_hash_init_t hash;
|
58
|
1688 ngx_http_proxy_redirect_t *pr;
|
|
1689 ngx_http_script_compile_t sc;
|
|
1690 ngx_http_script_copy_code_t *copy;
|
|
1691
|
320
|
1692 if (conf->upstream.store != 0) {
|
|
1693 ngx_conf_merge_value(conf->upstream.store,
|
|
1694 prev->upstream.store, 0);
|
|
1695
|
|
1696 if (conf->upstream.store_lengths == NULL) {
|
|
1697 conf->upstream.store_lengths = prev->upstream.store_lengths;
|
|
1698 conf->upstream.store_values = prev->upstream.store_values;
|
|
1699 }
|
|
1700 }
|
|
1701
|
|
1702 ngx_conf_merge_uint_value(conf->upstream.store_access,
|
|
1703 prev->upstream.store_access, 0600);
|
318
|
1704
|
130
|
1705 ngx_conf_merge_value(conf->upstream.buffering,
|
|
1706 prev->upstream.buffering, 1);
|
|
1707
|
178
|
1708 ngx_conf_merge_value(conf->upstream.ignore_client_abort,
|
|
1709 prev->upstream.ignore_client_abort, 0);
|
|
1710
|
58
|
1711 ngx_conf_merge_msec_value(conf->upstream.connect_timeout,
|
|
1712 prev->upstream.connect_timeout, 60000);
|
|
1713
|
|
1714 ngx_conf_merge_msec_value(conf->upstream.send_timeout,
|
|
1715 prev->upstream.send_timeout, 60000);
|
|
1716
|
|
1717 ngx_conf_merge_msec_value(conf->upstream.read_timeout,
|
|
1718 prev->upstream.read_timeout, 60000);
|
|
1719
|
|
1720 ngx_conf_merge_size_value(conf->upstream.send_lowat,
|
|
1721 prev->upstream.send_lowat, 0);
|
|
1722
|
130
|
1723 ngx_conf_merge_size_value(conf->upstream.buffer_size,
|
|
1724 prev->upstream.buffer_size,
|
58
|
1725 (size_t) ngx_pagesize);
|
|
1726
|
|
1727 ngx_conf_merge_bufs_value(conf->upstream.bufs, prev->upstream.bufs,
|
|
1728 8, ngx_pagesize);
|
|
1729
|
|
1730 if (conf->upstream.bufs.num < 2) {
|
126
|
1731 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
58
|
1732 "there must be at least 2 \"proxy_buffers\"");
|
|
1733 return NGX_CONF_ERROR;
|
|
1734 }
|
126
|
1735
|
58
|
1736
|
130
|
1737 size = conf->upstream.buffer_size;
|
126
|
1738 if (size < conf->upstream.bufs.size) {
|
58
|
1739 size = conf->upstream.bufs.size;
|
|
1740 }
|
|
1741
|
|
1742
|
78
|
1743 ngx_conf_merge_size_value(conf->upstream.busy_buffers_size_conf,
|
|
1744 prev->upstream.busy_buffers_size_conf,
|
58
|
1745 NGX_CONF_UNSET_SIZE);
|
|
1746
|
78
|
1747 if (conf->upstream.busy_buffers_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
1748 conf->upstream.busy_buffers_size = 2 * size;
|
78
|
1749 } else {
|
|
1750 conf->upstream.busy_buffers_size =
|
|
1751 conf->upstream.busy_buffers_size_conf;
|
|
1752 }
|
126
|
1753
|
78
|
1754 if (conf->upstream.busy_buffers_size < size) {
|
58
|
1755 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1756 "\"proxy_busy_buffers_size\" must be equal or bigger than "
|
288
|
1757 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
1758 "one of the \"proxy_buffers\"");
|
|
1759
|
|
1760 return NGX_CONF_ERROR;
|
78
|
1761 }
|
58
|
1762
|
78
|
1763 if (conf->upstream.busy_buffers_size
|
|
1764 > (conf->upstream.bufs.num - 1) * conf->upstream.bufs.size)
|
58
|
1765 {
|
|
1766 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1767 "\"proxy_busy_buffers_size\" must be less than "
|
|
1768 "the size of all \"proxy_buffers\" minus one buffer");
|
|
1769
|
|
1770 return NGX_CONF_ERROR;
|
|
1771 }
|
78
|
1772
|
58
|
1773
|
78
|
1774 ngx_conf_merge_size_value(conf->upstream.temp_file_write_size_conf,
|
|
1775 prev->upstream.temp_file_write_size_conf,
|
58
|
1776 NGX_CONF_UNSET_SIZE);
|
|
1777
|
78
|
1778 if (conf->upstream.temp_file_write_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
1779 conf->upstream.temp_file_write_size = 2 * size;
|
78
|
1780 } else {
|
|
1781 conf->upstream.temp_file_write_size =
|
|
1782 conf->upstream.temp_file_write_size_conf;
|
|
1783 }
|
126
|
1784
|
78
|
1785 if (conf->upstream.temp_file_write_size < size) {
|
58
|
1786 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
1787 "\"proxy_temp_file_write_size\" must be equal or bigger than "
|
288
|
1788 "maximum of the value of \"proxy_buffer_size\" and "
|
58
|
1789 "one of the \"proxy_buffers\"");
|
|
1790
|
|
1791 return NGX_CONF_ERROR;
|
|
1792 }
|
|
1793
|
78
|
1794 ngx_conf_merge_size_value(conf->upstream.max_temp_file_size_conf,
|
|
1795 prev->upstream.max_temp_file_size_conf,
|
58
|
1796 NGX_CONF_UNSET_SIZE);
|
|
1797
|
78
|
1798 if (conf->upstream.max_temp_file_size_conf == NGX_CONF_UNSET_SIZE) {
|
58
|
1799 conf->upstream.max_temp_file_size = 1024 * 1024 * 1024;
|
78
|
1800 } else {
|
|
1801 conf->upstream.max_temp_file_size =
|
|
1802 conf->upstream.max_temp_file_size_conf;
|
|
1803 }
|
58
|
1804
|
78
|
1805 if (conf->upstream.max_temp_file_size != 0
|
|
1806 && conf->upstream.max_temp_file_size < size)
|
58
|
1807 {
|
|
1808 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
110
|
1809 "\"proxy_max_temp_file_size\" must be equal to zero to disable "
|
58
|
1810 "the temporary files usage or must be equal or bigger than "
|
288
|
1811 "maximum of the value of \"proxy_buffer_size\" and "
|
110
|
1812 "one of the \"proxy_buffers\"");
|
58
|
1813
|
|
1814 return NGX_CONF_ERROR;
|
|
1815 }
|
|
1816
|
78
|
1817
|
58
|
1818 ngx_conf_merge_bitmask_value(conf->upstream.next_upstream,
|
|
1819 prev->upstream.next_upstream,
|
|
1820 (NGX_CONF_BITMASK_SET
|
|
1821 |NGX_HTTP_UPSTREAM_FT_ERROR
|
|
1822 |NGX_HTTP_UPSTREAM_FT_TIMEOUT));
|
|
1823
|
214
|
1824 if (conf->upstream.next_upstream & NGX_HTTP_UPSTREAM_FT_OFF) {
|
|
1825 conf->upstream.next_upstream = NGX_CONF_BITMASK_SET
|
|
1826 |NGX_HTTP_UPSTREAM_FT_OFF;
|
|
1827 }
|
|
1828
|
58
|
1829 ngx_conf_merge_path_value(conf->upstream.temp_path,
|
|
1830 prev->upstream.temp_path,
|
|
1831 NGX_HTTP_PROXY_TEMP_PATH, 1, 2, 0,
|
|
1832 ngx_garbage_collector_temp_handler, cf);
|
|
1833
|
110
|
1834 if (conf->method.len == 0) {
|
|
1835 conf->method = prev->method;
|
|
1836
|
|
1837 } else {
|
|
1838 conf->method.data[conf->method.len] = ' ';
|
|
1839 conf->method.len++;
|
58
|
1840 }
|
|
1841
|
|
1842 ngx_conf_merge_value(conf->upstream.pass_request_headers,
|
|
1843 prev->upstream.pass_request_headers, 1);
|
|
1844 ngx_conf_merge_value(conf->upstream.pass_request_body,
|
|
1845 prev->upstream.pass_request_body, 1);
|
|
1846
|
206
|
1847 ngx_conf_merge_value(conf->upstream.intercept_errors,
|
|
1848 prev->upstream.intercept_errors, 0);
|
58
|
1849
|
398
|
1850 #if (NGX_HTTP_SSL)
|
|
1851 ngx_conf_merge_value(conf->upstream.ssl_session_reuse,
|
|
1852 prev->upstream.ssl_session_reuse, 1);
|
|
1853 #endif
|
|
1854
|
58
|
1855 ngx_conf_merge_value(conf->redirect, prev->redirect, 1);
|
|
1856
|
|
1857 if (conf->redirect) {
|
|
1858
|
|
1859 if (conf->redirects == NULL) {
|
|
1860 conf->redirects = prev->redirects;
|
|
1861 }
|
|
1862
|
348
|
1863 if (conf->redirects == NULL && conf->url.data) {
|
58
|
1864
|
|
1865 conf->redirects = ngx_array_create(cf->pool, 1,
|
|
1866 sizeof(ngx_http_proxy_redirect_t));
|
|
1867 if (conf->redirects == NULL) {
|
|
1868 return NGX_CONF_ERROR;
|
|
1869 }
|
|
1870
|
|
1871 pr = ngx_array_push(conf->redirects);
|
|
1872 if (pr == NULL) {
|
|
1873 return NGX_CONF_ERROR;
|
|
1874 }
|
|
1875
|
|
1876 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
348
|
1877 pr->redirect = conf->url;
|
|
1878
|
|
1879 if (conf->vars.uri.len) {
|
|
1880 pr->replacement.text = conf->location;
|
202
|
1881
|
|
1882 } else {
|
|
1883 pr->replacement.text.len = 0;
|
|
1884 pr->replacement.text.data = NULL;
|
|
1885 }
|
58
|
1886 }
|
|
1887 }
|
|
1888
|
348
|
1889 /* STUB */
|
|
1890 if (prev->proxy_lengths) {
|
|
1891 conf->proxy_lengths = prev->proxy_lengths;
|
|
1892 conf->proxy_values = prev->proxy_values;
|
|
1893 }
|
|
1894
|
298
|
1895 ngx_conf_merge_uint_value(conf->headers_hash_max_size,
|
|
1896 prev->headers_hash_max_size, 512);
|
|
1897
|
|
1898 ngx_conf_merge_uint_value(conf->headers_hash_bucket_size,
|
|
1899 prev->headers_hash_bucket_size, 64);
|
|
1900
|
|
1901 conf->headers_hash_bucket_size = ngx_align(conf->headers_hash_bucket_size,
|
|
1902 ngx_cacheline_size);
|
|
1903
|
356
|
1904 hash.max_size = conf->headers_hash_max_size;
|
|
1905 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
1906 hash.name = "proxy_headers_hash";
|
|
1907
|
|
1908 if (ngx_http_upstream_hide_headers_hash(cf, &conf->upstream,
|
|
1909 &prev->upstream,
|
|
1910 ngx_http_proxy_hide_headers, &hash)
|
198
|
1911 != NGX_OK)
|
|
1912 {
|
|
1913 return NGX_CONF_ERROR;
|
|
1914 }
|
|
1915
|
260
|
1916 if (conf->upstream.upstream == NULL) {
|
|
1917 conf->upstream.upstream = prev->upstream.upstream;
|
178
|
1918
|
348
|
1919 conf->vars = prev->vars;
|
178
|
1920 conf->upstream.schema = prev->upstream.schema;
|
58
|
1921 }
|
|
1922
|
126
|
1923
|
|
1924 if (conf->body_source.data == NULL) {
|
|
1925 conf->body_source = prev->body_source;
|
|
1926 conf->body_set_len = prev->body_set_len;
|
|
1927 conf->body_set = prev->body_set;
|
|
1928 }
|
|
1929
|
128
|
1930 if (conf->body_source.data && conf->body_set_len == NULL) {
|
126
|
1931
|
|
1932 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
1933
|
|
1934 sc.cf = cf;
|
|
1935 sc.source = &conf->body_source;
|
|
1936 sc.flushes = &conf->flushes;
|
|
1937 sc.lengths = &conf->body_set_len;
|
|
1938 sc.values = &conf->body_set;
|
|
1939 sc.complete_lengths = 1;
|
|
1940 sc.complete_values = 1;
|
|
1941
|
|
1942 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
1943 return NGX_CONF_ERROR;
|
|
1944 }
|
|
1945
|
|
1946 if (conf->headers_source == NULL) {
|
|
1947 conf->headers_source = ngx_array_create(cf->pool, 4,
|
198
|
1948 sizeof(ngx_keyval_t));
|
126
|
1949 if (conf->headers_source == NULL) {
|
|
1950 return NGX_CONF_ERROR;
|
|
1951 }
|
|
1952 }
|
|
1953
|
|
1954 s = ngx_array_push(conf->headers_source);
|
|
1955 if (s == NULL) {
|
|
1956 return NGX_CONF_ERROR;
|
|
1957 }
|
|
1958
|
|
1959 s->key.len = sizeof("Content-Length") - 1;
|
|
1960 s->key.data = (u_char *) "Content-Length";
|
|
1961 s->value.len = sizeof("$proxy_internal_body_length") - 1;
|
|
1962 s->value.data = (u_char *) "$proxy_internal_body_length";
|
|
1963 }
|
|
1964
|
|
1965
|
58
|
1966 if (conf->headers_source == NULL) {
|
122
|
1967 conf->flushes = prev->flushes;
|
58
|
1968 conf->headers_set_len = prev->headers_set_len;
|
|
1969 conf->headers_set = prev->headers_set;
|
|
1970 conf->headers_set_hash = prev->headers_set_hash;
|
122
|
1971 conf->headers_source = prev->headers_source;
|
58
|
1972 }
|
|
1973
|
198
|
1974 if (conf->headers_set_hash.buckets) {
|
58
|
1975 return NGX_CONF_OK;
|
|
1976 }
|
|
1977
|
|
1978
|
198
|
1979 conf->headers_names = ngx_array_create(cf->pool, 4, sizeof(ngx_hash_key_t));
|
58
|
1980 if (conf->headers_names == NULL) {
|
|
1981 return NGX_CONF_ERROR;
|
|
1982 }
|
|
1983
|
|
1984 if (conf->headers_source == NULL) {
|
|
1985 conf->headers_source = ngx_array_create(cf->pool, 4,
|
198
|
1986 sizeof(ngx_keyval_t));
|
58
|
1987 if (conf->headers_source == NULL) {
|
|
1988 return NGX_CONF_ERROR;
|
|
1989 }
|
|
1990 }
|
|
1991
|
|
1992 conf->headers_set_len = ngx_array_create(cf->pool, 64, 1);
|
|
1993 if (conf->headers_set_len == NULL) {
|
|
1994 return NGX_CONF_ERROR;
|
|
1995 }
|
|
1996
|
|
1997 conf->headers_set = ngx_array_create(cf->pool, 512, 1);
|
|
1998 if (conf->headers_set == NULL) {
|
|
1999 return NGX_CONF_ERROR;
|
|
2000 }
|
|
2001
|
|
2002
|
|
2003 src = conf->headers_source->elts;
|
|
2004
|
|
2005 for (h = ngx_http_proxy_headers; h->key.len; h++) {
|
|
2006
|
|
2007 for (i = 0; i < conf->headers_source->nelts; i++) {
|
|
2008 if (ngx_strcasecmp(h->key.data, src[i].key.data) == 0) {
|
|
2009 goto next;
|
|
2010 }
|
|
2011 }
|
|
2012
|
|
2013 s = ngx_array_push(conf->headers_source);
|
|
2014 if (s == NULL) {
|
|
2015 return NGX_CONF_ERROR;
|
|
2016 }
|
|
2017
|
|
2018 *s = *h;
|
|
2019
|
66
|
2020 src = conf->headers_source->elts;
|
|
2021
|
58
|
2022 next:
|
|
2023
|
|
2024 continue;
|
|
2025 }
|
|
2026
|
126
|
2027
|
|
2028 src = conf->headers_source->elts;
|
58
|
2029 for (i = 0; i < conf->headers_source->nelts; i++) {
|
|
2030
|
198
|
2031 hk = ngx_array_push(conf->headers_names);
|
|
2032 if (hk == NULL) {
|
58
|
2033 return NGX_CONF_ERROR;
|
|
2034 }
|
|
2035
|
198
|
2036 hk->key = src[i].key;
|
|
2037 hk->key_hash = ngx_hash_key_lc(src[i].key.data, src[i].key.len);
|
|
2038 hk->value = (void *) 1;
|
58
|
2039
|
68
|
2040 if (src[i].value.len == 0) {
|
|
2041 continue;
|
|
2042 }
|
|
2043
|
58
|
2044 if (ngx_http_script_variables_count(&src[i].value) == 0) {
|
|
2045 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2046 sizeof(ngx_http_script_copy_code_t));
|
|
2047 if (copy == NULL) {
|
|
2048 return NGX_CONF_ERROR;
|
|
2049 }
|
|
2050
|
|
2051 copy->code = (ngx_http_script_code_pt)
|
|
2052 ngx_http_script_copy_len_code;
|
|
2053 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
2054 + src[i].value.len + sizeof(CRLF) - 1;
|
|
2055
|
|
2056
|
|
2057 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2058 + src[i].key.len + sizeof(": ") - 1
|
|
2059 + src[i].value.len + sizeof(CRLF) - 1
|
|
2060 + sizeof(uintptr_t) - 1)
|
|
2061 & ~(sizeof(uintptr_t) - 1);
|
|
2062
|
|
2063 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2064 if (copy == NULL) {
|
|
2065 return NGX_CONF_ERROR;
|
|
2066 }
|
|
2067
|
|
2068 copy->code = ngx_http_script_copy_code;
|
|
2069 copy->len = src[i].key.len + sizeof(": ") - 1
|
|
2070 + src[i].value.len + sizeof(CRLF) - 1;
|
|
2071
|
|
2072 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
2073
|
|
2074 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
2075 *p++ = ':'; *p++ = ' ';
|
|
2076 p = ngx_cpymem(p, src[i].value.data, src[i].value.len);
|
|
2077 *p++ = CR; *p = LF;
|
|
2078
|
|
2079 } else {
|
|
2080 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2081 sizeof(ngx_http_script_copy_code_t));
|
|
2082 if (copy == NULL) {
|
|
2083 return NGX_CONF_ERROR;
|
|
2084 }
|
|
2085
|
|
2086 copy->code = (ngx_http_script_code_pt)
|
|
2087 ngx_http_script_copy_len_code;
|
|
2088 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
2089
|
|
2090
|
|
2091 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2092 + src[i].key.len + sizeof(": ") - 1 + sizeof(uintptr_t) - 1)
|
|
2093 & ~(sizeof(uintptr_t) - 1);
|
|
2094
|
|
2095 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2096 if (copy == NULL) {
|
|
2097 return NGX_CONF_ERROR;
|
|
2098 }
|
|
2099
|
|
2100 copy->code = ngx_http_script_copy_code;
|
|
2101 copy->len = src[i].key.len + sizeof(": ") - 1;
|
|
2102
|
|
2103 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
2104 p = ngx_cpymem(p, src[i].key.data, src[i].key.len);
|
|
2105 *p++ = ':'; *p = ' ';
|
|
2106
|
|
2107
|
|
2108 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2109
|
|
2110 sc.cf = cf;
|
|
2111 sc.source = &src[i].value;
|
122
|
2112 sc.flushes = &conf->flushes;
|
58
|
2113 sc.lengths = &conf->headers_set_len;
|
|
2114 sc.values = &conf->headers_set;
|
|
2115
|
|
2116 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2117 return NGX_CONF_ERROR;
|
|
2118 }
|
|
2119
|
|
2120
|
|
2121 copy = ngx_array_push_n(conf->headers_set_len,
|
|
2122 sizeof(ngx_http_script_copy_code_t));
|
|
2123 if (copy == NULL) {
|
|
2124 return NGX_CONF_ERROR;
|
|
2125 }
|
|
2126
|
|
2127 copy->code = (ngx_http_script_code_pt)
|
|
2128 ngx_http_script_copy_len_code;
|
|
2129 copy->len = sizeof(CRLF) - 1;
|
|
2130
|
|
2131
|
|
2132 size = (sizeof(ngx_http_script_copy_code_t)
|
|
2133 + sizeof(CRLF) - 1 + sizeof(uintptr_t) - 1)
|
|
2134 & ~(sizeof(uintptr_t) - 1);
|
|
2135
|
|
2136 copy = ngx_array_push_n(conf->headers_set, size);
|
|
2137 if (copy == NULL) {
|
|
2138 return NGX_CONF_ERROR;
|
|
2139 }
|
|
2140
|
|
2141 copy->code = ngx_http_script_copy_code;
|
|
2142 copy->len = sizeof(CRLF) - 1;
|
|
2143
|
|
2144 p = (u_char *) copy + sizeof(ngx_http_script_copy_code_t);
|
|
2145 *p++ = CR; *p = LF;
|
|
2146 }
|
|
2147
|
|
2148 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
2149 if (code == NULL) {
|
|
2150 return NGX_CONF_ERROR;
|
|
2151 }
|
|
2152
|
|
2153 *code = (uintptr_t) NULL;
|
|
2154
|
|
2155 code = ngx_array_push_n(conf->headers_set, sizeof(uintptr_t));
|
|
2156 if (code == NULL) {
|
|
2157 return NGX_CONF_ERROR;
|
|
2158 }
|
|
2159
|
|
2160 *code = (uintptr_t) NULL;
|
|
2161 }
|
|
2162
|
|
2163 code = ngx_array_push_n(conf->headers_set_len, sizeof(uintptr_t));
|
|
2164 if (code == NULL) {
|
|
2165 return NGX_CONF_ERROR;
|
|
2166 }
|
|
2167
|
|
2168 *code = (uintptr_t) NULL;
|
|
2169
|
|
2170
|
198
|
2171 hash.hash = &conf->headers_set_hash;
|
|
2172 hash.key = ngx_hash_key_lc;
|
298
|
2173 hash.max_size = conf->headers_hash_max_size;
|
|
2174 hash.bucket_size = conf->headers_hash_bucket_size;
|
|
2175 hash.name = "proxy_headers_hash";
|
198
|
2176 hash.pool = cf->pool;
|
|
2177 hash.temp_pool = NULL;
|
|
2178
|
|
2179 if (ngx_hash_init(&hash, conf->headers_names->elts,
|
|
2180 conf->headers_names->nelts)
|
|
2181 != NGX_OK)
|
|
2182 {
|
|
2183 return NGX_CONF_ERROR;
|
|
2184 }
|
|
2185
|
58
|
2186 return NGX_CONF_OK;
|
|
2187 }
|
|
2188
|
|
2189
|
|
2190 static char *
|
|
2191 ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2192 {
|
|
2193 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
2194
|
348
|
2195 size_t add;
|
|
2196 u_short port;
|
|
2197 ngx_str_t *value, *url;
|
|
2198 ngx_url_t u;
|
|
2199 ngx_uint_t n;
|
|
2200 ngx_http_core_loc_conf_t *clcf;
|
|
2201 ngx_http_script_compile_t sc;
|
58
|
2202
|
104
|
2203 if (plcf->upstream.schema.len) {
|
|
2204 return "is duplicate";
|
|
2205 }
|
|
2206
|
348
|
2207 clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
|
|
2208
|
58
|
2209 value = cf->args->elts;
|
|
2210
|
|
2211 url = &value[1];
|
|
2212
|
348
|
2213 n = ngx_http_script_variables_count(url);
|
|
2214
|
|
2215 if (n) {
|
|
2216
|
|
2217 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2218
|
|
2219 sc.cf = cf;
|
|
2220 sc.source = url;
|
|
2221 sc.lengths = &plcf->proxy_lengths;
|
|
2222 sc.values = &plcf->proxy_values;
|
|
2223 sc.variables = n;
|
|
2224 sc.complete_lengths = 1;
|
|
2225 sc.complete_values = 1;
|
354
|
2226
|
348
|
2227 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2228 return NGX_CONF_ERROR;
|
|
2229 }
|
|
2230
|
|
2231 #if (NGX_HTTP_SSL)
|
|
2232 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
2233 return NGX_CONF_ERROR;
|
|
2234 }
|
|
2235 #endif
|
|
2236
|
|
2237 clcf->handler = ngx_http_proxy_handler;
|
|
2238
|
|
2239 return NGX_CONF_OK;
|
|
2240 }
|
|
2241
|
286
|
2242 if (ngx_strncasecmp(url->data, (u_char *) "http://", 7) == 0) {
|
126
|
2243 add = 7;
|
140
|
2244 port = 80;
|
126
|
2245
|
286
|
2246 } else if (ngx_strncasecmp(url->data, (u_char *) "https://", 8) == 0) {
|
126
|
2247
|
|
2248 #if (NGX_HTTP_SSL)
|
348
|
2249 if (ngx_http_proxy_set_ssl(cf, plcf) != NGX_OK) {
|
|
2250 return NGX_CONF_ERROR;
|
|
2251 }
|
126
|
2252
|
|
2253 add = 8;
|
140
|
2254 port = 443;
|
126
|
2255 #else
|
|
2256 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2257 "https protocol requires SSL support");
|
|
2258 return NGX_CONF_ERROR;
|
|
2259 #endif
|
|
2260
|
|
2261 } else {
|
58
|
2262 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "invalid URL prefix");
|
|
2263 return NGX_CONF_ERROR;
|
|
2264 }
|
|
2265
|
200
|
2266 ngx_memzero(&u, sizeof(ngx_url_t));
|
|
2267
|
|
2268 u.url.len = url->len - add;
|
|
2269 u.url.data = url->data + add;
|
266
|
2270 u.default_port = port;
|
|
2271 u.uri_part = 1;
|
260
|
2272 u.no_resolve = 1;
|
|
2273
|
|
2274 plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
|
|
2275 if (plcf->upstream.upstream == NULL) {
|
58
|
2276 return NGX_CONF_ERROR;
|
|
2277 }
|
|
2278
|
348
|
2279 if (ngx_http_proxy_set_vars(cf->pool, &u, &plcf->vars) != NGX_OK) {
|
|
2280 return NGX_CONF_ERROR;
|
266
|
2281 }
|
|
2282
|
140
|
2283 plcf->upstream.schema.len = add;
|
|
2284 plcf->upstream.schema.data = url->data;
|
348
|
2285 plcf->location = clcf->name;
|
58
|
2286
|
|
2287 clcf->handler = ngx_http_proxy_handler;
|
|
2288
|
324
|
2289 if (clcf->named
|
58
|
2290 #if (NGX_PCRE)
|
324
|
2291 || clcf->regex
|
|
2292 #endif
|
|
2293 || clcf->noname)
|
|
2294 {
|
348
|
2295 if (plcf->vars.uri.len) {
|
122
|
2296 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2297 "\"proxy_pass\" may not have URI part in "
|
256
|
2298 "location given by regular expression, "
|
324
|
2299 "or inside named location, "
|
256
|
2300 "or inside the \"if\" statement, "
|
|
2301 "or inside the \"limit_except\" block");
|
122
|
2302 return NGX_CONF_ERROR;
|
|
2303 }
|
|
2304
|
348
|
2305 plcf->location.len = 0;
|
104
|
2306 }
|
|
2307
|
348
|
2308 plcf->url = *url;
|
58
|
2309
|
|
2310 if (clcf->name.data[clcf->name.len - 1] == '/') {
|
|
2311 clcf->auto_redirect = 1;
|
|
2312 }
|
|
2313
|
|
2314 return NGX_CONF_OK;
|
|
2315 }
|
|
2316
|
|
2317
|
|
2318 static char *
|
|
2319 ngx_http_proxy_redirect(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2320 {
|
|
2321 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
2322
|
|
2323 ngx_str_t *value;
|
|
2324 ngx_array_t *vars_lengths, *vars_values;
|
|
2325 ngx_http_script_compile_t sc;
|
|
2326 ngx_http_proxy_redirect_t *pr;
|
|
2327
|
|
2328 if (plcf->redirect == 0) {
|
|
2329 return NGX_CONF_OK;
|
|
2330 }
|
|
2331
|
|
2332 value = cf->args->elts;
|
|
2333
|
|
2334 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
2335 plcf->redirect = 0;
|
|
2336 plcf->redirects = NULL;
|
|
2337 return NGX_CONF_OK;
|
|
2338 }
|
|
2339
|
|
2340 if (plcf->redirects == NULL) {
|
|
2341 plcf->redirects = ngx_array_create(cf->pool, 1,
|
|
2342 sizeof(ngx_http_proxy_redirect_t));
|
|
2343 if (plcf->redirects == NULL) {
|
|
2344 return NGX_CONF_ERROR;
|
|
2345 }
|
|
2346 }
|
|
2347
|
|
2348 pr = ngx_array_push(plcf->redirects);
|
|
2349 if (pr == NULL) {
|
|
2350 return NGX_CONF_ERROR;
|
|
2351 }
|
|
2352
|
|
2353 if (cf->args->nelts == 2 && ngx_strcmp(value[1].data, "default") == 0) {
|
348
|
2354 if (plcf->url.data == NULL) {
|
58
|
2355 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2356 "\"proxy_rewrite_location default\" must go "
|
|
2357 "after the \"proxy_pass\" directive");
|
|
2358 return NGX_CONF_ERROR;
|
|
2359 }
|
|
2360
|
|
2361 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
348
|
2362 pr->redirect = plcf->url;
|
|
2363
|
|
2364 if (plcf->vars.uri.len) {
|
|
2365 pr->replacement.text = plcf->location;
|
202
|
2366
|
|
2367 } else {
|
|
2368 pr->replacement.text.len = 0;
|
|
2369 pr->replacement.text.data = NULL;
|
|
2370 }
|
58
|
2371
|
|
2372 return NGX_CONF_OK;
|
|
2373 }
|
|
2374
|
|
2375 if (ngx_http_script_variables_count(&value[2]) == 0) {
|
|
2376 pr->handler = ngx_http_proxy_rewrite_redirect_text;
|
|
2377 pr->redirect = value[1];
|
|
2378 pr->replacement.text = value[2];
|
|
2379
|
|
2380 return NGX_CONF_OK;
|
|
2381 }
|
|
2382
|
|
2383 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2384
|
|
2385 vars_lengths = NULL;
|
|
2386 vars_values = NULL;
|
|
2387
|
|
2388 sc.cf = cf;
|
|
2389 sc.source = &value[2];
|
|
2390 sc.lengths = &vars_lengths;
|
|
2391 sc.values = &vars_values;
|
|
2392 sc.complete_lengths = 1;
|
|
2393 sc.complete_values = 1;
|
|
2394
|
|
2395 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2396 return NGX_CONF_ERROR;
|
|
2397 }
|
|
2398
|
|
2399 pr->handler = ngx_http_proxy_rewrite_redirect_vars;
|
|
2400 pr->redirect = value[1];
|
|
2401 pr->replacement.vars.lengths = vars_lengths->elts;
|
|
2402 pr->replacement.vars.values = vars_values->elts;
|
|
2403
|
|
2404 return NGX_CONF_OK;
|
|
2405 }
|
|
2406
|
|
2407
|
|
2408 static char *
|
320
|
2409 ngx_http_proxy_store(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
2410 {
|
|
2411 ngx_http_proxy_loc_conf_t *plcf = conf;
|
|
2412
|
|
2413 ngx_str_t *value;
|
|
2414 ngx_http_script_compile_t sc;
|
|
2415
|
|
2416 if (plcf->upstream.store != NGX_CONF_UNSET || plcf->upstream.store_lengths)
|
|
2417 {
|
|
2418 return "is duplicate";
|
|
2419 }
|
|
2420
|
|
2421 value = cf->args->elts;
|
|
2422
|
|
2423 if (ngx_strcmp(value[1].data, "on") == 0) {
|
|
2424 plcf->upstream.store = 1;
|
|
2425 return NGX_CONF_OK;
|
|
2426 }
|
|
2427
|
|
2428 if (ngx_strcmp(value[1].data, "off") == 0) {
|
|
2429 plcf->upstream.store = 0;
|
|
2430 return NGX_CONF_OK;
|
|
2431 }
|
|
2432
|
|
2433 /* include the terminating '\0' into script */
|
|
2434 value[1].len++;
|
|
2435
|
|
2436 ngx_memzero(&sc, sizeof(ngx_http_script_compile_t));
|
|
2437
|
|
2438 sc.cf = cf;
|
|
2439 sc.source = &value[1];
|
|
2440 sc.lengths = &plcf->upstream.store_lengths;
|
|
2441 sc.values = &plcf->upstream.store_values;
|
340
|
2442 sc.variables = ngx_http_script_variables_count(&value[1]);
|
320
|
2443 sc.complete_lengths = 1;
|
|
2444 sc.complete_values = 1;
|
|
2445
|
|
2446 if (ngx_http_script_compile(&sc) != NGX_OK) {
|
|
2447 return NGX_CONF_ERROR;
|
|
2448 }
|
|
2449
|
|
2450 return NGX_CONF_OK;
|
|
2451 }
|
|
2452
|
|
2453
|
|
2454 static char *
|
58
|
2455 ngx_http_proxy_lowat_check(ngx_conf_t *cf, void *post, void *data)
|
|
2456 {
|
|
2457 #if (NGX_FREEBSD)
|
|
2458 ssize_t *np = data;
|
|
2459
|
222
|
2460 if ((u_long) *np >= ngx_freebsd_net_inet_tcp_sendspace) {
|
58
|
2461 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2462 "\"proxy_send_lowat\" must be less than %d "
|
|
2463 "(sysctl net.inet.tcp.sendspace)",
|
|
2464 ngx_freebsd_net_inet_tcp_sendspace);
|
|
2465
|
|
2466 return NGX_CONF_ERROR;
|
|
2467 }
|
|
2468
|
|
2469 #elif !(NGX_HAVE_SO_SNDLOWAT)
|
|
2470 ssize_t *np = data;
|
|
2471
|
|
2472 ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
|
|
2473 "\"proxy_send_lowat\" is not supported, ignored");
|
|
2474
|
|
2475 *np = 0;
|
|
2476
|
|
2477 #endif
|
|
2478
|
|
2479 return NGX_CONF_OK;
|
|
2480 }
|
260
|
2481
|
|
2482
|
|
2483 static char *
|
|
2484 ngx_http_proxy_upstream_max_fails_unsupported(ngx_conf_t *cf,
|
|
2485 ngx_command_t *cmd, void *conf)
|
|
2486 {
|
|
2487 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2488 "\"proxy_upstream_max_fails\" is not supported, "
|
|
2489 "use the \"max_fails\" parameter of the \"server\" directive ",
|
|
2490 "inside the \"upstream\" block");
|
|
2491
|
|
2492 return NGX_CONF_ERROR;
|
|
2493 }
|
|
2494
|
|
2495
|
|
2496 static char *
|
|
2497 ngx_http_proxy_upstream_fail_timeout_unsupported(ngx_conf_t *cf,
|
|
2498 ngx_command_t *cmd, void *conf)
|
|
2499 {
|
|
2500 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
2501 "\"proxy_upstream_fail_timeout\" is not supported, "
|
|
2502 "use the \"fail_timeout\" parameter of the \"server\" directive ",
|
|
2503 "inside the \"upstream\" block");
|
|
2504
|
|
2505 return NGX_CONF_ERROR;
|
|
2506 }
|
348
|
2507
|
|
2508
|
|
2509 #if (NGX_HTTP_SSL)
|
|
2510
|
|
2511 static ngx_int_t
|
|
2512 ngx_http_proxy_set_ssl(ngx_conf_t *cf, ngx_http_proxy_loc_conf_t *plcf)
|
|
2513 {
|
|
2514 ngx_pool_cleanup_t *cln;
|
|
2515
|
|
2516 plcf->upstream.ssl = ngx_pcalloc(cf->pool, sizeof(ngx_ssl_t));
|
|
2517 if (plcf->upstream.ssl == NULL) {
|
|
2518 return NGX_ERROR;
|
|
2519 }
|
|
2520
|
|
2521 plcf->upstream.ssl->log = cf->log;
|
|
2522
|
|
2523 if (ngx_ssl_create(plcf->upstream.ssl,
|
|
2524 NGX_SSL_SSLv2|NGX_SSL_SSLv3|NGX_SSL_TLSv1, NULL)
|
|
2525 != NGX_OK)
|
|
2526 {
|
|
2527 return NGX_ERROR;
|
|
2528 }
|
|
2529
|
|
2530 cln = ngx_pool_cleanup_add(cf->pool, 0);
|
|
2531 if (cln == NULL) {
|
|
2532 return NGX_ERROR;
|
|
2533 }
|
|
2534
|
|
2535 cln->handler = ngx_ssl_cleanup_ctx;
|
|
2536 cln->data = plcf->upstream.ssl;
|
|
2537
|
|
2538 return NGX_OK;
|
|
2539 }
|
|
2540
|
|
2541 #endif
|
|
2542
|
|
2543
|
|
2544 static ngx_int_t
|
|
2545 ngx_http_proxy_set_vars(ngx_pool_t *pool, ngx_url_t *u,
|
|
2546 ngx_http_proxy_vars_t *v)
|
|
2547 {
|
400
|
2548 if (u->family != AF_UNIX) {
|
348
|
2549 if (u->no_port || u->port == u->default_port) {
|
|
2550 v->host_header = u->host;
|
|
2551
|
|
2552 if (u->default_port == 80) {
|
|
2553 v->port.len = sizeof("80") - 1;
|
|
2554 v->port.data = (u_char *) "80";
|
|
2555
|
|
2556 } else {
|
|
2557 v->port.len = sizeof("443") - 1;
|
|
2558 v->port.data = (u_char *) "443";
|
|
2559 }
|
|
2560
|
|
2561 } else {
|
354
|
2562 v->host_header.len = u->host.len + 1 + u->port_text.len;
|
|
2563 v->host_header.data = u->host.data;
|
|
2564 v->port = u->port_text;
|
348
|
2565 }
|
|
2566
|
|
2567 } else {
|
|
2568 v->host_header.len = sizeof("localhost") - 1;
|
|
2569 v->host_header.data = (u_char *) "localhost";
|
|
2570 v->port.len = 0;
|
|
2571 v->port.data = (u_char *) "";
|
|
2572 }
|
|
2573
|
|
2574 v->uri = u->uri;
|
|
2575
|
|
2576 return NGX_OK;
|
|
2577 }
|