Nginx Source Code Analysis-HTTP module-Ngx_http_block functions and initialization of HTTP modules

Source: Internet
Author: User
Tags data structures goto port number

The previous chapters have compiled the Nginx event module, which begins to tidy up the HTTP module.

The initialization of the HTTP module is done in the ngx_http_block function in src/http/nginx_http.c.
configuration of the HTTP module

Before looking at Nginx_http_block, we must also look at the configuration file of the HTTP large module in nginx.conf, only understand the configuration of the HTTP module, in order to better understand how the HTTP module is initialized.

The configuration is as follows:

HTTP {include mime.types;

  Default_type Application/octet-stream;
      
  #charset gb2312;
  Server_names_hash_bucket_size 128;
  Client_header_buffer_size 32k;
  Large_client_header_buffers 4 32k;
      
  Client_max_body_size 8m;
  Sendfile on;

  Tcp_nopush on;

  Keepalive_timeout 60;

  Tcp_nodelay on;
  Fastcgi_connect_timeout 300;
  Fastcgi_send_timeout 300;
  Fastcgi_read_timeout 300;
  Fastcgi_buffer_size 64k;
  Fastcgi_buffers 4 64k;
  Fastcgi_busy_buffers_size 128k;

  Fastcgi_temp_file_write_size 128k;
  gzip on;
  Gzip_min_length 1k;
  Gzip_buffers 4 16k;
  Gzip_http_version 1.0;
  Gzip_comp_level 2;
  Gzip_types text/plain application/x-javascript text/css application/xml;

  Gzip_vary on;

  #limit_zone crawler $binary _remote_addr 10m;
    server {Listen 80;
    #server_name blog.s135.com;
    Index index.html index.htm index.php;

    root/home/wwwroot/;    
                             
    #limit_conn crawler 20;Location ~. *\.
      (PHP|PHP5) $ {#fastcgi_pass unix:/tmp/php-cgi.sock;
      Fastcgi_pass 127.0.0.1:9000;
      Fastcgi_index index.php;
    Include fcgi.conf; } location ~. *\.
    (gif|jpg|jpeg|png|bmp|swf) $ {expires 30d; } location ~. *\.
    (JS|CSS)? $ {Expires 1h; } log_format Access ' $remote _addr-$remote _user [$time _local] "$request" ' $status $body _bytes_
    Sent "$http _referer" "$http _user_agent" $http _x_forwarded_for ";
      Access_log/usr/local/nginx-1.4.7/logs/access.log access;
    } server {Listen 80;
    server_name status.blog.s135.com;
    Location/{stub_status on;
    Access_log off; }
  }

From the above configuration file we can see that the configuration of HTTP is mainly divided into 4 layers:

1. Outermost http{} module. Module Type: Ngx_core_module (outermost core module)

2. Main configuration in the HTTP Core module: Http{include mine type;}. Module Type: Ngx_http_module (configuration information for the Global HTTP module)

3. Server configuration in the HTTP core module: server{}. Module Type: ngx_http_module (primarily configuration server information)

4. The location local information configuration in the HTTP core module: location{}. Module Type: Ngx_http_module (local resource information for primary server: Static resource, reverse proxy port address, various language container ports, etc.)

The outermost HTTP module, type Ngx_core_module, belongs to the core module, and the core module invokes the command set of the instruction at the beginning of the configuration file initialization. Therefore, when the core module is started, the HTTP module configuration parsing instruction function is invoked: Ngx_http_block


data structures for HTTP core modules

/** * HTTP Module Command set * HTTP module is also a large module, the outermost layer is: * HTTP {* .... *} * Ngx_http_block: This method is the callback function * HTTP Core module */static Ngx_comm and_t ngx_http_commands[] = {{ngx_string ("http"), ngx_main_conf| ngx_conf_block|

Ngx_conf_noargs, Ngx_http_block, 0, 0, NULL}, Ngx_null_command};

/** *http Core Module context */static ngx_core_module_t Ngx_http_module_ctx = {ngx_string ("http"), NULL, NULL};
    /** * HTTP CORE Module structure * Module type: Ngx_core_module * Resolve the HTTP module configuration in {} by calling the Ngx_http_block method */ngx_module_t Ngx_http_module = {                     NGX_MODULE_V1, &ngx_http_module_ctx,/* MODULE context */Ngx_http_commands,                                  /* Module directives */Ngx_core_module,/* Module type */NULL,                                  /* INIT master */NULL,/* INIT module */NULL, /* Init process */NULL,/* inIt thread */NULL,/* EXIT thread */NULL,/* EX It process */NULL,/* Exit Master */ngx_module_v1_padding};

From the structure above, you can see that http{} is ngx_core_module, when the core module is initialized, the callback function in the Ngx_http_commands command set is called and the configuration information of the core module is parsed one by one.

The HTTP module's total entry is the callback function for this http{} command set: Ngx_http_block

If you have forgotten how to parse the configuration file, please review the NGINX source analysis-Main process-parsing configuration file


Ngx_http_block Function Explanation

/** *ngx_http_commands Command Set callback function *http Module initialization entry function * */static char * Ngx_http_block (ngx_conf_t *cf, ngx_command_t *cmd,
    void *conf) {char *rv;
    ngx_uint_t mi, m, s;
    ngx_conf_t PCF;
    ngx_http_module_t *module;
    ngx_http_conf_ctx_t *ctx;
    ngx_http_core_loc_conf_t *CLCF;
    ngx_http_core_srv_conf_t **CSCFP;

    ngx_http_core_main_conf_t *cmcf;
    if (* (ngx_http_conf_ctx_t *) conf) {return "is duplicate"; }/* The main HTTP context/*/* Allocates a piece of memory, storing the HTTP configuration context */CTX = Ngx_pcalloc (cf->pool, sizeof (ngx_http_conf_c
    tx_t));
    if (CTX = = NULL) {return ngx_conf_error;


    } * (ngx_http_conf_ctx_t *) conf = CTX; /* Count the number of the HTTP modules and set up their indices */* Count HTTP Modules */ngx_http_max_module = Ngx_co


    Unt_modules (Cf->cycle, ngx_http_module); /* The HTTP main_conf context, it is the ' same in the ' All HTTP Contexts *//** * Outermost HTTP configuration * HTTP {include mime.types;
     Default_type Application/octet-stream; */ctx->main_conf = Ngx_pcalloc (cf->pool, sizeof (void *) * ngx_http_max_module)
    ;
    if (ctx->main_conf = = NULL) {return ngx_conf_error;
     }/* * The HTTP null srv_conf context, it is used to merge * The Server{}s ' srv_conf ' s *//**
    * Server Layer Configuration * server {Listen 80;
    #server_name blog.s135.com;
    Index index.html index.htm index.php;
     root/home/wwwroot/;
    */ctx->srv_conf = Ngx_pcalloc (cf->pool, sizeof (void *) * ngx_http_max_module);
    if (ctx->srv_conf = = NULL) {return ngx_conf_error;
     }/* * The HTTP null loc_conf context, it is used to merge * The Server{}s ' loc_conf ' s *//** * Location Layer Configuration location ~. *\. (PHP|PHP5) $ {#fastcgi_pass unix:/tmp/php-cgi.sock;
      Fastcgi_pass 127.0.0.1:9000;
      Fastcgi_index index.php;
    Include fcgi.conf;
    } */ctx->loc_conf = Ngx_pcalloc (cf->pool, sizeof (void *) * ngx_http_max_module);
    if (ctx->loc_conf = = NULL) {return ngx_conf_error;
     }/* * Create the main_conf ' s, the null srv_conf ' s, and the null loc_conf's * of the all HTTP modules *//** * Call: create_main_conf, create_srv_conf, create_loc_conf * Create configuration */for (m = 0; cf->cycl e->modules[m];
        m++) {if (Cf->cycle->modules[m]->type! = Ngx_http_module) {continue;
        } module = cf->cycle->modules[m]->ctx;

        Mi = cf->cycle->modules[m]->ctx_index;
            if (module->create_main_conf) {Ctx->main_conf[mi] = module->create_main_conf (CF);
            if (ctx->main_conf[mi] = = NULL) {return ngx_conf_error; }} if (Module->create_srv_conf) {Ctx->srv_conf[mi] = module->create_srv_conf (CF);
            if (ctx->srv_conf[mi] = = NULL) {return ngx_conf_error;
            }} if (module->create_loc_conf) {Ctx->loc_conf[mi] = module->create_loc_conf (CF);
            if (ctx->loc_conf[mi] = = NULL) {return ngx_conf_error;
    }}} PCF = *CF;

    Cf->ctx = CTX; /** * Preconfiguration Pre-initialize configuration information */for (m = 0; cf->cycle->modules[m]; m++) {if (cf->cycle
        ->modules[m]->type! = Ngx_http_module) {continue;

        } module = cf->cycle->modules[m]->ctx; if (module->preconfiguration) {if (module->preconfiguration (cf)! = NGX_OK) {return NGX
            _conf_error;
    }}}/* Parse inside the http{} block */Cf->module_type = Ngx_http_module; Cf->cmd_type =ngx_http_main_conf;

    RV = Ngx_conf_parse (cf, NULL);
    if (rv! = Ngx_conf_ok) {goto failed;

    }/* * init http{} main_conf ' s, merge the Server{}s ' srv_conf ' s * and its location{}s ' loc_conf ' s * * /** * Initialize main config * Merge server srv_conf * Merge location loc_conf */cmcf = Ctx->main_conf[ngx_htt
    P_core_module.ctx_index];

    CSCFP = cmcf->servers.elts;
            for (m = 0; cf->cycle->modules[m]; m++) {if (Cf->cycle->modules[m]->type! = ngx_http_module) {
        Continue
        } module = cf->cycle->modules[m]->ctx;

        Mi = cf->cycle->modules[m]->ctx_index; /* init http{} main_conf ' s */if (module->init_main_conf) {RV = module->init_main_conf (cf, CTX
            ->MAIN_CONF[MI]);
            if (rv! = Ngx_conf_ok) {goto failed;
        }} RV = Ngx_http_merge_servers (cf, CMCF, module, MI); if (RV!)= NGX_CONF_OK) {goto failed; }/* Create location Trees *//** * Creates location module trees */for (s = 0; s < cmcf->serv Ers.nelts;

        s++) {CLCF = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
        if (ngx_http_init_locations (cf, Cscfp[s], CLCF)! = NGX_OK) {return ngx_conf_error;
        } if (Ngx_http_init_static_location_trees (cf, CLCF)! = NGX_OK) {return ngx_conf_error;
    }} if (Ngx_http_init_phases (cf, cmcf)! = NGX_OK) {return ngx_conf_error;
    } if (Ngx_http_init_headers_in_hash (cf, cmcf)! = NGX_OK) {return ngx_conf_error; } for (m = 0; cf->cycle->modules[m]; m++) {if (Cf->cycle->modules[m]->type! = Ngx_http_modul
        E) {continue;

        } module = cf->cycle->modules[m]->ctx; if (module->postconfiguration) {if (Module->postconfiguration (cf) = Ngx_ok) {return ngx_conf_error;
    }}} if (Ngx_http_variables_init_vars (cf)! = NGX_OK) {return ngx_conf_error;
     }/* * http{} ' s cf->ctx is needed while the configuration merging * and in postconfiguration process


    */*cf = PCF;
    if (ngx_http_init_phase_handlers (cf, cmcf)! = NGX_OK) {return ngx_conf_error;  }/* Optimize the lists of ports, addresses and server names */* Ngx_http_optimize_servers Initialize Listen port number IP address
    Server and other listening information */if (ngx_http_optimize_servers (cf, CMCF, cmcf->ports)! = NGX_OK) {return ngx_conf_error;

} return NGX_CONF_OK;

    Failed: *CF = PCF;
return RV; }

The next chapter will explain in detail: ngx_http_optimize_servers



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.