Reading and practice of "deep understanding Nginx" (III.)

Source: Internet
Author: User
Tags nginx server

Access to third party services using upstream and Subrequest

First, upstream: to the request to the Nginx server into the Google Server search request for example (i) Module framework

The first thing to be clear here is to write a module that uses upstream instead of writing a upstream module. Therefore, similar to HelloWorld, the modular structure body Ngx_http_mytest_module, the module context structure body NGX_HTTP_MYTEST_MODULE_CTX, the array ngx_http_mytest_command[], The framework of Methods Ngx_http_mytest () and Ngx_http_mytest_handler () is not small and very similar. If you forget the relationship between them, please review the original book or "Deep Understanding nginx" reading and Practice (i): Nginx installation configuration and HelloWorld.

The request that the module handles is the ngx_http_request_t struct object R, which contains a ngx_http_upstream_t type member upstream. When upstream is not NULL, the way in which Third-party services are accessed is customized based on the content that is set. The processing of this request is done by the upstream module. From this point of view, it is not one thing to distinguish between the upstream members in request R and the upstream modules provided by Nginx, but rather the former to guide the latter's work. The setting and opening of the former (i.e. informing the upstream module to be processed and implemented by Ngx_http_upstream_init) is done by the third party module (mytest) that we have written.

(ii) Upstream settings

The configuration of the upstream work can be done by filling in the ngx_http_upstream_t structure in Request R, which is passed in by Ngx_http_upstream_create (ngx_http_request_t *r). When this function returns successfully, the upstream in Request R is set to NOT NULL. NGX_HTTP_UPSTREAM_T structure is mainly composed of a few members, because the original book on the module to be used to write a detailed description of the members (the temporary use of members in Chapter 12), here only to do a part of the generalization:

typedef ngx_http_upstream_s NGX_HTTP_UPSTREAM_T; STURCT ngx_http_upstream_s {... ngx_chain_t request_bufs;//the request to the upstream server, completed by Create_request () ngx_http_upstre am_conf_t conf;//Timeout and other restrictive parameters ngx_http_upstream_resolved_t resolved;//for directly specified upstream server address//setting method see MyTest Module Ngx_http _mytest_handler () method * * 3 callback methods that must be implemented/* NGX_INT_T (*create_request) (ngx_http_request_t *r);//construct upstream service The requested content sent by the When MyTest is invoked, only one ngx_int_t (*process_header) (ngx_http_request_t *r) is called, or the method void for header processing after the upstream server is received (*finalize_re  Quest) (ngx_http_request_t *r, ngx_int_t RC)//* 5 Optional callback method is called when the upstream request is destroyed. (*input_filter_init) (void *data);//processing upstream ngx_int_t (*input_filter) (void *data,ssize_t bytes);//Processing upstream package ngx_int_t (*reinit_requ
    
  EST) (ngx_http_request_t *r);//call void (*abort_request) (ngx_http_request_t *r) When a connection to the upstream server fails for the first time; ngx_int_t (*rewrite_redirect) (ngx_http_request_t *r, ngx_table_elt_t *h, Size_t prefix); Mainly used for reverse proxy ...}

As you can see, when you use the upstream feature, you need to write your own modules and provide a way to process configuration items in addition to HelloWorld ngx_http_mytest_create_loc_conf (), NGX_HTTP_MYTEST_MERGE_LOC_ Conf (), you also need to fill in the ngx_http_upstream_t structure and implement the 3 required callback methods. Note that these callback methods are provided by the creator of the module and then invoked by the upstream module.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.