src/http/ngx_http_upstream.h - nginx-1.31.4 nginx/ @ 8d9666701

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_COMPAT_BEGIN(2)
  59.     NGX_COMPAT_END
  60. } ngx_http_upstream_state_t;


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

  66. typedef struct ngx_http_upstream_srv_conf_s  ngx_http_upstream_srv_conf_t;

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


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


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

  86.     unsigned                         backup:1;

  87. #if (NGX_HTTP_UPSTREAM_ZONE)
  88.     ngx_str_t                        host;
  89.     ngx_str_t                        service;
  90. #endif

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


  95. #define NGX_HTTP_UPSTREAM_CREATE        0x0001
  96. #define NGX_HTTP_UPSTREAM_WEIGHT        0x0002
  97. #define NGX_HTTP_UPSTREAM_MAX_FAILS     0x0004
  98. #define NGX_HTTP_UPSTREAM_FAIL_TIMEOUT  0x0008
  99. #define NGX_HTTP_UPSTREAM_DOWN          0x0010
  100. #define NGX_HTTP_UPSTREAM_BACKUP        0x0020
  101. #define NGX_HTTP_UPSTREAM_MODIFY        0x0040
  102. #define NGX_HTTP_UPSTREAM_MAX_CONNS     0x0100


  103. struct ngx_http_upstream_srv_conf_s {
  104.     ngx_http_upstream_peer_t         peer;
  105.     void                           **srv_conf;

  106.     ngx_array_t                     *servers;  /* ngx_http_upstream_server_t */

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

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


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


  126. typedef struct {
  127.     ngx_http_upstream_srv_conf_t    *upstream;

  128.     ngx_msec_t                       connect_timeout;
  129.     ngx_msec_t                       send_timeout;
  130.     ngx_msec_t                       read_timeout;
  131.     ngx_msec_t                       next_upstream_timeout;

  132.     size_t                           send_lowat;
  133.     size_t                           buffer_size;
  134.     ngx_http_complex_value_t        *limit_rate;

  135.     size_t                           busy_buffers_size;
  136.     size_t                           max_temp_file_size;
  137.     size_t                           temp_file_write_size;

  138.     size_t                           busy_buffers_size_conf;
  139.     size_t                           max_temp_file_size_conf;
  140.     size_t                           temp_file_write_size_conf;

  141.     ngx_bufs_t                       bufs;

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

  152.     ngx_flag_t                       ignore_client_abort;
  153.     ngx_flag_t                       intercept_errors;
  154.     ngx_flag_t                       cyclic_temp_file;
  155.     ngx_flag_t                       force_ranges;

  156.     ngx_path_t                      *temp_path;

  157.     ngx_hash_t                       hide_headers_hash;
  158.     ngx_array_t                     *hide_headers;
  159.     ngx_array_t                     *pass_headers;

  160.     ngx_http_upstream_local_t       *local;
  161.     ngx_flag_t                       socket_keepalive;
  162.     size_t                           socket_rcvbuf;
  163.     size_t                           socket_sndbuf;

  164. #if (NGX_HTTP_CACHE)
  165.     ngx_shm_zone_t                  *cache_zone;
  166.     ngx_http_complex_value_t        *cache_value;

  167.     ngx_uint_t                       cache_min_uses;
  168.     ngx_uint_t                       cache_use_stale;
  169.     ngx_uint_t                       cache_methods;

  170.     off_t                            cache_max_range_offset;

  171.     ngx_flag_t                       cache_lock;
  172.     ngx_msec_t                       cache_lock_timeout;
  173.     ngx_msec_t                       cache_lock_age;

  174.     ngx_flag_t                       cache_revalidate;
  175.     ngx_flag_t                       cache_convert_head;
  176.     ngx_flag_t                       cache_background_update;

  177.     ngx_array_t                     *cache_valid;
  178.     ngx_array_t                     *cache_bypass;
  179.     ngx_array_t                     *cache_purge;
  180.     ngx_array_t                     *no_cache;
  181. #endif

  182.     ngx_array_t                     *store_lengths;
  183.     ngx_array_t                     *store_values;

  184. #if (NGX_HTTP_CACHE)
  185.     signed                           cache:2;
  186. #endif
  187.     signed                           store:2;
  188.     unsigned                         intercept_404:1;
  189.     unsigned                         change_buffering:1;
  190.     unsigned                         preserve_output:1;
  191.     unsigned                         ignore_input:1;

  192. #if (NGX_HTTP_SSL || NGX_COMPAT)
  193.     ngx_ssl_t                       *ssl;
  194.     ngx_flag_t                       ssl_session_reuse;

  195.     ngx_http_complex_value_t        *ssl_name;
  196.     ngx_flag_t                       ssl_server_name;
  197.     ngx_flag_t                       ssl_verify;

  198.     ngx_http_complex_value_t        *ssl_certificate;
  199.     ngx_http_complex_value_t        *ssl_certificate_key;
  200.     ngx_ssl_cache_t                 *ssl_certificate_cache;
  201.     ngx_array_t                     *ssl_passwords;
  202. #endif

  203.     ngx_str_t                        module;

  204.     NGX_COMPAT_BEGIN(5)
  205.     NGX_COMPAT_END
  206. } ngx_http_upstream_conf_t;


  207. typedef struct {
  208.     ngx_str_t                        name;
  209.     ngx_http_header_handler_pt       handler;
  210.     ngx_uint_t                       offset;
  211.     ngx_http_header_handler_pt       copy_handler;
  212.     ngx_uint_t                       conf;
  213.     ngx_uint_t                       redirect;  /* unsigned   redirect:1; */
  214. } ngx_http_upstream_header_t;


  215. typedef struct {
  216.     ngx_list_t                       headers;
  217.     ngx_list_t                       trailers;

  218.     ngx_uint_t                       status_n;
  219.     ngx_str_t                        status_line;

  220.     ngx_table_elt_t                 *status;
  221.     ngx_table_elt_t                 *date;
  222.     ngx_table_elt_t                 *server;
  223.     ngx_table_elt_t                 *connection;

  224.     ngx_table_elt_t                 *expires;
  225.     ngx_table_elt_t                 *etag;
  226.     ngx_table_elt_t                 *x_accel_expires;
  227.     ngx_table_elt_t                 *x_accel_redirect;
  228.     ngx_table_elt_t                 *x_accel_limit_rate;

  229.     ngx_table_elt_t                 *content_type;
  230.     ngx_table_elt_t                 *content_length;

  231.     ngx_table_elt_t                 *last_modified;
  232.     ngx_table_elt_t                 *location;
  233.     ngx_table_elt_t                 *refresh;
  234.     ngx_table_elt_t                 *www_authenticate;
  235.     ngx_table_elt_t                 *transfer_encoding;
  236.     ngx_table_elt_t                 *vary;

  237.     ngx_table_elt_t                 *cache_control;
  238.     ngx_table_elt_t                 *set_cookie;

  239.     off_t                            content_length_n;
  240.     time_t                           last_modified_time;

  241.     unsigned                         connection_close:1;
  242.     unsigned                         chunked:1;
  243.     unsigned                         no_cache:1;
  244.     unsigned                         expired:1;
  245. } ngx_http_upstream_headers_in_t;


  246. typedef struct {
  247.     ngx_str_t                        host;
  248.     in_port_t                        port;
  249.     ngx_uint_t                       no_port; /* unsigned no_port:1 */

  250.     ngx_uint_t                       naddrs;
  251.     ngx_resolver_addr_t             *addrs;

  252.     struct sockaddr                 *sockaddr;
  253.     socklen_t                        socklen;
  254.     ngx_str_t                        name;

  255.     ngx_resolver_ctx_t              *ctx;
  256. } ngx_http_upstream_resolved_t;


  257. typedef void (*ngx_http_upstream_handler_pt)(ngx_http_request_t *r,
  258.     ngx_http_upstream_t *u);


  259. struct ngx_http_upstream_s {
  260.     ngx_http_upstream_handler_pt     read_event_handler;
  261.     ngx_http_upstream_handler_pt     write_event_handler;

  262.     ngx_peer_connection_t            peer;

  263.     ngx_event_pipe_t                *pipe;

  264.     ngx_chain_t                     *request_bufs;

  265.     ngx_output_chain_ctx_t           output;
  266.     ngx_chain_writer_ctx_t           writer;

  267.     ngx_http_upstream_conf_t        *conf;
  268.     ngx_http_upstream_srv_conf_t    *upstream;
  269. #if (NGX_HTTP_CACHE)
  270.     ngx_array_t                     *caches;
  271. #endif

  272.     ngx_http_upstream_headers_in_t   headers_in;

  273.     ngx_http_upstream_resolved_t    *resolved;

  274.     ngx_buf_t                        from_client;

  275.     ngx_buf_t                        buffer;
  276.     off_t                            length;
  277.     off_t                            early_hints_length;

  278.     ngx_chain_t                     *out_bufs;
  279.     ngx_chain_t                     *busy_bufs;
  280.     ngx_chain_t                     *free_bufs;

  281.     ngx_int_t                      (*input_filter_init)(void *data);
  282.     ngx_int_t                      (*input_filter)(void *data, ssize_t bytes);
  283.     void                            *input_filter_ctx;

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

  297.     ngx_msec_t                       start_time;

  298.     ngx_http_upstream_state_t       *state;

  299.     ngx_str_t                        method;
  300.     ngx_str_t                        schema;
  301.     ngx_str_t                        uri;

  302. #if (NGX_HTTP_SSL || NGX_COMPAT)
  303.     ngx_str_t                        ssl_name;
  304.     ngx_str_t                        ssl_alpn_protocol;
  305. #endif

  306.     ngx_http_cleanup_pt             *cleanup;

  307.     unsigned                         store:1;
  308.     unsigned                         cacheable:1;
  309.     unsigned                         accel:1;
  310.     unsigned                         ssl:1;
  311. #if (NGX_HTTP_CACHE)
  312.     unsigned                         cache_status:3;
  313. #endif

  314.     unsigned                         buffering:1;
  315.     unsigned                         keepalive:1;
  316.     unsigned                         upgrade:1;
  317.     unsigned                         error:1;

  318.     unsigned                         request_sent:1;
  319.     unsigned                         request_body_sent:1;
  320.     unsigned                         request_body_blocked:1;
  321.     unsigned                         header_sent:1;
  322.     unsigned                         response_received:1;
  323. };


  324. typedef struct {
  325.     ngx_uint_t                      status;
  326.     ngx_uint_t                      mask;
  327. } ngx_http_upstream_next_t;


  328. typedef struct {
  329.     ngx_str_t   key;
  330.     ngx_str_t   value;
  331.     ngx_uint_t  skip_empty;
  332. } ngx_http_upstream_param_t;


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


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


  352. extern ngx_module_t        ngx_http_upstream_module;
  353. extern ngx_conf_bitmask_t  ngx_http_upstream_cache_method_mask[];
  354. extern ngx_conf_bitmask_t  ngx_http_upstream_ignore_headers_masks[];


  355. #endif /* _NGX_HTTP_UPSTREAM_H_INCLUDED_ */