blob: 83953626bcd41750f27d820a6f526dbde3841562 [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
23#define NGX_HTTP_UPSTREAM_FT_HTTP_503 0x00000020
24#define NGX_HTTP_UPSTREAM_FT_HTTP_404 0x00000040
25#define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK 0x00000080
26#define NGX_HTTP_UPSTREAM_FT_MAX_WAITING 0x00000100
Igor Sysoev6876bcd2007-08-09 13:54:33 +000027#define NGX_HTTP_UPSTREAM_FT_NOLIVE 0x40000000
Igor Sysoevbb28b6d2006-07-11 13:20:19 +000028#define NGX_HTTP_UPSTREAM_FT_OFF 0x80000000
Igor Sysoev02025fd2005-01-18 13:03:58 +000029
30
31#define NGX_HTTP_UPSTREAM_INVALID_HEADER 40
32
33
34typedef struct {
Igor Sysoevc2068d02005-10-19 12:33:58 +000035 ngx_msec_t bl_time;
Igor Sysoev899b44e2005-05-12 14:58:06 +000036 ngx_uint_t bl_state;
Igor Sysoev02025fd2005-01-18 13:03:58 +000037
Igor Sysoev899b44e2005-05-12 14:58:06 +000038 ngx_uint_t status;
Igor Sysoev8f985812006-12-06 11:33:18 +000039 time_t response_sec;
40 ngx_uint_t response_msec;
Igor Sysoevc2068d02005-10-19 12:33:58 +000041
Igor Sysoev899b44e2005-05-12 14:58:06 +000042 ngx_str_t *peer;
Igor Sysoev02025fd2005-01-18 13:03:58 +000043} ngx_http_upstream_state_t;
44
45
46typedef struct {
Igor Sysoev3338cfd2006-05-11 14:43:47 +000047 ngx_hash_t headers_in_hash;
Igor Sysoev6f134cc2006-05-23 14:54:58 +000048 ngx_array_t upstreams;
Igor Sysoev3d2fd182006-12-04 16:46:13 +000049 /* ngx_http_upstream_srv_conf_t */
Igor Sysoev899b44e2005-05-12 14:58:06 +000050} ngx_http_upstream_main_conf_t;
Igor Sysoev02025fd2005-01-18 13:03:58 +000051
Igor Sysoev3d2fd182006-12-04 16:46:13 +000052typedef struct ngx_http_upstream_srv_conf_s ngx_http_upstream_srv_conf_t;
53
54typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t *cf,
55 ngx_http_upstream_srv_conf_t *us);
56typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(ngx_http_request_t *r,
57 ngx_http_upstream_srv_conf_t *us);
58
Igor Sysoev02025fd2005-01-18 13:03:58 +000059
Igor Sysoev899b44e2005-05-12 14:58:06 +000060typedef struct {
Igor Sysoev3d2fd182006-12-04 16:46:13 +000061 ngx_http_upstream_init_pt init_upstream;
62 ngx_http_upstream_init_peer_pt init;
63 void *data;
64} ngx_http_upstream_peer_t;
Igor Sysoev6f134cc2006-05-23 14:54:58 +000065
Igor Sysoev6f134cc2006-05-23 14:54:58 +000066
Igor Sysoev3d2fd182006-12-04 16:46:13 +000067typedef struct {
68 ngx_peer_addr_t *addrs;
69 ngx_uint_t naddrs;
70 ngx_uint_t weight;
71 ngx_uint_t max_fails;
72 time_t fail_timeout;
73
74 unsigned down:1;
75 unsigned backup:1;
76} ngx_http_upstream_server_t;
77
78
79#define NGX_HTTP_UPSTREAM_CREATE 0x0001
80#define NGX_HTTP_UPSTREAM_WEIGHT 0x0002
81#define NGX_HTTP_UPSTREAM_MAX_FAILS 0x0004
82#define NGX_HTTP_UPSTREAM_FAIL_TIMEOUT 0x0008
83#define NGX_HTTP_UPSTREAM_DOWN 0x0010
84#define NGX_HTTP_UPSTREAM_BACKUP 0x0020
85
86
87struct ngx_http_upstream_srv_conf_s {
88 ngx_http_upstream_peer_t peer;
89 void **srv_conf;
90
91 ngx_array_t *servers; /* ngx_http_upstream_server_t */
92
93 ngx_uint_t flags;
Igor Sysoev6f134cc2006-05-23 14:54:58 +000094 ngx_str_t host;
Igor Sysoev4b96baa2007-09-15 17:11:06 +000095 u_char *file_name;
Igor Sysoev6f134cc2006-05-23 14:54:58 +000096 ngx_uint_t line;
97 in_port_t port;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +000098 in_port_t default_port;
Igor Sysoev3d2fd182006-12-04 16:46:13 +000099};
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000100
101
102typedef struct {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000103 ngx_http_upstream_srv_conf_t *upstream;
104
Igor Sysoev899b44e2005-05-12 14:58:06 +0000105 ngx_msec_t connect_timeout;
106 ngx_msec_t send_timeout;
107 ngx_msec_t read_timeout;
Igor Sysoev5192b362005-07-08 14:34:20 +0000108 ngx_msec_t timeout;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000109
Igor Sysoev899b44e2005-05-12 14:58:06 +0000110 size_t send_lowat;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000111 size_t buffer_size;
Igor Sysoev187b7d92005-07-14 12:51:53 +0000112
Igor Sysoev899b44e2005-05-12 14:58:06 +0000113 size_t busy_buffers_size;
114 size_t max_temp_file_size;
115 size_t temp_file_write_size;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000116
Igor Sysoev187b7d92005-07-14 12:51:53 +0000117 size_t busy_buffers_size_conf;
118 size_t max_temp_file_size_conf;
119 size_t temp_file_write_size_conf;
120
Igor Sysoev899b44e2005-05-12 14:58:06 +0000121 ngx_uint_t next_upstream;
Igor Sysoevfbd9b432007-07-13 08:30:34 +0000122 ngx_uint_t store_access;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000123
Igor Sysoev899b44e2005-05-12 14:58:06 +0000124 ngx_bufs_t bufs;
125
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000126 ngx_flag_t buffering;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000127 ngx_flag_t pass_request_headers;
128 ngx_flag_t pass_request_body;
129
Igor Sysoev6d16e1e2006-04-05 13:40:54 +0000130 ngx_flag_t ignore_client_abort;
Igor Sysoevef809b82006-06-28 16:00:26 +0000131 ngx_flag_t intercept_errors;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000132 ngx_flag_t cyclic_temp_file;
133
Igor Sysoev899b44e2005-05-12 14:58:06 +0000134 ngx_path_t *temp_path;
135
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000136 ngx_hash_t hide_headers_hash;
137 ngx_array_t *hide_headers;
138 ngx_array_t *pass_headers;
139
Igor Sysoev899b44e2005-05-12 14:58:06 +0000140 ngx_str_t schema;
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000141
Igor Sysoevfbd9b432007-07-13 08:30:34 +0000142 ngx_array_t *store_lengths;
143 ngx_array_t *store_values;
144
145 signed store:2;
Igor Sysoevef316432006-08-16 13:09:33 +0000146 unsigned intercept_404:1;
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000147 unsigned change_buffering:1;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000148
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000149#if (NGX_HTTP_SSL)
150 ngx_ssl_t *ssl;
151#endif
152
Igor Sysoev02025fd2005-01-18 13:03:58 +0000153} ngx_http_upstream_conf_t;
154
155
Igor Sysoev899b44e2005-05-12 14:58:06 +0000156typedef struct {
157 ngx_str_t name;
158 ngx_http_header_handler_pt handler;
159 ngx_uint_t offset;
160 ngx_http_header_handler_pt copy_handler;
161 ngx_uint_t conf;
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000162 ngx_uint_t redirect; /* unsigned redirect:1; */
Igor Sysoev899b44e2005-05-12 14:58:06 +0000163} ngx_http_upstream_header_t;
164
165
166typedef struct {
167 ngx_list_t headers;
168
Igor Sysoev187b7d92005-07-14 12:51:53 +0000169 ngx_uint_t status_n;
170 ngx_str_t status_line;
171
Igor Sysoev899b44e2005-05-12 14:58:06 +0000172 ngx_table_elt_t *status;
173 ngx_table_elt_t *date;
174 ngx_table_elt_t *server;
175 ngx_table_elt_t *connection;
176
177 ngx_table_elt_t *expires;
178 ngx_table_elt_t *etag;
179 ngx_table_elt_t *x_accel_expires;
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000180 ngx_table_elt_t *x_accel_redirect;
Igor Sysoev5192b362005-07-08 14:34:20 +0000181 ngx_table_elt_t *x_accel_limit_rate;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000182
183 ngx_table_elt_t *content_type;
184 ngx_table_elt_t *content_length;
185
186 ngx_table_elt_t *last_modified;
187 ngx_table_elt_t *location;
188 ngx_table_elt_t *accept_ranges;
Igor Sysoev187b7d92005-07-14 12:51:53 +0000189 ngx_table_elt_t *www_authenticate;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000190
191#if (NGX_HTTP_GZIP)
192 ngx_table_elt_t *content_encoding;
193#endif
194
195 ngx_array_t cache_control;
196} ngx_http_upstream_headers_in_t;
197
Igor Sysoev02025fd2005-01-18 13:03:58 +0000198
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000199typedef struct {
200 ngx_str_t host;
201 in_port_t port;
202 ngx_uint_t naddrs;
203 in_addr_t *addrs;
204 ngx_resolver_ctx_t *ctx;
205} ngx_http_upstream_resolved_t;
206
207
Igor Sysoev02025fd2005-01-18 13:03:58 +0000208struct ngx_http_upstream_s {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000209 ngx_peer_connection_t peer;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000210
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000211 ngx_event_pipe_t *pipe;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000212
Igor Sysoev899b44e2005-05-12 14:58:06 +0000213 ngx_chain_t *request_bufs;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000214
Igor Sysoev899b44e2005-05-12 14:58:06 +0000215 ngx_output_chain_ctx_t output;
216 ngx_chain_writer_ctx_t writer;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000217
Igor Sysoev899b44e2005-05-12 14:58:06 +0000218 ngx_http_upstream_conf_t *conf;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000219
Igor Sysoev899b44e2005-05-12 14:58:06 +0000220 ngx_http_upstream_headers_in_t headers_in;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000221
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000222 ngx_http_upstream_resolved_t *resolved;
223
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000224 ngx_buf_t buffer;
225 size_t length;
226
227 ngx_chain_t *out_bufs;
228 ngx_chain_t *busy_bufs;
229 ngx_chain_t *free_bufs;
230
231 ngx_int_t (*input_filter_init)(void *data);
232 ngx_int_t (*input_filter)(void *data, ssize_t bytes);
233 void *input_filter_ctx;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000234
Igor Sysoev899b44e2005-05-12 14:58:06 +0000235 ngx_int_t (*create_request)(ngx_http_request_t *r);
236 ngx_int_t (*reinit_request)(ngx_http_request_t *r);
237 ngx_int_t (*process_header)(ngx_http_request_t *r);
238 void (*abort_request)(ngx_http_request_t *r);
239 void (*finalize_request)(ngx_http_request_t *r,
240 ngx_int_t rc);
241 ngx_int_t (*rewrite_redirect)(ngx_http_request_t *r,
242 ngx_table_elt_t *h, size_t prefix);
Igor Sysoev02025fd2005-01-18 13:03:58 +0000243
Igor Sysoev5192b362005-07-08 14:34:20 +0000244 ngx_msec_t timeout;
245
Igor Sysoev899b44e2005-05-12 14:58:06 +0000246 ngx_http_upstream_state_t *state;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000247
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000248 ngx_str_t method;
249 ngx_str_t schema;
Igor Sysoeva2573672005-10-05 14:46:21 +0000250 ngx_str_t uri;
251
Igor Sysoev9ac946b2005-10-24 15:09:41 +0000252 ngx_http_cleanup_pt *cleanup;
253
Igor Sysoev58feb532007-07-12 11:19:05 +0000254 unsigned store:1;
Igor Sysoev2d3f3f62007-10-14 18:56:15 +0000255 unsigned cacheable:1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000256 unsigned accel:1;
Igor Sysoev96dd8af2007-11-27 13:34:13 +0000257 unsigned ssl:1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000258
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000259 unsigned buffering:1;
260
Igor Sysoev899b44e2005-05-12 14:58:06 +0000261 unsigned request_sent:1;
262 unsigned header_sent:1;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000263};
264
265
Igor Sysoev27233612007-04-10 07:08:06 +0000266ngx_int_t ngx_http_upstream_header_variable(ngx_http_request_t *r,
267 ngx_http_variable_value_t *v, uintptr_t data);
268
Igor Sysoev02025fd2005-01-18 13:03:58 +0000269void ngx_http_upstream_init(ngx_http_request_t *r);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000270ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf,
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000271 ngx_url_t *u, ngx_uint_t flags);
272
273
274#define ngx_http_conf_upstream_srv_conf(uscf, module) \
275 uscf->srv_conf[module.ctx_index]
Igor Sysoev02025fd2005-01-18 13:03:58 +0000276
277
Igor Sysoev899b44e2005-05-12 14:58:06 +0000278extern ngx_module_t ngx_http_upstream_module;
279
Igor Sysoev02025fd2005-01-18 13:03:58 +0000280
281#endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */