blob: b3bb1b4fdb3ac4c8804f7aefde118f77446744d8 [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
27#define NGX_HTTP_UPSTREAM_FT_OFF 0x80000000
Igor Sysoev02025fd2005-01-18 13:03:58 +000028
29
30#define NGX_HTTP_UPSTREAM_INVALID_HEADER 40
31
32
33typedef struct {
Igor Sysoevc2068d02005-10-19 12:33:58 +000034 ngx_msec_t bl_time;
Igor Sysoev899b44e2005-05-12 14:58:06 +000035 ngx_uint_t bl_state;
Igor Sysoev02025fd2005-01-18 13:03:58 +000036
Igor Sysoev899b44e2005-05-12 14:58:06 +000037 ngx_uint_t status;
Igor Sysoev8f985812006-12-06 11:33:18 +000038 time_t response_sec;
39 ngx_uint_t response_msec;
Igor Sysoevc2068d02005-10-19 12:33:58 +000040
Igor Sysoev899b44e2005-05-12 14:58:06 +000041 ngx_str_t *peer;
Igor Sysoev02025fd2005-01-18 13:03:58 +000042} ngx_http_upstream_state_t;
43
44
45typedef struct {
Igor Sysoev3338cfd2006-05-11 14:43:47 +000046 ngx_hash_t headers_in_hash;
Igor Sysoev6f134cc2006-05-23 14:54:58 +000047 ngx_array_t upstreams;
Igor Sysoev3d2fd182006-12-04 16:46:13 +000048 /* ngx_http_upstream_srv_conf_t */
Igor Sysoev899b44e2005-05-12 14:58:06 +000049} ngx_http_upstream_main_conf_t;
Igor Sysoev02025fd2005-01-18 13:03:58 +000050
Igor Sysoev3d2fd182006-12-04 16:46:13 +000051typedef struct ngx_http_upstream_srv_conf_s ngx_http_upstream_srv_conf_t;
52
53typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t *cf,
54 ngx_http_upstream_srv_conf_t *us);
55typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(ngx_http_request_t *r,
56 ngx_http_upstream_srv_conf_t *us);
57
Igor Sysoev02025fd2005-01-18 13:03:58 +000058
Igor Sysoev899b44e2005-05-12 14:58:06 +000059typedef struct {
Igor Sysoev3d2fd182006-12-04 16:46:13 +000060 ngx_http_upstream_init_pt init_upstream;
61 ngx_http_upstream_init_peer_pt init;
62 void *data;
63} ngx_http_upstream_peer_t;
Igor Sysoev6f134cc2006-05-23 14:54:58 +000064
Igor Sysoev6f134cc2006-05-23 14:54:58 +000065
Igor Sysoev3d2fd182006-12-04 16:46:13 +000066typedef struct {
67 ngx_peer_addr_t *addrs;
68 ngx_uint_t naddrs;
69 ngx_uint_t weight;
70 ngx_uint_t max_fails;
71 time_t fail_timeout;
72
73 unsigned down:1;
74 unsigned backup:1;
75} ngx_http_upstream_server_t;
76
77
78#define NGX_HTTP_UPSTREAM_CREATE 0x0001
79#define NGX_HTTP_UPSTREAM_WEIGHT 0x0002
80#define NGX_HTTP_UPSTREAM_MAX_FAILS 0x0004
81#define NGX_HTTP_UPSTREAM_FAIL_TIMEOUT 0x0008
82#define NGX_HTTP_UPSTREAM_DOWN 0x0010
83#define NGX_HTTP_UPSTREAM_BACKUP 0x0020
84
85
86struct ngx_http_upstream_srv_conf_s {
87 ngx_http_upstream_peer_t peer;
88 void **srv_conf;
89
90 ngx_array_t *servers; /* ngx_http_upstream_server_t */
91
92 ngx_uint_t flags;
Igor Sysoev6f134cc2006-05-23 14:54:58 +000093 ngx_str_t host;
94 ngx_str_t file_name;
95 ngx_uint_t line;
96 in_port_t port;
Igor Sysoevbf3aaac2006-12-12 16:46:16 +000097 in_port_t default_port;
Igor Sysoev3d2fd182006-12-04 16:46:13 +000098};
Igor Sysoev6f134cc2006-05-23 14:54:58 +000099
100
101typedef struct {
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000102 ngx_http_upstream_srv_conf_t *upstream;
103
Igor Sysoev899b44e2005-05-12 14:58:06 +0000104 ngx_msec_t connect_timeout;
105 ngx_msec_t send_timeout;
106 ngx_msec_t read_timeout;
Igor Sysoev5192b362005-07-08 14:34:20 +0000107 ngx_msec_t timeout;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000108
Igor Sysoev899b44e2005-05-12 14:58:06 +0000109 size_t send_lowat;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000110 size_t buffer_size;
Igor Sysoev187b7d92005-07-14 12:51:53 +0000111
Igor Sysoev899b44e2005-05-12 14:58:06 +0000112 size_t busy_buffers_size;
113 size_t max_temp_file_size;
114 size_t temp_file_write_size;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000115
Igor Sysoev187b7d92005-07-14 12:51:53 +0000116 size_t busy_buffers_size_conf;
117 size_t max_temp_file_size_conf;
118 size_t temp_file_write_size_conf;
119
Igor Sysoev899b44e2005-05-12 14:58:06 +0000120 ngx_uint_t next_upstream;
Igor Sysoevfbd9b432007-07-13 08:30:34 +0000121 ngx_uint_t store_access;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000122
Igor Sysoev899b44e2005-05-12 14:58:06 +0000123 ngx_bufs_t bufs;
124
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000125 ngx_flag_t buffering;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000126 ngx_flag_t pass_request_headers;
127 ngx_flag_t pass_request_body;
128
Igor Sysoev6d16e1e2006-04-05 13:40:54 +0000129 ngx_flag_t ignore_client_abort;
Igor Sysoevef809b82006-06-28 16:00:26 +0000130 ngx_flag_t intercept_errors;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000131 ngx_flag_t cyclic_temp_file;
132
Igor Sysoev899b44e2005-05-12 14:58:06 +0000133 ngx_path_t *temp_path;
134
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000135 ngx_hash_t hide_headers_hash;
136 ngx_array_t *hide_headers;
137 ngx_array_t *pass_headers;
138
Igor Sysoev899b44e2005-05-12 14:58:06 +0000139 ngx_str_t schema;
140 ngx_str_t uri;
Igor Sysoev09c684b2005-11-09 17:25:55 +0000141 ngx_str_t location;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000142 ngx_str_t url; /* used in proxy_rewrite_location */
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000143
Igor Sysoevfbd9b432007-07-13 08:30:34 +0000144 ngx_array_t *store_lengths;
145 ngx_array_t *store_values;
146
147 signed store:2;
Igor Sysoevef316432006-08-16 13:09:33 +0000148 unsigned intercept_404:1;
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000149 unsigned change_buffering:1;
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000150
Igor Sysoev0e5dc5c2005-11-15 13:30:52 +0000151#if (NGX_HTTP_SSL)
152 ngx_ssl_t *ssl;
153#endif
154
Igor Sysoev02025fd2005-01-18 13:03:58 +0000155} ngx_http_upstream_conf_t;
156
157
Igor Sysoev899b44e2005-05-12 14:58:06 +0000158typedef struct {
159 ngx_str_t name;
160 ngx_http_header_handler_pt handler;
161 ngx_uint_t offset;
162 ngx_http_header_handler_pt copy_handler;
163 ngx_uint_t conf;
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000164 ngx_uint_t redirect; /* unsigned redirect:1; */
Igor Sysoev899b44e2005-05-12 14:58:06 +0000165} ngx_http_upstream_header_t;
166
167
168typedef struct {
169 ngx_list_t headers;
170
Igor Sysoev187b7d92005-07-14 12:51:53 +0000171 ngx_uint_t status_n;
172 ngx_str_t status_line;
173
Igor Sysoev899b44e2005-05-12 14:58:06 +0000174 ngx_table_elt_t *status;
175 ngx_table_elt_t *date;
176 ngx_table_elt_t *server;
177 ngx_table_elt_t *connection;
178
179 ngx_table_elt_t *expires;
180 ngx_table_elt_t *etag;
181 ngx_table_elt_t *x_accel_expires;
Igor Sysoeve31e90b2005-05-19 13:25:22 +0000182 ngx_table_elt_t *x_accel_redirect;
Igor Sysoev5192b362005-07-08 14:34:20 +0000183 ngx_table_elt_t *x_accel_limit_rate;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000184
185 ngx_table_elt_t *content_type;
186 ngx_table_elt_t *content_length;
187
188 ngx_table_elt_t *last_modified;
189 ngx_table_elt_t *location;
190 ngx_table_elt_t *accept_ranges;
Igor Sysoev187b7d92005-07-14 12:51:53 +0000191 ngx_table_elt_t *www_authenticate;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000192
193#if (NGX_HTTP_GZIP)
194 ngx_table_elt_t *content_encoding;
195#endif
196
197 ngx_array_t cache_control;
198} ngx_http_upstream_headers_in_t;
199
Igor Sysoev02025fd2005-01-18 13:03:58 +0000200
201struct ngx_http_upstream_s {
Igor Sysoev899b44e2005-05-12 14:58:06 +0000202 ngx_peer_connection_t peer;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000203
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000204 ngx_event_pipe_t *pipe;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000205
Igor Sysoev899b44e2005-05-12 14:58:06 +0000206 ngx_chain_t *request_bufs;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000207
Igor Sysoev899b44e2005-05-12 14:58:06 +0000208 ngx_output_chain_ctx_t output;
209 ngx_chain_writer_ctx_t writer;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000210
Igor Sysoev899b44e2005-05-12 14:58:06 +0000211 ngx_http_upstream_conf_t *conf;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000212
Igor Sysoev899b44e2005-05-12 14:58:06 +0000213 ngx_http_upstream_headers_in_t headers_in;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000214
Igor Sysoevc31a9bb2005-11-26 10:11:11 +0000215 ngx_buf_t buffer;
216 size_t length;
217
218 ngx_chain_t *out_bufs;
219 ngx_chain_t *busy_bufs;
220 ngx_chain_t *free_bufs;
221
222 ngx_int_t (*input_filter_init)(void *data);
223 ngx_int_t (*input_filter)(void *data, ssize_t bytes);
224 void *input_filter_ctx;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000225
Igor Sysoev899b44e2005-05-12 14:58:06 +0000226 ngx_int_t (*create_request)(ngx_http_request_t *r);
227 ngx_int_t (*reinit_request)(ngx_http_request_t *r);
228 ngx_int_t (*process_header)(ngx_http_request_t *r);
229 void (*abort_request)(ngx_http_request_t *r);
230 void (*finalize_request)(ngx_http_request_t *r,
231 ngx_int_t rc);
232 ngx_int_t (*rewrite_redirect)(ngx_http_request_t *r,
233 ngx_table_elt_t *h, size_t prefix);
Igor Sysoev02025fd2005-01-18 13:03:58 +0000234
Igor Sysoev5192b362005-07-08 14:34:20 +0000235 ngx_msec_t timeout;
236
Igor Sysoev78452232005-10-12 13:50:36 +0000237 ngx_str_t method;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000238
Igor Sysoev899b44e2005-05-12 14:58:06 +0000239 ngx_http_upstream_state_t *state;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000240
Igor Sysoeva2573672005-10-05 14:46:21 +0000241 ngx_str_t uri;
242
Igor Sysoev9ac946b2005-10-24 15:09:41 +0000243 ngx_http_cleanup_pt *cleanup;
244
Igor Sysoev58feb532007-07-12 11:19:05 +0000245 unsigned store:1;
Igor Sysoev899b44e2005-05-12 14:58:06 +0000246 unsigned cachable:1;
247 unsigned accel:1;
248
Igor Sysoev3338cfd2006-05-11 14:43:47 +0000249 unsigned buffering:1;
250
Igor Sysoev899b44e2005-05-12 14:58:06 +0000251 unsigned request_sent:1;
252 unsigned header_sent:1;
Igor Sysoev02025fd2005-01-18 13:03:58 +0000253};
254
255
Igor Sysoev27233612007-04-10 07:08:06 +0000256ngx_int_t ngx_http_upstream_header_variable(ngx_http_request_t *r,
257 ngx_http_variable_value_t *v, uintptr_t data);
258
Igor Sysoev02025fd2005-01-18 13:03:58 +0000259void ngx_http_upstream_init(ngx_http_request_t *r);
Igor Sysoev6f134cc2006-05-23 14:54:58 +0000260ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf,
Igor Sysoev3d2fd182006-12-04 16:46:13 +0000261 ngx_url_t *u, ngx_uint_t flags);
262
263
264#define ngx_http_conf_upstream_srv_conf(uscf, module) \
265 uscf->srv_conf[module.ctx_index]
Igor Sysoev02025fd2005-01-18 13:03:58 +0000266
267
Igor Sysoev899b44e2005-05-12 14:58:06 +0000268extern ngx_module_t ngx_http_upstream_module;
269
Igor Sysoev02025fd2005-01-18 13:03:58 +0000270
271#endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */