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