src/http/ngx_http_upstream.h - nginx

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_HTTP_429        0x00000400
  23. #define NGX_HTTP_UPSTREAM_FT_UPDATING        0x00000800
  24. #define NGX_HTTP_UPSTREAM_FT_BUSY_LOCK       0x00001000
  25. #define NGX_HTTP_UPSTREAM_FT_MAX_WAITING     0x00002000
  26. #define NGX_HTTP_UPSTREAM_FT_NON_IDEMPOTENT  0x00004000
  27. #define NGX_HTTP_UPSTREAM_FT_NOLIVE          0x40000000
  28. #define NGX_HTTP_UPSTREAM_FT_OFF             0x80000000

  29. #define NGX_HTTP_UPSTREAM_FT_STATUS          (NGX_HTTP_UPSTREAM_FT_HTTP_500  \
  30.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_502  \
  31.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_503  \
  32.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_504  \
  33.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_403  \
  34.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_404  \
  35.                                              |NGX_HTTP_UPSTREAM_FT_HTTP_429)

  36. #define NGX_HTTP_UPSTREAM_INVALID_HEADER     40
  37. #define NGX_HTTP_UPSTREAM_EARLY_HINTS        41


  38. #define NGX_HTTP_UPSTREAM_IGN_XA_REDIRECT    0x00000002
  39. #define NGX_HTTP_UPSTREAM_IGN_XA_EXPIRES     0x00000004
  40. #define NGX_HTTP_UPSTREAM_IGN_EXPIRES        0x00000008
  41. #define NGX_HTTP_UPSTREAM_IGN_CACHE_CONTROL  0x00000010
  42. #define NGX_HTTP_UPSTREAM_IGN_SET_COOKIE     0x00000020
  43. #define NGX_HTTP_UPSTREAM_IGN_XA_LIMIT_RATE  0x00000040
  44. #define NGX_HTTP_UPSTREAM_IGN_XA_BUFFERING   0x00000080
  45. #define NGX_HTTP_UPSTREAM_IGN_XA_CHARSET     0x00000100
  46. #define NGX_HTTP_UPSTREAM_IGN_VARY           0x00000200


  47. #define NGX_HTTP_UPSTREAM_NOTIFY_HEADER      0x1


  48. typedef struct {
  49.     ngx_uint_t                       status;
  50.     ngx_msec_t                       response_time;
  51.     ngx_msec_t                       connect_time;
  52.     ngx_msec_t                       header_time;
  53.     ngx_msec_t                       queue_time;
  54.     off_t                            response_length;
  55.     off_t                            bytes_received;
  56.     off_t                            bytes_sent;

  57.     ngx_str_t                       *peer;
  58. } ngx_http_upstream_state_t;


  59. typedef struct {
  60.     ngx_hash_t                       headers_in_hash;
  61.     ngx_array_t                      upstreams;
  62.                                              /* ngx_http_upstream_srv_conf_t */
  63. } ngx_http_upstream_main_conf_t;

  64. typedef struct ngx_http_upstream_srv_conf_s  ngx_http_upstream_srv_conf_t;

  65. typedef ngx_int_t (*ngx_http_upstream_init_pt)(ngx_conf_t *cf,
  66.     ngx_http_upstream_srv_conf_t *us);
  67. typedef ngx_int_t (*ngx_http_upstream_init_peer_pt)(ngx_http_request_t *r,
  68.     ngx_http_upstream_srv_conf_t *us);


  69. typedef struct {
  70.     ngx_http_upstream_init_pt        init_upstream;
  71.     ngx_http_upstream_init_peer_pt   init;
  72.     void                            *data;
  73. } ngx_http_upstream_peer_t;


  74. typedef struct {
  75.     ngx_str_t                        name;
  76.     ngx_addr_t                      *addrs;
  77.     ngx_uint_t                       naddrs;
  78.     ngx_uint_t                       weight;
  79.     ngx_uint_t                       max_conns;
  80.     ngx_uint_t                       max_fails;
  81.     time_t                           fail_timeout;
  82.     ngx_msec_t                       slow_start;
  83.     ngx_uint_t                       down;

  84.     unsigned                         backup:1;

  85. #if (NGX_HTTP_UPSTREAM_ZONE)
  86.     ngx_str_t                        host;
  87.     ngx_str_t                        service;
  88. #endif

  89. #if (NGX_HTTP_UPSTREAM_SID || NGX_COMPAT)
  90.     ngx_str_t                        sid;
  91. #endif
  92. } ngx_http_upstream_server_t;


  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. #define NGX_HTTP_UPSTREAM_MODIFY        0x0040
  100. #define NGX_HTTP_UPSTREAM_MAX_CONNS     0x0100


  101. struct ngx_http_upstream_srv_conf_s {
  102.     ngx_http_upstream_peer_t         peer;
  103.     void                           **srv_conf;

  104.     ngx_array_t                     *servers;  /* ngx_http_upstream_server_t */

  105.     ngx_uint_t                       flags;
  106.     ngx_str_t                        host;
  107.     u_char                          *file_name;
  108.     ngx_uint_t                       line;
  109.     in_port_t                        port;
  110.     ngx_uint_t                       no_port;  /* unsigned no_port:1 */

  111. #if (NGX_HTTP_UPSTREAM_ZONE)
  112.     ngx_shm_zone_t                  *shm_zone;
  113.     ngx_resolver_t                  *resolver;
  114.     ngx_msec_t                       resolver_timeout;
  115. #endif
  116. };


  117. typedef struct {
  118.     ngx_addr_t                      *addr;
  119.     ngx_http_complex_value_t        *value;
  120. #if (NGX_HAVE_TRANSPARENT_PROXY)
  121.     ngx_uint_t                       transparent; /* unsigned  transparent:1; */
  122. #endif
  123. } ngx_http_upstream_local_t;


  124. typedef struct {
  125.     ngx_http_upstream_srv_conf_t    *upstream;

  126.     ngx_msec_t                       connect_timeout;
  127.     ngx_msec_t                       send_timeout;
  128.     ngx_msec_t                       read_timeout;
  129.     ngx_msec_t                       next_upstream_timeout;

  130.     size_t                           send_lowat;
  131.     size_t                           buffer_size;
  132.     ngx_http_complex_value_t        *limit_rate;

  133.     size_t                           busy_buffers_size;
  134.     size_t                           max_temp_file_size;
  135.     size_t                           temp_file_write_size;

  136.     size_t                           busy_buffers_size_conf;
  137.     size_t                           max_temp_file_size_conf;
  138.     size_t                           temp_file_write_size_conf;

  139.     ngx_bufs_t                       bufs;

  140.     ngx_uint_t                       ignore_headers;
  141.     ngx_uint_t                       next_upstream;
  142.     ngx_uint_t                       store_access;
  143.     ngx_uint_t                       next_upstream_tries;
  144.     ngx_flag_t                       buffering;
  145.     ngx_flag_t                       request_buffering;
  146.     ngx_flag_t                       pass_request_headers;
  147.     ngx_flag_t                       pass_request_body;
  148.     ngx_flag_t                       pass_trailers;
  149.     ngx_flag_t                       pass_early_hints;

  150.     ngx_flag_t                       ignore_client_abort;
  151.     ngx_flag_t                       intercept_errors;
  152.     ngx_flag_t                       cyclic_temp_file;
  153.     ngx_flag_t                       force_ranges;

  154.     ngx_path_t                      *temp_path;

  155.     ngx_hash_t                       hide_headers_hash;
  156.     ngx_array_t                     *hide_headers;
  157.     ngx_array_t                     *pass_headers;

  158.     ngx_http_upstream_local_t       *local;
  159.     ngx_flag_t                       socket_keepalive;

  160. #if (NGX_HTTP_CACHE)
  161.     ngx_shm_zone_t                  *cache_zone;
  162.     ngx_http_complex_value_t        *cache_value;

  163.     ngx_uint_t                       cache_min_uses;
  164.     ngx_uint_t                       cache_use_stale;
  165.     ngx_uint_t                       cache_methods;

  166.     off_t                            cache_max_range_offset;

  167.     ngx_flag_t                       cache_lock;
  168.     ngx_msec_t                       cache_lock_timeout;
  169.     ngx_msec_t                       cache_lock_age;

  170.     ngx_flag_t                       cache_revalidate;
  171.     ngx_flag_t                       cache_convert_head;
  172.     ngx_flag_t                       cache_background_update;

  173.     ngx_array_t                     *cache_valid;
  174.     ngx_array_t                     *cache_bypass;
  175.     ngx_array_t                     *cache_purge;
  176.     ngx_array_t                     *no_cache;
  177. #endif

  178.     ngx_array_t                     *store_lengths;
  179.     ngx_array_t                     *store_values;

  180. #if (NGX_HTTP_CACHE)
  181.     signed                           cache:2;
  182. #endif
  183.     signed                           store:2;
  184.     unsigned                         intercept_404:1;
  185.     unsigned                         change_buffering:1;
  186.     unsigned                         preserve_output:1;
  187.     unsigned                         ignore_input:1;

  188. #if (NGX_HTTP_SSL || NGX_COMPAT)
  189.     ngx_ssl_t                       *ssl;
  190.     ngx_flag_t                       ssl_session_reuse;

  191.     ngx_http_complex_value_t        *ssl_name;
  192.     ngx_flag_t                       ssl_server_name;
  193.     ngx_flag_t                       ssl_verify;

  194.     ngx_http_complex_value_t        *ssl_certificate;
  195.     ngx_http_complex_value_t        *ssl_certificate_key;
  196.     ngx_ssl_cache_t                 *ssl_certificate_cache;
  197.     ngx_array_t                     *ssl_passwords;
  198. #endif

  199.     ngx_str_t                        module;

  200.     NGX_COMPAT_BEGIN(5)
  201.     NGX_COMPAT_END
  202. } ngx_http_upstream_conf_t;


  203. typedef struct {
  204.     ngx_str_t                        name;
  205.     ngx_http_header_handler_pt       handler;
  206.     ngx_uint_t                       offset;
  207.     ngx_http_header_handler_pt       copy_handler;
  208.     ngx_uint_t                       conf;
  209.     ngx_uint_t                       redirect;  /* unsigned   redirect:1; */
  210. } ngx_http_upstream_header_t;


  211. typedef struct {
  212.     ngx_list_t                       headers;
  213.     ngx_list_t                       trailers;

  214.     ngx_uint_t                       status_n;
  215.     ngx_str_t                        status_line;

  216.     ngx_table_elt_t                 *status;
  217.     ngx_table_elt_t                 *date;
  218.     ngx_table_elt_t                 *server;
  219.     ngx_table_elt_t                 *connection;

  220.     ngx_table_elt_t                 *expires;
  221.     ngx_table_elt_t                 *etag;
  222.     ngx_table_elt_t                 *x_accel_expires;
  223.     ngx_table_elt_t                 *x_accel_redirect;
  224.     ngx_table_elt_t                 *x_accel_limit_rate;

  225.     ngx_table_elt_t                 *content_type;
  226.     ngx_table_elt_t                 *content_length;

  227.     ngx_table_elt_t                 *last_modified;
  228.     ngx_table_elt_t                 *location;
  229.     ngx_table_elt_t                 *refresh;
  230.     ngx_table_elt_t                 *www_authenticate;
  231.     ngx_table_elt_t                 *transfer_encoding;
  232.     ngx_table_elt_t                 *vary;

  233.     ngx_table_elt_t                 *cache_control;
  234.     ngx_table_elt_t                 *set_cookie;

  235.     off_t                            content_length_n;
  236.     time_t                           last_modified_time;

  237.     unsigned                         connection_close:1;
  238.     unsigned                         chunked:1;
  239.     unsigned                         no_cache:1;
  240.     unsigned                         expired:1;
  241. } ngx_http_upstream_headers_in_t;


  242. typedef struct {
  243.     ngx_str_t                        host;
  244.     in_port_t                        port;
  245.     ngx_uint_t                       no_port; /* unsigned no_port:1 */

  246.     ngx_uint_t                       naddrs;
  247.     ngx_resolver_addr_t             *addrs;

  248.     struct sockaddr                 *sockaddr;
  249.     socklen_t                        socklen;
  250.     ngx_str_t                        name;

  251.     ngx_resolver_ctx_t              *ctx;
  252. } ngx_http_upstream_resolved_t;


  253. typedef void (*ngx_http_upstream_handler_pt)(ngx_http_request_t *r,
  254.     ngx_http_upstream_t *u);


  255. struct ngx_http_upstream_s {
  256.     ngx_http_upstream_handler_pt     read_event_handler;
  257.     ngx_http_upstream_handler_pt     write_event_handler;

  258.     ngx_peer_connection_t            peer;

  259.     ngx_event_pipe_t                *pipe;

  260.     ngx_chain_t                     *request_bufs;

  261.     ngx_output_chain_ctx_t           output;
  262.     ngx_chain_writer_ctx_t           writer;

  263.     ngx_http_upstream_conf_t        *conf;
  264.     ngx_http_upstream_srv_conf_t    *upstream;
  265. #if (NGX_HTTP_CACHE)
  266.     ngx_array_t                     *caches;
  267. #endif

  268.     ngx_http_upstream_headers_in_t   headers_in;

  269.     ngx_http_upstream_resolved_t    *resolved;

  270.     ngx_buf_t                        from_client;

  271.     ngx_buf_t                        buffer;
  272.     off_t                            length;
  273.     off_t                            early_hints_length;

  274.     ngx_chain_t                     *out_bufs;
  275.     ngx_chain_t                     *busy_bufs;
  276.     ngx_chain_t                     *free_bufs;

  277.     ngx_int_t                      (*input_filter_init)(void *data);
  278.     ngx_int_t                      (*input_filter)(void *data, ssize_t bytes);
  279.     void                            *input_filter_ctx;

  280. #if (NGX_HTTP_CACHE)
  281.     ngx_int_t                      (*create_key)(ngx_http_request_t *r);
  282. #endif
  283.     ngx_int_t                      (*create_request)(ngx_http_request_t *r);
  284.     ngx_int_t                      (*reinit_request)(ngx_http_request_t *r);
  285.     ngx_int_t                      (*process_header)(ngx_http_request_t *r);
  286.     void                           (*abort_request)(ngx_http_request_t *r);
  287.     void                           (*finalize_request)(ngx_http_request_t *r,
  288.                                          ngx_int_t rc);
  289.     ngx_int_t                      (*rewrite_redirect)(ngx_http_request_t *r,
  290.                                          ngx_table_elt_t *h, size_t prefix);
  291.     ngx_int_t                      (*rewrite_cookie)(ngx_http_request_t *r,
  292.                                          ngx_table_elt_t *h);

  293.     ngx_msec_t                       start_time;

  294.     ngx_http_upstream_state_t       *state;

  295.     ngx_str_t                        method;
  296.     ngx_str_t                        schema;
  297.     ngx_str_t                        uri;

  298. #if (NGX_HTTP_SSL || NGX_COMPAT)
  299.     ngx_str_t                        ssl_name;
  300.     ngx_str_t                        ssl_alpn_protocol;
  301. #endif

  302.     ngx_http_cleanup_pt             *cleanup;

  303.     unsigned                         store:1;
  304.     unsigned                         cacheable:1;
  305.     unsigned                         accel:1;
  306.     unsigned                         ssl:1;
  307. #if (NGX_HTTP_CACHE)
  308.     unsigned                         cache_status:3;
  309. #endif

  310.     unsigned                         buffering:1;
  311.     unsigned                         keepalive:1;
  312.     unsigned                         upgrade:1;
  313.     unsigned                         error:1;

  314.     unsigned                         request_sent:1;
  315.     unsigned                         request_body_sent:1;
  316.     unsigned                         request_body_blocked:1;
  317.     unsigned                         header_sent:1;
  318.     unsigned                         response_received:1;
  319. };


  320. typedef struct {
  321.     ngx_uint_t                      status;
  322.     ngx_uint_t                      mask;
  323. } ngx_http_upstream_next_t;


  324. typedef struct {
  325.     ngx_str_t   key;
  326.     ngx_str_t   value;
  327.     ngx_uint_t  skip_empty;
  328. } ngx_http_upstream_param_t;


  329. ngx_int_t ngx_http_upstream_create(ngx_http_request_t *r);
  330. void ngx_http_upstream_init(ngx_http_request_t *r);
  331. ngx_int_t ngx_http_upstream_non_buffered_filter_init(void *data);
  332. ngx_int_t ngx_http_upstream_non_buffered_filter(void *data, ssize_t bytes);
  333. ngx_http_upstream_srv_conf_t *ngx_http_upstream_add(ngx_conf_t *cf,
  334.     ngx_url_t *u, ngx_uint_t flags);
  335. char *ngx_http_upstream_bind_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
  336.     void *conf);
  337. char *ngx_http_upstream_param_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
  338.     void *conf);
  339. ngx_int_t ngx_http_upstream_hide_headers_hash(ngx_conf_t *cf,
  340.     ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev,
  341.     ngx_str_t *default_hide_headers, ngx_hash_init_t *hash);
  342. #if (NGX_HTTP_SSL)
  343. ngx_int_t ngx_http_upstream_merge_ssl_passwords(ngx_conf_t *cf,
  344.     ngx_http_upstream_conf_t *conf, ngx_http_upstream_conf_t *prev);
  345. #endif


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


  348. extern ngx_module_t        ngx_http_upstream_module;
  349. extern ngx_conf_bitmask_t  ngx_http_upstream_cache_method_mask[];
  350. extern ngx_conf_bitmask_t  ngx_http_upstream_ignore_headers_masks[];


  351. #endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */