ngx_http_upstream.h revision e18a033b
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#ifndef _NGX_HTTP_UPSTREAM_H_INCLUDED_
9#define _NGX_HTTP_UPSTREAM_H_INCLUDED_
10
11
12#include <ngx_config.h>
13#include <ngx_core.h>
14#include <ngx_event.h>
15#include <ngx_event_connect.h>
16#include <ngx_event_pipe.h>
17#include <ngx_http.h>
18
19
20#define NGX_HTTP_UPSTREAM_FT_ERROR           0x00000002
21#define NGX_HTTP_UPSTREAM_FT_TIMEOUT         0x00000004
22#define NGX_HTTP_UPSTREAM_FT_INVALID_HEADER  0x00000008
23#define NGX_HTTP_UPSTREAM_FT_HTTP_500        0x00000010
24#define NGX_HTTP_UPSTREAM_FT_HTTP_502        0x00000020
25#define NGX_HTTP_UPSTREAM_FT_HTTP_503        0x00000040
26#define NGX_HTTP_UPSTREAM_FT_HTTP_504        0x00000080
27#define NGX_HTTP_UPSTREAM_FT_HTTP_403        0x00000100
28#define NGX_HTTP_UPSTREAM_FT_HTTP_404        0x00000200
29#define NGX_HTTP_UPSTREAM_FT_HTTP_429        0x00000400
30#define NGX_HTTP_UPSTREAM_FT_UPDATING        0x00000800
31#define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK       0x00001000
32#define NGX_HTTP_UPSTREAM_FT_MAX_WAITING     0x00002000
33#define NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT  0x00004000
34#define NGX_HTTP_UPSTREAM_FT_NOLIVE          0x40000000
35#define NGX_HTTP_UPSTREAM_FT_OFF             0x80000000
36
37#define NGX_HTTP_UPSTREAM_FT_STATUS          (NGX_HTTP_UPSTREAM_FT_HTTP_500  \
38                                             |NGX_HTTP_UPSTREAM_FT_HTTP_502  \
39                                             |NGX_HTTP_UPSTREAM_FT_HTTP_503  \
40                                             |NGX_HTTP_UPSTREAM_FT_HTTP_504  \
41                                             |NGX_HTTP_UPSTREAM_FT_HTTP_403  \
42                                             |NGX_HTTP_UPSTREAM_FT_HTTP_404  \
43                                             |NGX_HTTP_UPSTREAM_FT_HTTP_429)
44
45#define NGX_HTTP_UPSTREAM_INVALID_HEADER     40
46
47
48#define NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT    0x00000002
49#define NGX_HTTP_UPSTREAM_IGN_XA_EXPIRES     0x00000004
50#define NGX_HTTP_UPSTREAM_IGN_EXPIRES        0x00000008
51#define NGX_HTTP_UPSTREAM_IGN_CACHE_CONTROL  0x00000010
52#define NGX_HTTP_UPSTREAM_IGN_SET_COOKIE     0x00000020
53#define NGX_HTTP_UPSTREAM_IGN_XA_LIMIT_RATE  0x00000040
54#define NGX_HTTP_UPSTREAM_IGN_XA_BUFFERING   0x00000080
55#define NGX_HTTP_UPSTREAM_IGN_XA_CHARSET     0x00000100
56#define NGX_HTTP_UPSTREAM_IGN_VARY           0x00000200
57
58
59typedef struct {
60    ngx_uint_t                       status;
61    ngx_msec_t                       response_time;
62    ngx_msec_t                       connect_time;
63    ngx_msec_t                       header_time;
64    off_t                            response_length;
65    off_t                            bytes_received;
66
67    ngx_str_t                       *peer;
68} ngx_http_upstream_state_t;
69
70
71typedef struct {
72    ngx_hash_t                       headers_in_hash;
73    ngx_array_t                      upstreams;
74                                             /* ngx_http_upstream_srv_conf_t */
75} ngx_http_upstream_main_conf_t;
76
77typedef struct ngx_http_upstream_srv_conf_s  ngx_http_upstream_srv_conf_t;
78
79typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t *cf,
80    ngx_http_upstream_srv_conf_t *us);
81typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(ngx_http_request_t *r,
82    ngx_http_upstream_srv_conf_t *us);
83
84
85typedef struct {
86    ngx_http_upstream_init_pt        init_upstream;
87    ngx_http_upstream_init_peer_pt   init;
88    void                            *data;
89} ngx_http_upstream_peer_t;
90
91
92typedef struct {
93    ngx_str_t                        name;
94    ngx_addr_t                      *addrs;
95    ngx_uint_t                       naddrs;
96    ngx_uint_t                       weight;
97    ngx_uint_t                       max_conns;
98    ngx_uint_t                       max_fails;
99    time_t                           fail_timeout;
100    ngx_msec_t                       slow_start;
101
102    unsigned                         down:1;
103    unsigned                         backup:1;
104
105    NGX_COMPAT_BEGIN(6)
106    NGX_COMPAT_END
107} ngx_http_upstream_server_t;
108
109
110#define NGX_HTTP_UPSTREAM_CREATE        0x0001
111#define NGX_HTTP_UPSTREAM_WEIGHT        0x0002
112#define NGX_HTTP_UPSTREAM_MAX_FAILS     0x0004
113#define NGX_HTTP_UPSTREAM_FAIL_TIMEOUT  0x0008
114#define NGX_HTTP_UPSTREAM_DOWN          0x0010
115#define NGX_HTTP_UPSTREAM_BACKUP        0x0020
116#define NGX_HTTP_UPSTREAM_MAX_CONNS     0x0100
117
118
119struct ngx_http_upstream_srv_conf_s {
120    ngx_http_upstream_peer_t         peer;
121    void                           **srv_conf;
122
123    ngx_array_t                     *servers;  /* ngx_http_upstream_server_t */
124
125    ngx_uint_t                       flags;
126    ngx_str_t                        host;
127    u_char                          *file_name;
128    ngx_uint_t                       line;
129    in_port_t                        port;
130    ngx_uint_t                       no_port;  /* unsigned no_port:1 */
131
132#if (NGX_HTTP_UPSTREAM_ZONE)
133    ngx_shm_zone_t                  *shm_zone;
134#endif
135};
136
137
138typedef struct {
139    ngx_addr_t                      *addr;
140    ngx_http_complex_value_t        *value;
141#if (NGX_HAVE_TRANSPARENT_PROXY)
142    ngx_uint_t                       transparent; /* unsigned  transparent:1; */
143#endif
144} ngx_http_upstream_local_t;
145
146
147typedef struct {
148    ngx_http_upstream_srv_conf_t    *upstream;
149
150    ngx_msec_t                       connect_timeout;
151    ngx_msec_t                       send_timeout;
152    ngx_msec_t                       read_timeout;
153    ngx_msec_t                       next_upstream_timeout;
154
155    size_t                           send_lowat;
156    size_t                           buffer_size;
157    size_t                           limit_rate;
158
159    size_t                           busy_buffers_size;
160    size_t                           max_temp_file_size;
161    size_t                           temp_file_write_size;
162
163    size_t                           busy_buffers_size_conf;
164    size_t                           max_temp_file_size_conf;
165    size_t                           temp_file_write_size_conf;
166
167    ngx_bufs_t                       bufs;
168
169    ngx_uint_t                       ignore_headers;
170    ngx_uint_t                       next_upstream;
171    ngx_uint_t                       store_access;
172    ngx_uint_t                       next_upstream_tries;
173    ngx_flag_t                       buffering;
174    ngx_flag_t                       request_buffering;
175    ngx_flag_t                       pass_request_headers;
176    ngx_flag_t                       pass_request_body;
177
178    ngx_flag_t                       ignore_client_abort;
179    ngx_flag_t                       intercept_errors;
180    ngx_flag_t                       cyclic_temp_file;
181    ngx_flag_t                       force_ranges;
182
183    ngx_path_t                      *temp_path;
184
185    ngx_hash_t                       hide_headers_hash;
186    ngx_array_t                     *hide_headers;
187    ngx_array_t                     *pass_headers;
188
189    ngx_http_upstream_local_t       *local;
190
191#if (NGX_HTTP_CACHE)
192    ngx_shm_zone_t                  *cache_zone;
193    ngx_http_complex_value_t        *cache_value;
194
195    ngx_uint_t                       cache_min_uses;
196    ngx_uint_t                       cache_use_stale;
197    ngx_uint_t                       cache_methods;
198
199    off_t                            cache_max_range_offset;
200
201    ngx_flag_t                       cache_lock;
202    ngx_msec_t                       cache_lock_timeout;
203    ngx_msec_t                       cache_lock_age;
204
205    ngx_flag_t                       cache_revalidate;
206    ngx_flag_t                       cache_convert_head;
207    ngx_flag_t                       cache_background_update;
208
209    ngx_array_t                     *cache_valid;
210    ngx_array_t                     *cache_bypass;
211    ngx_array_t                     *cache_purge;
212    ngx_array_t                     *no_cache;
213#endif
214
215    ngx_array_t                     *store_lengths;
216    ngx_array_t                     *store_values;
217
218#if (NGX_HTTP_CACHE)
219    signed                           cache:2;
220#endif
221    signed                           store:2;
222    unsigned                         intercept_404:1;
223    unsigned                         change_buffering:1;
224
225#if (NGX_HTTP_SSL || NGX_COMPAT)
226    ngx_ssl_t                       *ssl;
227    ngx_flag_t                       ssl_session_reuse;
228
229    ngx_http_complex_value_t        *ssl_name;
230    ngx_flag_t                       ssl_server_name;
231    ngx_flag_t                       ssl_verify;
232#endif
233
234    ngx_str_t                        module;
235
236    NGX_COMPAT_BEGIN(2)
237    NGX_COMPAT_END
238} ngx_http_upstream_conf_t;
239
240
241typedef struct {
242    ngx_str_t                        name;
243    ngx_http_header_handler_pt       handler;
244    ngx_uint_t                       offset;
245    ngx_http_header_handler_pt       copy_handler;
246    ngx_uint_t                       conf;
247    ngx_uint_t                       redirect;  /* unsigned   redirect:1; */
248} ngx_http_upstream_header_t;
249
250
251typedef struct {
252    ngx_list_t                       headers;
253
254    ngx_uint_t                       status_n;
255    ngx_str_t                        status_line;
256
257    ngx_table_elt_t                 *status;
258    ngx_table_elt_t                 *date;
259    ngx_table_elt_t                 *server;
260    ngx_table_elt_t                 *connection;
261
262    ngx_table_elt_t                 *expires;
263    ngx_table_elt_t                 *etag;
264    ngx_table_elt_t                 *x_accel_expires;
265    ngx_table_elt_t                 *x_accel_redirect;
266    ngx_table_elt_t                 *x_accel_limit_rate;
267
268    ngx_table_elt_t                 *content_type;
269    ngx_table_elt_t                 *content_length;
270
271    ngx_table_elt_t                 *last_modified;
272    ngx_table_elt_t                 *location;
273    ngx_table_elt_t                 *accept_ranges;
274    ngx_table_elt_t                 *www_authenticate;
275    ngx_table_elt_t                 *transfer_encoding;
276    ngx_table_elt_t                 *vary;
277
278#if (NGX_HTTP_GZIP)
279    ngx_table_elt_t                 *content_encoding;
280#endif
281
282    ngx_array_t                      cache_control;
283    ngx_array_t                      cookies;
284
285    off_t                            content_length_n;
286    time_t                           last_modified_time;
287
288    unsigned                         connection_close:1;
289    unsigned                         chunked:1;
290} ngx_http_upstream_headers_in_t;
291
292
293typedef struct {
294    ngx_str_t                        host;
295    in_port_t                        port;
296    ngx_uint_t                       no_port; /* unsigned no_port:1 */
297
298    ngx_uint_t                       naddrs;
299    ngx_resolver_addr_t             *addrs;
300
301    struct sockaddr                 *sockaddr;
302    socklen_t                        socklen;
303    ngx_str_t                        name;
304
305    ngx_resolver_ctx_t              *ctx;
306} ngx_http_upstream_resolved_t;
307
308
309typedef void (*ngx_http_upstream_handler_pt)(ngx_http_request_t *r,
310    ngx_http_upstream_t *u);
311
312
313struct ngx_http_upstream_s {
314    ngx_http_upstream_handler_pt     read_event_handler;
315    ngx_http_upstream_handler_pt     write_event_handler;
316
317    ngx_peer_connection_t            peer;
318
319    ngx_event_pipe_t                *pipe;
320
321    ngx_chain_t                     *request_bufs;
322
323    ngx_output_chain_ctx_t           output;
324    ngx_chain_writer_ctx_t           writer;
325
326    ngx_http_upstream_conf_t        *conf;
327    ngx_http_upstream_srv_conf_t    *upstream;
328#if (NGX_HTTP_CACHE)
329    ngx_array_t                     *caches;
330#endif
331
332    ngx_http_upstream_headers_in_t   headers_in;
333
334    ngx_http_upstream_resolved_t    *resolved;
335
336    ngx_buf_t                        from_client;
337
338    ngx_buf_t                        buffer;
339    off_t                            length;
340
341    ngx_chain_t                     *out_bufs;
342    ngx_chain_t                     *busy_bufs;
343    ngx_chain_t                     *free_bufs;
344
345    ngx_int_t                      (*input_filter_init)(void *data);
346    ngx_int_t                      (*input_filter)(void *data, ssize_t bytes);
347    void                            *input_filter_ctx;
348
349#if (NGX_HTTP_CACHE)
350    ngx_int_t                      (*create_key)(ngx_http_request_t *r);
351#endif
352    ngx_int_t                      (*create_request)(ngx_http_request_t *r);
353    ngx_int_t                      (*reinit_request)(ngx_http_request_t *r);
354    ngx_int_t                      (*process_header)(ngx_http_request_t *r);
355    void                           (*abort_request)(ngx_http_request_t *r);
356    void                           (*finalize_request)(ngx_http_request_t *r,
357                                         ngx_int_t rc);
358    ngx_int_t                      (*rewrite_redirect)(ngx_http_request_t *r,
359                                         ngx_table_elt_t *h, size_t prefix);
360    ngx_int_t                      (*rewrite_cookie)(ngx_http_request_t *r,
361                                         ngx_table_elt_t *h);
362
363    ngx_msec_t                       timeout;
364
365    ngx_http_upstream_state_t       *state;
366
367    ngx_str_t                        method;
368    ngx_str_t                        schema;
369    ngx_str_t                        uri;
370
371#if (NGX_HTTP_SSL || NGX_COMPAT)
372    ngx_str_t                        ssl_name;
373#endif
374
375    ngx_http_cleanup_pt             *cleanup;
376
377    unsigned                         store:1;
378    unsigned                         cacheable:1;
379    unsigned                         accel:1;
380    unsigned                         ssl:1;
381#if (NGX_HTTP_CACHE)
382    unsigned                         cache_status:3;
383#endif
384
385    unsigned                         buffering:1;
386    unsigned                         keepalive:1;
387    unsigned                         upgrade:1;
388
389    unsigned                         request_sent:1;
390    unsigned                         request_body_sent:1;
391    unsigned                         header_sent:1;
392};
393
394
395typedef struct {
396    ngx_uint_t                      status;
397    ngx_uint_t                      mask;
398} ngx_http_upstream_next_t;
399
400
401typedef struct {
402    ngx_str_t   key;
403    ngx_str_t   value;
404    ngx_uint_t  skip_empty;
405} ngx_http_upstream_param_t;
406
407
408ngx_int_t ngx_http_upstream_create(ngx_http_request_t *r);
409void ngx_http_upstream_init(ngx_http_request_t *r);
410ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf,
411    ngx_url_t *u, ngx_uint_t flags);
412char *ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
413    void *conf);
414char *ngx_http_upstream_param_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
415    void *conf);
416ngx_int_t ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
417    ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
418    ngx_str_t *default_hide_headers, ngx_hash_init_t *hash);
419
420
421#define ngx_http_conf_upstream_srv_conf(uscf, module)                         \
422    uscf->srv_conf[module.ctx_index]
423
424
425extern ngx_module_t        ngx_http_upstream_module;
426extern ngx_conf_bitmask_t  ngx_http_upstream_cache_method_mask[];
427extern ngx_conf_bitmask_t  ngx_http_upstream_ignore_headers_masks[];
428
429
430#endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */
431