src/http/ngx_http_upstream.h - nginx-1.7.10

Data types defined

Macros defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #ifndef _NGX_HTTP_UPSTREAM_H_INCLUDED_
  6. #define _NGX_HTTP_UPSTREAM_H_INCLUDED_


  7. #include <ngx_config.h>
  8. #include <ngx_core.h>
  9. #include <ngx_event.h>
  10. #include <ngx_event_connect.h>
  11. #include <ngx_event_pipe.h>
  12. #include <ngx_http.h>


  13. #define NGX_HTTP_UPSTREAM_FT_ERROR           0x00000002
  14. #define NGX_HTTP_UPSTREAM_FT_TIMEOUT         0x00000004
  15. #define NGX_HTTP_UPSTREAM_FT_INVALID_HEADER  0x00000008
  16. #define NGX_HTTP_UPSTREAM_FT_HTTP_500        0x00000010
  17. #define NGX_HTTP_UPSTREAM_FT_HTTP_502        0x00000020
  18. #define NGX_HTTP_UPSTREAM_FT_HTTP_503        0x00000040
  19. #define NGX_HTTP_UPSTREAM_FT_HTTP_504        0x00000080
  20. #define NGX_HTTP_UPSTREAM_FT_HTTP_403        0x00000100
  21. #define NGX_HTTP_UPSTREAM_FT_HTTP_404        0x00000200
  22. #define NGX_HTTP_UPSTREAM_FT_UPDATING        0x00000400
  23. #define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK       0x00000800
  24. #define NGX_HTTP_UPSTREAM_FT_MAX_WAITING     0x00001000
  25. #define NGX_HTTP_UPSTREAM_FT_NOLIVE          0x40000000
  26. #define NGX_HTTP_UPSTREAM_FT_OFF             0x80000000

  27. #define NGX_HTTP_UPSTREAM_FT_STATUS          (NGX_HTTP_UPSTREAM_FT_HTTP_500  \
  28.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_502  \
  29.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_503  \
  30.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_504  \
  31.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_403  \
  32.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_404)

  33. #define NGX_HTTP_UPSTREAM_INVALID_HEADER     40


  34. #define NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT    0x00000002
  35. #define NGX_HTTP_UPSTREAM_IGN_XA_EXPIRES     0x00000004
  36. #define NGX_HTTP_UPSTREAM_IGN_EXPIRES        0x00000008
  37. #define NGX_HTTP_UPSTREAM_IGN_CACHE_CONTROL  0x00000010
  38. #define NGX_HTTP_UPSTREAM_IGN_SET_COOKIE     0x00000020
  39. #define NGX_HTTP_UPSTREAM_IGN_XA_LIMIT_RATE  0x00000040
  40. #define NGX_HTTP_UPSTREAM_IGN_XA_BUFFERING   0x00000080
  41. #define NGX_HTTP_UPSTREAM_IGN_XA_CHARSET     0x00000100
  42. #define NGX_HTTP_UPSTREAM_IGN_VARY           0x00000200


  43. typedef struct {
  44.     ngx_msec_t                       bl_time;
  45.     ngx_uint_t                       bl_state;

  46.     ngx_uint_t                       status;
  47.     time_t                           response_sec;
  48.     ngx_uint_t                       response_msec;
  49.     time_t                           header_sec;
  50.     ngx_uint_t                       header_msec;
  51.     off_t                            response_length;

  52.     ngx_str_t                       *peer;
  53. } ngx_http_upstream_state_t;


  54. typedef struct {
  55.     ngx_hash_t                       headers_in_hash;
  56.     ngx_array_t                      upstreams;
  57.                                              /* ngx_http_upstream_srv_conf_t */
  58. } ngx_http_upstream_main_conf_t;

  59. typedef struct ngx_http_upstream_srv_conf_s  ngx_http_upstream_srv_conf_t;

  60. typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t *cf,
  61.     ngx_http_upstream_srv_conf_t *us);
  62. typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(ngx_http_request_t *r,
  63.     ngx_http_upstream_srv_conf_t *us);


  64. typedef struct {
  65.     ngx_http_upstream_init_pt        init_upstream;
  66.     ngx_http_upstream_init_peer_pt   init;
  67.     void                            *data;
  68. } ngx_http_upstream_peer_t;


  69. typedef struct {
  70.     ngx_str_t                        name;
  71.     ngx_addr_t                      *addrs;
  72.     ngx_uint_t                       naddrs;
  73.     ngx_uint_t                       weight;
  74.     ngx_uint_t                       max_fails;
  75.     time_t                           fail_timeout;

  76.     unsigned                         down:1;
  77.     unsigned                         backup:1;
  78. } ngx_http_upstream_server_t;


  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. struct ngx_http_upstream_srv_conf_s {
  86.     ngx_http_upstream_peer_t         peer;
  87.     void                           **srv_conf;

  88.     ngx_array_t                     *servers;  /* ngx_http_upstream_server_t */

  89.     ngx_uint_t                       flags;
  90.     ngx_str_t                        host;
  91.     u_char                          *file_name;
  92.     ngx_uint_t                       line;
  93.     in_port_t                        port;
  94.     in_port_t                        default_port;
  95.     ngx_uint_t                       no_port;  /* unsigned no_port:1 */
  96. };


  97. typedef struct {
  98.     ngx_addr_t                      *addr;
  99.     ngx_http_complex_value_t        *value;
  100. } ngx_http_upstream_local_t;


  101. typedef struct {
  102.     ngx_http_upstream_srv_conf_t    *upstream;

  103.     ngx_msec_t                       connect_timeout;
  104.     ngx_msec_t                       send_timeout;
  105.     ngx_msec_t                       read_timeout;
  106.     ngx_msec_t                       timeout;
  107.     ngx_msec_t                       next_upstream_timeout;

  108.     size_t                           send_lowat;
  109.     size_t                           buffer_size;
  110.     size_t                           limit_rate;

  111.     size_t                           busy_buffers_size;
  112.     size_t                           max_temp_file_size;
  113.     size_t                           temp_file_write_size;

  114.     size_t                           busy_buffers_size_conf;
  115.     size_t                           max_temp_file_size_conf;
  116.     size_t                           temp_file_write_size_conf;

  117.     ngx_bufs_t                       bufs;

  118.     ngx_uint_t                       ignore_headers;
  119.     ngx_uint_t                       next_upstream;
  120.     ngx_uint_t                       store_access;
  121.     ngx_uint_t                       next_upstream_tries;
  122.     ngx_flag_t                       buffering;
  123.     ngx_flag_t                       pass_request_headers;
  124.     ngx_flag_t                       pass_request_body;

  125.     ngx_flag_t                       ignore_client_abort;
  126.     ngx_flag_t                       intercept_errors;
  127.     ngx_flag_t                       cyclic_temp_file;
  128.     ngx_flag_t                       force_ranges;

  129.     ngx_path_t                      *temp_path;

  130.     ngx_hash_t                       hide_headers_hash;
  131.     ngx_array_t                     *hide_headers;
  132.     ngx_array_t                     *pass_headers;

  133.     ngx_http_upstream_local_t       *local;

  134. #if (NGX_HTTP_CACHE)
  135.     ngx_shm_zone_t                  *cache_zone;
  136.     ngx_http_complex_value_t        *cache_value;

  137.     ngx_uint_t                       cache_min_uses;
  138.     ngx_uint_t                       cache_use_stale;
  139.     ngx_uint_t                       cache_methods;

  140.     ngx_flag_t                       cache_lock;
  141.     ngx_msec_t                       cache_lock_timeout;
  142.     ngx_msec_t                       cache_lock_age;

  143.     ngx_flag_t                       cache_revalidate;

  144.     ngx_array_t                     *cache_valid;
  145.     ngx_array_t                     *cache_bypass;
  146.     ngx_array_t                     *no_cache;
  147. #endif

  148.     ngx_array_t                     *store_lengths;
  149.     ngx_array_t                     *store_values;

  150. #if (NGX_HTTP_CACHE)
  151.     signed                           cache:2;
  152. #endif
  153.     signed                           store:2;
  154.     unsigned                         intercept_404:1;
  155.     unsigned                         change_buffering:1;

  156. #if (NGX_HTTP_SSL)
  157.     ngx_ssl_t                       *ssl;
  158.     ngx_flag_t                       ssl_session_reuse;

  159.     ngx_http_complex_value_t        *ssl_name;
  160.     ngx_flag_t                       ssl_server_name;
  161.     ngx_flag_t                       ssl_verify;
  162. #endif

  163.     ngx_str_t                        module;
  164. } ngx_http_upstream_conf_t;


  165. typedef struct {
  166.     ngx_str_t                        name;
  167.     ngx_http_header_handler_pt       handler;
  168.     ngx_uint_t                       offset;
  169.     ngx_http_header_handler_pt       copy_handler;
  170.     ngx_uint_t                       conf;
  171.     ngx_uint_t                       redirect;  /* unsigned   redirect:1; */
  172. } ngx_http_upstream_header_t;


  173. typedef struct {
  174.     ngx_list_t                       headers;

  175.     ngx_uint_t                       status_n;
  176.     ngx_str_t                        status_line;

  177.     ngx_table_elt_t                 *status;
  178.     ngx_table_elt_t                 *date;
  179.     ngx_table_elt_t                 *server;
  180.     ngx_table_elt_t                 *connection;

  181.     ngx_table_elt_t                 *expires;
  182.     ngx_table_elt_t                 *etag;
  183.     ngx_table_elt_t                 *x_accel_expires;
  184.     ngx_table_elt_t                 *x_accel_redirect;
  185.     ngx_table_elt_t                 *x_accel_limit_rate;

  186.     ngx_table_elt_t                 *content_type;
  187.     ngx_table_elt_t                 *content_length;

  188.     ngx_table_elt_t                 *last_modified;
  189.     ngx_table_elt_t                 *location;
  190.     ngx_table_elt_t                 *accept_ranges;
  191.     ngx_table_elt_t                 *www_authenticate;
  192.     ngx_table_elt_t                 *transfer_encoding;
  193.     ngx_table_elt_t                 *vary;

  194. #if (NGX_HTTP_GZIP)
  195.     ngx_table_elt_t                 *content_encoding;
  196. #endif

  197.     ngx_array_t                      cache_control;
  198.     ngx_array_t                      cookies;

  199.     off_t                            content_length_n;
  200.     time_t                           last_modified_time;

  201.     unsigned                         connection_close:1;
  202.     unsigned                         chunked:1;
  203. } ngx_http_upstream_headers_in_t;


  204. typedef struct {
  205.     ngx_str_t                        host;
  206.     in_port_t                        port;
  207.     ngx_uint_t                       no_port; /* unsigned no_port:1 */

  208.     ngx_uint_t                       naddrs;
  209.     ngx_addr_t                      *addrs;

  210.     struct sockaddr                 *sockaddr;
  211.     socklen_t                        socklen;

  212.     ngx_resolver_ctx_t              *ctx;
  213. } ngx_http_upstream_resolved_t;


  214. typedef void (*ngx_http_upstream_handler_pt)(ngx_http_request_t *r,
  215.     ngx_http_upstream_t *u);


  216. struct ngx_http_upstream_s {
  217.     ngx_http_upstream_handler_pt     read_event_handler;
  218.     ngx_http_upstream_handler_pt     write_event_handler;

  219.     ngx_peer_connection_t            peer;

  220.     ngx_event_pipe_t                *pipe;

  221.     ngx_chain_t                     *request_bufs;

  222.     ngx_output_chain_ctx_t           output;
  223.     ngx_chain_writer_ctx_t           writer;

  224.     ngx_http_upstream_conf_t        *conf;
  225. #if (NGX_HTTP_CACHE)
  226.     ngx_array_t                     *caches;
  227. #endif

  228.     ngx_http_upstream_headers_in_t   headers_in;

  229.     ngx_http_upstream_resolved_t    *resolved;

  230.     ngx_buf_t                        from_client;

  231.     ngx_buf_t                        buffer;
  232.     off_t                            length;

  233.     ngx_chain_t                     *out_bufs;
  234.     ngx_chain_t                     *busy_bufs;
  235.     ngx_chain_t                     *free_bufs;

  236.     ngx_int_t                      (*input_filter_init)(void *data);
  237.     ngx_int_t                      (*input_filter)(void *data, ssize_t bytes);
  238.     void                            *input_filter_ctx;

  239. #if (NGX_HTTP_CACHE)
  240.     ngx_int_t                      (*create_key)(ngx_http_request_t *r);
  241. #endif
  242.     ngx_int_t                      (*create_request)(ngx_http_request_t *r);
  243.     ngx_int_t                      (*reinit_request)(ngx_http_request_t *r);
  244.     ngx_int_t                      (*process_header)(ngx_http_request_t *r);
  245.     void                           (*abort_request)(ngx_http_request_t *r);
  246.     void                           (*finalize_request)(ngx_http_request_t *r,
  247.                                          ngx_int_t rc);
  248.     ngx_int_t                      (*rewrite_redirect)(ngx_http_request_t *r,
  249.                                          ngx_table_elt_t *h, size_t prefix);
  250.     ngx_int_t                      (*rewrite_cookie)(ngx_http_request_t *r,
  251.                                          ngx_table_elt_t *h);

  252.     ngx_msec_t                       timeout;

  253.     ngx_http_upstream_state_t       *state;

  254.     ngx_str_t                        method;
  255.     ngx_str_t                        schema;
  256.     ngx_str_t                        uri;

  257. #if (NGX_HTTP_SSL)
  258.     ngx_str_t                        ssl_name;
  259. #endif

  260.     ngx_http_cleanup_pt             *cleanup;

  261.     unsigned                         store:1;
  262.     unsigned                         cacheable:1;
  263.     unsigned                         accel:1;
  264.     unsigned                         ssl:1;
  265. #if (NGX_HTTP_CACHE)
  266.     unsigned                         cache_status:3;
  267. #endif

  268.     unsigned                         buffering:1;
  269.     unsigned                         keepalive:1;
  270.     unsigned                         upgrade:1;

  271.     unsigned                         request_sent:1;
  272.     unsigned                         header_sent:1;
  273. };


  274. typedef struct {
  275.     ngx_uint_t                      status;
  276.     ngx_uint_t                      mask;
  277. } ngx_http_upstream_next_t;


  278. typedef struct {
  279.     ngx_str_t   key;
  280.     ngx_str_t   value;
  281.     ngx_uint_t  skip_empty;
  282. } ngx_http_upstream_param_t;


  283. ngx_int_t ngx_http_upstream_cookie_variable(ngx_http_request_t *r,
  284.     ngx_http_variable_value_t *v, uintptr_t data);
  285. ngx_int_t ngx_http_upstream_header_variable(ngx_http_request_t *r,
  286.     ngx_http_variable_value_t *v, uintptr_t data);

  287. ngx_int_t ngx_http_upstream_create(ngx_http_request_t *r);
  288. void ngx_http_upstream_init(ngx_http_request_t *r);
  289. ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf,
  290.     ngx_url_t *u, ngx_uint_t flags);
  291. char *ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
  292.     void *conf);
  293. char *ngx_http_upstream_param_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
  294.     void *conf);
  295. ngx_int_t ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
  296.     ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
  297.     ngx_str_t *default_hide_headers, ngx_hash_init_t *hash);


  298. #define ngx_http_conf_upstream_srv_conf(uscf, module)                         \
  299.     uscf->srv_conf[module.ctx_index]


  300. extern ngx_module_t        ngx_http_upstream_module;
  301. extern ngx_conf_bitmask_t  ngx_http_upstream_cache_method_mask[];
  302. extern ngx_conf_bitmask_t  ngx_http_upstream_ignore_headers_masks[];


  303. #endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */