How "Nginx" should respond to HTTP configuration

Source: Internet
Author: User

The same configuration items can occur within the same time as multiple blocks. such as HTTP slices, server slices, location slices. Blocks the value that is taken in the configuration item.
There are 4 steps to working with HTTP configuration items:
    1. Create a data structure to store the corresponding number of parameters for a configuration item
    2. Set constraints and callback methods when configuration items appear in the configuration file
    3. Implement the above callback method, or use the 14 callback methods of Nginx preset
    4. Merging different levels of identically named configuration items
1. The data structure that is allocated for saving the configuration parameters creates a struct to hold the parameters of interest. For example, define one of the following structures:
typedef struct{    ngx_str_t    my_str;    ngx_int_t    My_num;    ngx_flag_t   My_flag;    size_t      my_size;    ngx_array_t* My_str_array;    ngx_array_t* My_keyval;    off_t    My_off;    ngx_msec_t   my_msec;    time_t       my_sec;    ngx_bufs_t   My_bufs;    ngx_uint_t   my_enum_seq;    ngx_uint_t  My_bitmask;    ngx_uint_t   my_access;    ngx_path_t* My_path;     ngx_str_t    my_config_str;    ngx_int_t    My_config_num;} ngx_http_mytest_conf_t;


The HTTP framework only encounters http{}, server{}, location{} When parsing the configuration file nginx.conf. A struct that holds the number of references is assigned. These structures are generated by callback methods in the ngx_http_module_t interface:
typedef struct {    ngx_int_t   (*preconfiguration) (ngx_conf_t *cf);    Call    ngx_int_t   (*postconfiguration) (ngx_conf_t *CF) before parsing the configuration file;   Call     void       * (*create_main_conf) (ngx_conf_t *CF) After parsing the configuration file;    Create a struct body that stores configuration items directly under http{}    Char       * (*init_main_conf) (ngx_conf_t *cf, void *conf);  Initialize the main level configuration item     void       * (*create_srv_conf) (ngx_conf_t *cf);     Create a struct body that stores configuration items directly under srv{}    Char       * (*merge_srv_conf) (ngx_conf_t *cf, void *prev, void *conf);  Merges the same configuration entry     void       * (*create_loc_conf) (ngx_conf_t *cf) with the same name as the main level and the SRV level;     Create a struct body that stores configuration items directly under loc{}    Char       * (*merge_loc_conf) (ngx_conf_t *cf, void *prev, void *conf);  Merge the same configuration item with the same name as the SRV level and the LOC level} ngx_http_module_t;


Detailed steps such as the following:
    • Discover http{}, call create_main_conf, create_srv_conf, create_loc_conf
    • Find server{}, call create_srv_conf, create_loc_conf
    • Discover location{}. Call create_loc_conf
Ordinary HTTP modules generally only implement the Create_loc_conf method. Used to match a URL request, as defined in the following way:
Static void* ngx_http_mytest_create_loc_conf (ngx_conf_t *cf) {    ngx_http_mytest_conf_t  *mycf;     MYCF = (ngx_http_mytest_conf_t  *) ngx_pcalloc (cf->pool, sizeof (ngx_http_mytest_conf_t));    if (MYCF = = null)        return null;     Mycf->my_flag = Ngx_conf_unset;    Mycf->my_num = Ngx_conf_unset;    Mycf->my_str_array = ngx_conf_unset_ptr;    Mycf->my_keyval = NULL;    Mycf->my_off = Ngx_conf_unset;    Mycf->my_msec = ngx_conf_unset_msec;    Mycf->my_sec = Ngx_conf_unset;    Mycf->my_size = ngx_conf_unset_size;     return MYCF;}


2, set the resolution of the configuration item the parsing method of the configuration item is defined by the ngx_command_t array:
struct ngx_command_s {    ngx_str_t             name;     Configuration Item Name    ngx_uint_t            type;     The configuration item type, which contains the location where the configuration item can appear and the number of parameters that can be carried, or the     name configuration item that appears, calls this method to resolve the configuration item parameters    Char               * (*set) (ngx_conf_t *cf, ngx_command_t *cmd, void *conf);    ngx_uint_t            conf;     The offset in the configuration file that determines which storage structure the configuration item is placed in    ngx_uint_t            offset;   Which field of the storage structure the configuration item is placed in    void                 *post;      The processing method after the configuration item reads};


Member meanings such as the following:
    • Name: Configuration Item Name
    • Type: Determines which blocks the configuration item can appear in
    • Set: Callback method for parsing configuration items
    • Conf: tells the HTTP framework which structure the configuration item is placed in. Take the following values such as:
      • NGX_HTTP_MAIN_CONF_OFFSET: Structure storage configuration item parameters generated by create_main_conf
      • NGX_HTTP_SRV_CONF_OFFSET: Structure storage configuration item parameters generated by create_srv_conf
      • NGX_HTTP_LOC_CONF_OFFSET: Structure storage configuration item parameters generated by create_loc_conf
      • Under normal circumstances. The HTTP module implements the Create_loc_conf callback method. Then set the CONF to Ngx_http_loc_conf_offset
    • Offset: Represents the offset position of the current configuration item in the structure of the entire storage configuration item. The offset is computed by the macro offsetof (struct, member).

      The default parsing method of Nginx needs to determine the structure of storage configuration item parameters through Conf members. The member that stores the configuration item parameters is then determined by the offset member. But your own definition parsing method sets this to 0, because we define how the store is stored.

    • Post: Multi-purpose pointer. Generally not used
3.14 Preset methods for parsing configuration items take Ngx_conf_set_flag_slot as an example, which is used to parse configuration items with on or off parameters, such as the following ngx_command_s structure:
{    ngx_string ("Test_flag"),    ngx_http_loc_conf | Ngx_conf_flag,    Ngx_conf_set_flag_slot,    ngx_http_loc_conf_offset,    offsetof (ngx_http_mytest_conf_t, My_flag),    NULL},


Among the above-mentioned members. Ngx_conf_flag indicates that the configuration item has only one parameter and can only be on or off. Offsetof (ngx_http_mytest_conf_t, My_flag) indicates that the parameters after the configuration item test_flag are stored in ngx_http_mytest_conf_t members of the My_flag struct.


The remaining 13 preset methods are similar.
4. Define the configuration item processing method The purpose of this method is to store the configuration items of interest in a pre-allocated structure:

Static char* Ngx_conf_set_myconfig (ngx_conf_t *cf, ngx_command_t *cmd, void *conf) {/    * Parameters conf is the HTTP framework passed to us.     struct ngx_http_mytest_conf_t */    ngx_http_mytest_conf_t  assigned in Ngx_http_mytest_create_loc_conf * callback method *MYCF = conf;     /* Cf->args is a queue of 1 ngx_array_t. Its members are ngx_str_t structures.     * We use value to point to the ELTs content of ngx_array_t, where Value[1] is the 1th     * parameter, value[2] is the 2nd parameter */    ngx_str_t* value = cf-> args->elts;     The nelts of the ngx_array_t indicates the number of arguments    if (cf->args->nelts > 1)        mycf->my_config_str = value[1];     if (Cf->args->nelts > 2)    {        mycf->my_config_num = Ngx_atoi (Value[2].data, Value[2].len);     /* string-to-integer type *////         //Assuming string conversion integer failed, will be reported "invalid number" error, Nginx boot failure        if (mycf->my_config_num = = ngx_error)            Return "Invalid number";    }     return NGX_CONF_OK; Return success}


When present such as the following ngx_command_s:
{    ngx_string ("Test_myconfig"),    ngx_http_loc_conf | Ngx_conf_take12,    ngx_conf_set_myconfig,    Ngx_http_loc_conf_offset,    0,    NULL},


When the Test_myconfig configuration item appears in the configuration file, it calls our own defined Ngx_conf_set_myconfig method to parse the configuration item, storing the configuration item parameters. For example: Test_myconfig Test 123 Then after parsing: mycf->my_config_str = Testmycf->my_config_num = 123
5, the configuration item merges if NGX_HTTP_MODULE_T.MERGE_LOC_CONF is null, then the configuration item with the same name in the outer configuration block http{] and server{} will not take effect. So, if you need to merge configuration items with the same name, define the MERGE_LOC_CONF function. The merge_srv_conf function works the same way. How to merge is free, such as merge_loc_conf can be defined as the following:
The static char * ngx_http_mytest_merge_loc_conf (ngx_conf_t *cf, void *parent, void *child) {/    * * Parent points to the corresponding struct body of the parent configuration block. Child points to the corresponding structure of the sub-configuration block *    /ngx_http_mytest_conf_t *prev = (ngx_http_mytest_conf_t *) parent;    ngx_http_mytest_conf_t *conf = (ngx_http_mytest_conf_t *) child;     /* conf = prev or "defaultstr" */    Ngx_conf_merge_str_value (Conf->my_str, Prev->my_str, "defaultstr");     /* conf = prev or 0 */    ngx_conf_merge_value (Conf->my_flag, Prev->my_flag, 0);     return NGX_CONF_OK;}


There are 10 ways to combine configuration items, Ngx_conf_merge_str_value and Ngx_conf_merge_value are two of them.


References: "In-depth understanding Nginx" p115-p140.

Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

How "Nginx" should respond to HTTP configuration

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.