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