blob: 95b396ee6fdc51254d3c89a0209c28b6a4e0d981 [file] [log] [blame]
Igor Sysoev02025fd2005-01-18 13:03:58 +00001
2/*
3 * Copyright (C) Igor Sysoev
4 */
5
6
7#ifndef _NGX_HTTP_UPSTREAM_H_INCLUDED_
8#define _NGX_HTTP_UPSTREAM_H_INCLUDED_
9
10
11#include <ngx_config.h>
12#include <ngx_core.h>
13#include <ngx_event.h>
14#include <ngx_event_connect.h>
15#include <ngx_event_pipe.h>
16#include <ngx_http.h>
17
18
Igor Sysoevbb28b6d2006-07-11 13:20:19 +000019#define NGX_HTTP_UPSTREAM_FT_ERROR 0x00000002
20#define NGX_HTTP_UPSTREAM_FT_TIMEOUT 0x00000004
21#define NGX_HTTP_UPSTREAM_FT_INVALID_HEADER 0x00000008
22#define NGX_HTTP_UPSTREAM_FT_HTTP_500 0x00000010
Igor Sysoev797c6ef2008-09-30 15:39:02 +000023#define NGX_HTTP_UPSTREAM_FT_HTTP_502 0x00000020
24#define NGX_HTTP_UPSTREAM_FT_HTTP_503 0x00000040
25#define NGX_HTTP_UPSTREAM_FT_HTTP_504 0x00000080
26#define NGX_HTTP_UPSTREAM_FT_HTTP_404 0x00000100
27#define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK 0x00000200
28#define NGX_HTTP_UPSTREAM_FT_MAX_WAITING 0x00000400
Igor Sysoev6876bcd2007-08-09 13:54:33 +000029#define NGX_HTTP_UPSTREAM_FT_NOLIVE 0x40000000
Igor Sysoevbb28b6d2006-07-11 13:20:19 +000030#define NGX_HTTP_UPSTREAM_FT_OFF 0x80000000
Igor Sysoev02025fd2005-01-18 13:03:58 +000031
Igor Sysoev797c6ef2008-09-30 15:39:02 +000032#define NGX_HTTP_UPSTREAM_FT_STATUS (NGX_HTTP_UPSTREAM_FT_HTTP_500 \
33 |NGX_HTTP_UPSTREAM_FT_HTTP_502 \
34 |NGX_HTTP_UPSTREAM_FT_HTTP_503 \
35 |NGX_HTTP_UPSTREAM_FT_HTTP_504 \
36 |NGX_HTTP_UPSTREAM_FT_HTTP_404)
Igor Sysoev02025fd2005-01-18 13:03:58 +000037
38#define NGX_HTTP_UPSTREAM_INVALID_HEADER 40
39
40
Igor Sysoevb01c9cc2009-04-30 16:15:07 +000041#define NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT 0x00000002
42#define NGX_HTTP_UPSTREAM_IGN_XA_EXPIRES 0x00000004
43#define NGX_HTTP_UPSTREAM_IGN_EXPIRES 0x00000008
44#define NGX_HTTP_UPSTREAM_IGN_CACHE_CONTROL 0x00000010
45
46
Igor Sysoev02025fd2005-01-18 13:03:58 +000047typedef struct {
Igor Sysoev52859f22009-03-23 13:14:51 +000048 ngx_msec_t bl_time;
49 ngx_uint_t bl_state;
Igor Sysoev02025fd2005-01-18 13:03:58 +000050
Igor Sysoev52859f22009-03-23 13:14:51 +000051 ngx_uint_t status;
52 time_t response_sec;
53 ngx_uint_t response_msec;
Igor Sysoev371766c2008-12-11 15:30:52 +000054 off_t response_length;
Igor Sysoevc2068d02005-10-19 12:33:58 +000055
Igor Sysoev52859f22009-03-23 13:14:51 +000056 ngx_str_t *peer;
Igor Sysoev02025fd2005-01-18 13:03:58 +000057} ngx_http_upstream_state_t;
58
59
60typedef struct {
Igor Sysoev52859f22009-03-23 13:14:51 +000061 ngx_hash_t headers_in_hash;
62 ngx_array_t upstreams;
Igor Sysoev3d2fd182006-12-04 16:46:13 +000063 /* ngx_http_upstream_srv_conf_t */
Igor Sysoev899b44e2005-05-12 14:58:06 +000064} ngx_http_upstream_main_conf_t;
Igor Sysoev02025fd2005-01-18 13:03:58 +000065
Igor Sysoev3d2fd182006-12-04 16:46:13 +000066typedef struct ngx_http_upstream_srv_conf_s ngx_http_upstream_srv_conf_t;
67
68typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t *cf,
69 ngx_http_upstream_srv_conf_t *us);
70typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(ngx_http_request_t *r,
71 ngx_http_upstream_srv_conf_t *us);
72
Igor Sysoev02025fd2005-01-18 13:03:58 +000073
Igor Sysoev899b44e2005-05-12 14:58:06 +000074typedef struct {
Igor Sysoev52859f22009-03-23 13:14:51 +000075 ngx_http_upstream_init_pt init_upstream;
76 ngx_http_upstream_init_peer_pt init;
77 void *data;
Igor Sysoev3d2fd182006-12-04 16:46:13 +000078} ngx_http_upstream_peer_t;
Igor Sysoev6f134cc2006-05-23 14:54:58 +000079
Igor Sysoev6f134cc2006-05-23 14:54:58 +000080
Igor Sysoev3d2fd182006-12-04 16:46:13 +000081typedef struct {
Igor Sysoev52859f22009-03-23 13:14:51 +000082 ngx_peer_addr_t *addrs;
83 ngx_uint_t naddrs;
84 ngx_uint_t weight;
85 ngx_uint_t max_fails;
86 time_t fail_timeout;
Igor Sysoev3d2fd182006-12-04 16:46:13 +000087
Igor Sysoev52859f22009-03-23 13:14:51 +000088 unsigned down:1;
89 unsigned backup:1;
Igor Sysoev3d2fd182006-12-04 16:46:13 +000090} ngx_http_upstream_server_t;
91
92
93#define NGX_HTTP_UPSTREAM_CREATE 0x0001
94#define NGX_HTTP_UPSTREAM_WEIGHT 0x0002
95#define NGX_HTTP_UPSTREAM_MAX_FAILS 0x0004
96#define NGX_HTTP_UPSTREAM_FAIL_TIMEOUT 0x0008
97#define NGX_HTTP_UPSTREAM_DOWN 0x0010
98#define NGX_HTTP_UPSTREAM_BACKUP 0x0020
99
100
101struct ngx_http_upstream_srv_conf_s {
Igor Sysoev52859f22009-03-23 13:14:51 +0000102 ngx_http_upstream_peer_t peer;
103 void **srv_conf;
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000104
Igor Sysoev52859f22009-03-23 13:14:51 +0000105 ngx_array_t *servers; /* ngx_http_upstream_server_t */
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000106
Igor Sysoev52859f22009-03-23 13:14:51 +0000107 ngx_uint_t flags;
108 ngx_str_t host;
109 u_char *file_name;
110 ngx_uint_t line;
111 in_port_t port;
112 in_port_t default_port;
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000113};
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000114
115
116typedef struct {
Igor Sysoev52859f22009-03-23 13:14:51 +0000117 ngx_http_upstream_srv_conf_t *upstream;
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000118
Igor Sysoev52859f22009-03-23 13:14:51 +0000119 ngx_msec_t connect_timeout;
120 ngx_msec_t send_timeout;
121 ngx_msec_t read_timeout;
122 ngx_msec_t timeout;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000123
Igor Sysoev52859f22009-03-23 13:14:51 +0000124 size_t send_lowat;
125 size_t buffer_size;
Igor Sysoev187b7d92005-07-14 12:51:53 +0000126
Igor Sysoev52859f22009-03-23 13:14:51 +0000127 size_t busy_buffers_size;
128 size_t max_temp_file_size;
129 size_t temp_file_write_size;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000130
Igor Sysoev52859f22009-03-23 13:14:51 +0000131 size_t busy_buffers_size_conf;
132 size_t max_temp_file_size_conf;
133 size_t temp_file_write_size_conf;
Igor Sysoev187b7d92005-07-14 12:51:53 +0000134
Igor Sysoev52859f22009-03-23 13:14:51 +0000135 ngx_bufs_t bufs;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000136
Igor Sysoevb01c9cc2009-04-30 16:15:07 +0000137 ngx_uint_t ignore_headers;
Igor Sysoev52859f22009-03-23 13:14:51 +0000138 ngx_uint_t next_upstream;
139 ngx_uint_t store_access;
140 ngx_flag_t buffering;
141 ngx_flag_t pass_request_headers;
142 ngx_flag_t pass_request_body;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000143
Igor Sysoev52859f22009-03-23 13:14:51 +0000144 ngx_flag_t ignore_client_abort;
145 ngx_flag_t intercept_errors;
146 ngx_flag_t cyclic_temp_file;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000147
Igor Sysoev52859f22009-03-23 13:14:51 +0000148 ngx_path_t *temp_path;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000149
Igor Sysoev52859f22009-03-23 13:14:51 +0000150 ngx_hash_t hide_headers_hash;
151 ngx_array_t *hide_headers;
152 ngx_array_t *pass_headers;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000153
Igor Sysoev52859f22009-03-23 13:14:51 +0000154#if (NGX_HTTP_CACHE)
155 ngx_shm_zone_t *cache;
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000156
Igor Sysoev52859f22009-03-23 13:14:51 +0000157 ngx_uint_t cache_min_uses;
158 ngx_uint_t cache_use_stale;
Igor Sysoevfbd9b432007-07-13 08:30:34 +0000159
Igor Sysoev52859f22009-03-23 13:14:51 +0000160 ngx_array_t *cache_valid;
161#endif
162
163 ngx_array_t *store_lengths;
164 ngx_array_t *store_values;
165
166 signed store:2;
167 unsigned intercept_404:1;
168 unsigned change_buffering:1;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000169
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000170#if (NGX_HTTP_SSL)
Igor Sysoev52859f22009-03-23 13:14:51 +0000171 ngx_ssl_t *ssl;
Igor Sysoev43e1b882009-04-24 19:58:19 +0000172 ngx_flag_t ssl_session_reuse;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000173#endif
174
Igor Sysoev02025fd2005-01-18 13:03:58 +0000175} ngx_http_upstream_conf_t;
176
177
Igor Sysoev899b44e2005-05-12 14:58:06 +0000178typedef struct {
Igor Sysoev52859f22009-03-23 13:14:51 +0000179 ngx_str_t name;
180 ngx_http_header_handler_pt handler;
181 ngx_uint_t offset;
182 ngx_http_header_handler_pt copy_handler;
183 ngx_uint_t conf;
184 ngx_uint_t redirect; /* unsigned redirect:1; */
Igor Sysoev899b44e2005-05-12 14:58:06 +0000185} ngx_http_upstream_header_t;
186
187
188typedef struct {
Igor Sysoev52859f22009-03-23 13:14:51 +0000189 ngx_list_t headers;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000190
Igor Sysoev52859f22009-03-23 13:14:51 +0000191 ngx_uint_t status_n;
192 ngx_str_t status_line;
Igor Sysoev187b7d92005-07-14 12:51:53 +0000193
Igor Sysoev52859f22009-03-23 13:14:51 +0000194 ngx_table_elt_t *status;
195 ngx_table_elt_t *date;
196 ngx_table_elt_t *server;
197 ngx_table_elt_t *connection;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000198
Igor Sysoev52859f22009-03-23 13:14:51 +0000199 ngx_table_elt_t *expires;
200 ngx_table_elt_t *etag;
201 ngx_table_elt_t *x_accel_expires;
202 ngx_table_elt_t *x_accel_redirect;
203 ngx_table_elt_t *x_accel_limit_rate;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000204
Igor Sysoev52859f22009-03-23 13:14:51 +0000205 ngx_table_elt_t *content_type;
206 ngx_table_elt_t *content_length;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000207
Igor Sysoev52859f22009-03-23 13:14:51 +0000208 ngx_table_elt_t *last_modified;
209 ngx_table_elt_t *location;
210 ngx_table_elt_t *accept_ranges;
211 ngx_table_elt_t *www_authenticate;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000212
213#if (NGX_HTTP_GZIP)
Igor Sysoev52859f22009-03-23 13:14:51 +0000214 ngx_table_elt_t *content_encoding;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000215#endif
216
Igor Sysoev52859f22009-03-23 13:14:51 +0000217 off_t content_length_n;
Igor Sysoevbe0a61e2008-02-11 13:14:56 +0000218
Igor Sysoev52859f22009-03-23 13:14:51 +0000219 ngx_array_t cache_control;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000220} ngx_http_upstream_headers_in_t;
221
Igor Sysoev02025fd2005-01-18 13:03:58 +0000222
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000223typedef struct {
Igor Sysoev52859f22009-03-23 13:14:51 +0000224 ngx_str_t host;
225 in_port_t port;
226 ngx_uint_t no_port; /* unsigned no_port:1 */
Igor Sysoev302cedc2008-12-23 19:35:12 +0000227
Igor Sysoev52859f22009-03-23 13:14:51 +0000228 ngx_uint_t naddrs;
229 in_addr_t *addrs;
Igor Sysoev302cedc2008-12-23 19:35:12 +0000230
Igor Sysoev52859f22009-03-23 13:14:51 +0000231 struct sockaddr *sockaddr;
232 socklen_t socklen;
Igor Sysoev302cedc2008-12-23 19:35:12 +0000233
Igor Sysoev52859f22009-03-23 13:14:51 +0000234 ngx_resolver_ctx_t *ctx;
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000235} ngx_http_upstream_resolved_t;
236
237
Igor Sysoevb9409a82008-12-09 17:25:03 +0000238typedef void (*ngx_http_upstream_handler_pt)(ngx_http_request_t *r,
239 ngx_http_upstream_t *u);
240
241
Igor Sysoev02025fd2005-01-18 13:03:58 +0000242struct ngx_http_upstream_s {
Igor Sysoev52859f22009-03-23 13:14:51 +0000243 ngx_http_upstream_handler_pt read_event_handler;
244 ngx_http_upstream_handler_pt write_event_handler;
Igor Sysoev851cd732008-12-08 14:23:20 +0000245
Igor Sysoev52859f22009-03-23 13:14:51 +0000246 ngx_peer_connection_t peer;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000247
Igor Sysoev52859f22009-03-23 13:14:51 +0000248 ngx_event_pipe_t *pipe;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000249
Igor Sysoev52859f22009-03-23 13:14:51 +0000250 ngx_chain_t *request_bufs;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000251
Igor Sysoev52859f22009-03-23 13:14:51 +0000252 ngx_output_chain_ctx_t output;
253 ngx_chain_writer_ctx_t writer;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000254
Igor Sysoev52859f22009-03-23 13:14:51 +0000255 ngx_http_upstream_conf_t *conf;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000256
Igor Sysoev52859f22009-03-23 13:14:51 +0000257 ngx_http_upstream_headers_in_t headers_in;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000258
Igor Sysoev52859f22009-03-23 13:14:51 +0000259 ngx_http_upstream_resolved_t *resolved;
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000260
Igor Sysoev52859f22009-03-23 13:14:51 +0000261 ngx_buf_t buffer;
262 size_t length;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000263
Igor Sysoev52859f22009-03-23 13:14:51 +0000264 ngx_chain_t *out_bufs;
265 ngx_chain_t *busy_bufs;
266 ngx_chain_t *free_bufs;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000267
Igor Sysoev52859f22009-03-23 13:14:51 +0000268 ngx_int_t (*input_filter_init)(void *data);
269 ngx_int_t (*input_filter)(void *data, ssize_t bytes);
270 void *input_filter_ctx;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000271
Igor Sysoev52859f22009-03-23 13:14:51 +0000272#if (NGX_HTTP_CACHE)
273 ngx_int_t (*create_key)(ngx_http_request_t *r);
274#endif
275 ngx_int_t (*create_request)(ngx_http_request_t *r);
276 ngx_int_t (*reinit_request)(ngx_http_request_t *r);
277 ngx_int_t (*process_header)(ngx_http_request_t *r);
278 void (*abort_request)(ngx_http_request_t *r);
279 void (*finalize_request)(ngx_http_request_t *r,
280 ngx_int_t rc);
281 ngx_int_t (*rewrite_redirect)(ngx_http_request_t *r,
282 ngx_table_elt_t *h, size_t prefix);
Igor Sysoev02025fd2005-01-18 13:03:58 +0000283
Igor Sysoev52859f22009-03-23 13:14:51 +0000284 ngx_msec_t timeout;
Igor Sysoev5192b362005-07-08 14:34:20 +0000285
Igor Sysoev52859f22009-03-23 13:14:51 +0000286 ngx_http_upstream_state_t *state;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000287
Igor Sysoev52859f22009-03-23 13:14:51 +0000288 ngx_str_t method;
289 ngx_str_t schema;
290 ngx_str_t uri;
Igor Sysoeva2573672005-10-05 14:46:21 +0000291
Igor Sysoev52859f22009-03-23 13:14:51 +0000292 ngx_http_cleanup_pt *cleanup;
Igor Sysoev9ac946b2005-10-24 15:09:41 +0000293
Igor Sysoev52859f22009-03-23 13:14:51 +0000294 unsigned store:1;
295 unsigned cacheable:1;
296 unsigned accel:1;
297 unsigned ssl:1;
298#if (NGX_HTTP_CACHE)
299 unsigned stale_cache:1;
300#endif
Igor Sysoev899b44e2005-05-12 14:58:06 +0000301
Igor Sysoev52859f22009-03-23 13:14:51 +0000302 unsigned buffering:1;
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000303
Igor Sysoev52859f22009-03-23 13:14:51 +0000304 unsigned request_sent:1;
305 unsigned header_sent:1;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000306};
307
308
Igor Sysoev797c6ef2008-09-30 15:39:02 +0000309typedef struct {
310 ngx_uint_t status;
311 ngx_uint_t mask;
312} ngx_http_upstream_next_t;
313
314
Igor Sysoev27233612007-04-10 07:08:06 +0000315ngx_int_t ngx_http_upstream_header_variable(ngx_http_request_t *r,
316 ngx_http_variable_value_t *v, uintptr_t data);
317
Igor Sysoev02025fd2005-01-18 13:03:58 +0000318void ngx_http_upstream_init(ngx_http_request_t *r);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000319ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf,
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000320 ngx_url_t *u, ngx_uint_t flags);
Igor Sysoevcb540612007-12-09 18:03:20 +0000321ngx_int_t ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
322 ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
323 ngx_str_t *default_hide_headers, ngx_hash_init_t *hash);
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000324
325
326#define ngx_http_conf_upstream_srv_conf(uscf, module) \
327 uscf->srv_conf[module.ctx_index]
Igor Sysoev02025fd2005-01-18 13:03:58 +0000328
329
Igor Sysoev899b44e2005-05-12 14:58:06 +0000330extern ngx_module_t ngx_http_upstream_module;
331
Igor Sysoev02025fd2005-01-18 13:03:58 +0000332
333#endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */