欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

全面了解Nginx中的HTTP協(xié)議相關(guān)模塊配置

 更新時(shí)間:2016年07月13日 18:12:35   投稿:goldensun  
HTTP的處理是Nginx服務(wù)器的最重要功能,這里我們就帶大家來全面了解Nginx中的HTTP協(xié)議相關(guān)模塊配置,需要的朋友可以參考下

要理解 HTTP 模塊配置解析的過程,首先需要對 nginx 的配置文件結(jié)構(gòu)做一個(gè)了解

nginx 的配置文件是用樹狀結(jié)構(gòu)組織的,每個(gè) NGX_CORE_MODULE 作為根統(tǒng)領(lǐng)著其下的所有配置項(xiàng)

而如下圖所示,HTTP 模塊的配置被分成了 main、server、location 三層

2016713180141450.png (901×409)

整個(gè) nginx 配置解析的過程其實(shí)就是這棵樹的深度遍歷過程

而遍歷 HTTP 子樹的函數(shù)就是下面要介紹的 ngx_http_block

配置文件解析 -- http 配置塊
當(dāng)我們需要使用 http 模塊的時(shí)候,我們需要在配置文件中加入 http 配置塊:

http { // http 配置塊 {{{
  include    mime.types;
  default_type application/octet-stream;
  #log_format main '$remote_addr - $remote_user [$time_local] "$request" 
'
  #         '$status $body_bytes_sent "$http_referer" '
  #         '"$http_user_agent" "$http_x_forwarded_for"';
  #access_log logs/access.log main;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;
  server {
    listen    8001;
    server_name localhost;
    #autoindex  on;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      root  /var/www/;
      index index.html index.htm index.php;

在 http 配置塊中,我們配置了 http 連接相關(guān)的信息,HTTP 框架也正是從這里啟動的.

在 nginx 初始化的過程中,執(zhí)行了 ngx_init_cycle 函數(shù),其中進(jìn)行了配置文件解析,調(diào)用了 ngx_conf_parse 函數(shù)

配置文件解析

函數(shù) ngx_conf_handler 根據(jù)配置項(xiàng)的 command 調(diào)用了對應(yīng)的 set 回調(diào)函數(shù)

// static ngx_int_t ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last)
// 配置項(xiàng)解析 {{{
static ngx_int_t
ngx_conf_handler(ngx_conf_t *cf, ngx_int_t last)
{
  char      *rv;
  void      *conf, **confp;
  ngx_uint_t   i, found;
  ngx_str_t   *name;
  ngx_command_t *cmd;
  name = cf->args->elts;
  found = 0;
  for (i = 0; ngx_modules[i]; i++) {
    cmd = ngx_modules[i]->commands;
    if (cmd == NULL) {
      continue;
    }
    for ( /* void */ ; cmd->name.len; cmd++) {
      if (name->len != cmd->name.len) {
        continue;
      }
      if (ngx_strcmp(name->data, cmd->name.data) != 0) {
        continue;
      }

閱讀各模塊的 ngx_command_t 命令配置結(jié)構(gòu),可以找到:

// static ngx_command_t ngx_http_commands
// http 模塊命令結(jié)構(gòu) {{{
static ngx_command_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 配置塊解析 -- ngx_http_block

2016713180305891.jpg (391×1645)

在解析到 http 配置塊時(shí),執(zhí)行了對應(yīng)的 set 回調(diào)函數(shù) ngx_http_block

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;

  /* the main http context */

  ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
  if (ctx == NULL) {
    return NGX_CONF_ERROR;
  }

 // 創(chuàng)建 http 配置結(jié)構(gòu),保存所有 http 模塊的配置信息
  *(ngx_http_conf_ctx_t **) conf = ctx;


  /* count the number of the http modules and set up their indices */

  ngx_http_max_module = 0;
  for (m = 0; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

 // 重新設(shè)定 HTTP 模塊序號
    ngx_modules[m]->ctx_index = ngx_http_max_module++;
  }


  /* the http main_conf context, it is the same in the all http contexts */

  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
   */

  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
   */

  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
   */

  for (m = 0; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[m]->ctx;
    mi = ngx_modules[m]->ctx_index;

    if (module->create_main_conf) {
  // 調(diào)用每個(gè)模塊的 create_main_conf 回調(diào)函數(shù)
  // 創(chuàng)建自己的 main_conf
  //
  // ngx_http_core_module  ngx_http_core_create_main_conf
  // ngx_http_log_module  ngx_http_log_create_main_conf
  // ngx_http_upstream_module  ngx_http_upstream_create_main_conf
  // ngx_http_map_module  ngx_http_map_create_conf
  // ngx_http_ssi_filter_module ngx_http_ssi_create_main_conf
  // ngx_http_charset_filter_module ngx_http_charset_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) {
  // 調(diào)用每個(gè)模塊的 create_srv_conf 回調(diào)函數(shù)
  // 創(chuàng)建自己的 srv_conf
  //
  // ngx_http_core_module   ngx_http_core_create_srv_conf
  // ngx_http_ssl_module   ngx_http_ssl_create_srv_conf
  // ngx_http_upstream_hash_module ngx_http_upstream_hash_create_conf
  // ngx_http_upstream_least_conn_module ngx_http_upstream_least_conn_create_conf
  // ngx_http_upstream_keepalive_module ngx_http_upstream_keepalive_create_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) {
  // 調(diào)用每個(gè)模塊的 create_loc_conf 回調(diào)函數(shù)
  // 創(chuàng)建自己的 loc_conf
  //
  // ngx_http_core_module  ngx_http_core_create_loc_conf
  // ngx_http_log_module  ngx_http_log_create_loc_conf
  // ngx_http_gzip_static_module ngx_http_gzip_static_create_conf
  // ngx_http_autoindex_module ngx_http_autoindex_create_loc_conf
  // ngx_http_index_module  ngx_http_index_create_loc_conf
  // ngx_http_auth_basic_module ngx_http_auth_basic_create_loc_conf
  // ngx_http_access_module  ngx_http_access_create_loc_conf
  // ngx_http_limit_conn_module ngx_http_limit_conn_create_conf
  // ngx_http_limit_req_module ngx_http_limit_req_create_conf
  // ngx_http_referer_module  ngx_http_referer_create_conf
  // ngx_http_rewrite_module  ngx_http_rewrite_create_loc_conf
  // ngx_http_proxy_module  ngx_http_proxy_create_loc_conf
  // ngx_http_fastcgi_module  ngx_http_fastcgi_create_loc_conf
  // ngx_http_uwsgi_module  ngx_http_uwsgi_create_loc_conf
  // ngx_http_scgi_module  ngx_http_scgi_create_loc_conf
  // ngx_http_memcached_module ngx_http_memcached_create_loc_conf
  // ngx_http_browser_module  ngx_http_browser_create_conf
  // ngx_http_gzip_filter_module ngx_http_gzip_create_conf
  // ngx_http_ssi_filter_module ngx_http_ssi_create_loc_conf
  // ngx_http_charset_filter_module ngx_http_charset_create_loc_conf
  // ngx_http_userid_filter_module ngx_http_userid_create_conf
  // ngx_http_headers_filter_module ngx_http_headers_create_conf
  // ngx_http_copy_filter_module ngx_http_copy_filter_create_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;

  for (m = 0; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[m]->ctx;

    if (module->preconfiguration) {
  // 調(diào)用每個(gè)模塊的 preconfiguration 回調(diào)函數(shù)
  //
  // ngx_http_core_module  ngx_http_core_preconfiguration
  // ngx_http_upstream_module  ngx_http_upstream_add_variables
  // ngx_http_ssl_module  ngx_http_ssl_add_variables
  // ngx_http_proxy_module  ngx_http_proxy_add_variables
  // ngx_http_fastcgi_module  ngx_http_fastcgi_add_variables
  // ngx_http_browser_module  ngx_http_browser_add_variable
  // ngx_http_stub_status_module ngx_http_stub_status_add_variables
  // ngx_http_gzip_filter_module ngx_http_gzip_add_variables
  // ngx_http_ssi_filter_module ngx_http_ssi_preconfiguration
  // ngx_http_userid_filter_module ngx_http_userid_add_variables
      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
   */

  cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];
  cscfp = cmcf->servers.elts;

  for (m = 0; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[m]->ctx;
    mi = ngx_modules[m]->ctx_index;

    /* init http{} main_conf's */

    if (module->init_main_conf) {
  // 調(diào)用每個(gè)模塊的 init_main_conf 回調(diào)函數(shù)
  // 初始化自己的 main_conf
  //
  // ngx_http_core_module  ngx_http_core_init_main_conf
  // ngx_http_upstream_module ngx_http_upstream_init_main_conf
  // ngx_http_ssi_filter_module ngx_http_ssi_init_main_conf
      rv = module->init_main_conf(cf, ctx->main_conf[mi]);
      if (rv != NGX_CONF_OK) {
        goto failed;
      }
    }

 // 合并同名配置項(xiàng)
    rv = ngx_http_merge_servers(cf, cmcf, module, mi);
    if (rv != NGX_CONF_OK) {
      goto failed;
    }
  }


  /* create location trees */

  for (s = 0; s < cmcf->servers.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; ngx_modules[m]; m++) {
    if (ngx_modules[m]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[m]->ctx;

    if (module->postconfiguration) {
  // 調(diào)用每個(gè) HTTP 模塊的 postconfiguration 回調(diào)函數(shù)
  //
  // ngx_http_log_module   ngx_http_log_init
  // ngx_http_static_module  ngx_http_static_init
  // ngx_http_gzip_static_module  ngx_http_gzip_static_init
  // ngx_http_autoindex_module  ngx_http_autoindex_init
  // ngx_http_index_module  ngx_http_index_init
  // ngx_http_auth_basic_module  ngx_http_auth_basic_init
  // ngx_http_access_module  ngx_http_access_init
  // ngx_http_limit_conn_module  ngx_http_limit_conn_init
  // ngx_http_limit_req_module  ngx_http_limit_req_init
  // ngx_http_rewrite_module  ngx_http_rewrite_init
  // ngx_http_ssl_module   ngx_http_ssl_init
  // ngx_http_write_filter_module  ngx_http_write_filter_init
  // ngx_http_header_filter_module ngx_http_header_filter_init
  // ngx_http_chunked_filter_module ngx_http_chunked_filter_init
  // ngx_http_range_body_filter_module ngx_http_range_body_filter_init
  // ngx_http_gzip_filter_module  ngx_http_gzip_filter_init
  // ngx_http_postpone_filter_module ngx_http_postpone_filter_init
  // ngx_http_ssi_filter_module  ngx_http_ssi_filter_init
  // ngx_http_charset_filter_module ngx_http_charset_postconfiguration
  // ngx_http_userid_filter_module ngx_http_userid_init
  // ngx_http_headers_filter_module ngx_http_headers_filter_init
  // ngx_http_copy_filter_module  ngx_http_copy_filter_init
  // ngx_http_range_body_filter_module ngx_http_range_body_filter_init
  // ngx_http_not_modified_filter_module ngx_http_not_modified_filter_init
      if (module->postconfiguration(cf) != NGX_OK) {
        return NGX_CONF_ERROR;
      }
    }
  }

 // 初始化所有的變量
 // 不僅包括HTTP core模塊的變量
 // 也包括其他的HTTP模塊導(dǎo)出的變量,以及配置文件中使用 set 命令設(shè)置的變量
 // 這里的初始化包括初始化hash表,以及初始化數(shù)組索引
  if (ngx_http_variables_init_vars(cf) != NGX_OK) {
    return NGX_CONF_ERROR;
  }

  /*
   * http{}'s cf->ctx was needed while the configuration merging
   * and in postconfiguration process
   */

  *cf = pcf;


 // 初始化 phase_engine 結(jié)構(gòu)
  if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) {
    return NGX_CONF_ERROR;
  }


  /* optimize the lists of ports, addresses and server names */

 // 創(chuàng)建 http 連接,并設(shè)置為監(jiān)聽狀態(tài)
  if (ngx_http_optimize_servers(cf, cmcf, cmcf->ports) != NGX_OK) {
    return NGX_CONF_ERROR;
  }

  return NGX_CONF_OK;

failed:

  *cf = pcf;

  return rv;
} // }}}

這個(gè)函數(shù)中,為所有的 http 模塊都分配并創(chuàng)建了配置結(jié)構(gòu),同時(shí),調(diào)用了每個(gè)模塊相應(yīng)的初始化回調(diào)。

最后,調(diào)用 ngx_http_optimize_servers 創(chuàng)建了 http 連接,加入 cycle 的監(jiān)聽數(shù)組,并設(shè)為監(jiān)聽狀態(tài)。

nginx 配置文件對 http 模塊的配置分為三層:main、sever、location,因此,http 模塊上下文 ngx_http_module_t 中定義了以下六個(gè)回調(diào)函數(shù),用來創(chuàng)建和保存配置信息:

  • create_main_conf
  • init_main_conf
  • create_srv_conf
  • merge_srv_conf
  • create_loc_conf
  • merge_loc_conf

在 ngx_http_block 中,循環(huán)調(diào)用了所有 NGX_HTTP_MODULE 的這六個(gè)回調(diào)函數(shù),創(chuàng)建相關(guān)的配置結(jié)構(gòu)。

server、location 配置解析 -- ngx_http_core_server、ngx_http_core_location
在調(diào)用所有 HTTP 模塊的 create_main_conf、create_srv_conf、create_loc_conf 后,所有需要配置結(jié)構(gòu)的模塊都完成了配置結(jié)構(gòu)的創(chuàng)建,于是在調(diào)用所有模塊的 preconfiguration 回調(diào)函數(shù)后,配置解析工作正式展開

通過調(diào)用 ngx_conf_parse 函數(shù),開始了 http 配置塊的解析,并通過解析到的命令調(diào)用相應(yīng)的函數(shù)

在首個(gè) NGX_HTTP_MODULE ngx_http_core_module 的 ngx_command_t 域中包含了大量的配置指令,它們都是在http{}塊中出現(xiàn)的,其中包括兩個(gè)重要的指令:

{ ngx_string("listen"),
 NGX_HTTP_SRV_CONF|NGX_CONF_1MORE,
 ngx_http_core_listen,
 NGX_HTTP_SRV_CONF_OFFSET,
 0,
 NULL },

{ ngx_string("server"),
 NGX_HTTP_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS,
 ngx_http_core_server,
 0,
 0,
 NULL },

{ ngx_string("location"),
 NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_BLOCK|NGX_CONF_TAKE12,
 ngx_http_core_location,
 NGX_HTTP_SRV_CONF_OFFSET,
 0,
 NULL },

這里配置了 listen、server 與 location 塊的解析函數(shù) ngx_http_core_listen、ngx_http_core_server 和 ngx_http_core_location.


server 塊解析 -- ngx_http_core_server

// static char *
// ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
// http server 塊解析 {{{
static char *
ngx_http_core_server(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
{
  char            *rv;
  void            *mconf;
  ngx_uint_t          i;
  ngx_conf_t          pcf;
  ngx_http_module_t      *module;
  struct sockaddr_in     *sin;
  ngx_http_conf_ctx_t     *ctx, *http_ctx;
  ngx_http_listen_opt_t    lsopt;
  ngx_http_core_srv_conf_t  *cscf, **cscfp;
  ngx_http_core_main_conf_t  *cmcf;

  ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
  if (ctx == NULL) {
    return NGX_CONF_ERROR;
  }

  http_ctx = cf->ctx;
  ctx->main_conf = http_ctx->main_conf;

  /* the server{}'s srv_conf */

 // 為所有 HTTP 模塊分配空間存儲 srv_conf
  ctx->srv_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
  if (ctx->srv_conf == NULL) {
    return NGX_CONF_ERROR;
  }

  /* the server{}'s loc_conf */

 // 為所有 HTTP 模塊分配空間存儲 loc_conf
  ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
  if (ctx->loc_conf == NULL) {
    return NGX_CONF_ERROR;
  }

 // 循環(huán)調(diào)用每個(gè)模塊的 create_srv_conf 與 create_loc_conf 回調(diào),創(chuàng)建配置結(jié)構(gòu)
  for (i = 0; ngx_modules[i]; i++) {
    if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[i]->ctx;

    if (module->create_srv_conf) {
      mconf = module->create_srv_conf(cf);
      if (mconf == NULL) {
        return NGX_CONF_ERROR;
      }

      ctx->srv_conf[ngx_modules[i]->ctx_index] = mconf;
    }

    if (module->create_loc_conf) {
      mconf = module->create_loc_conf(cf);
      if (mconf == NULL) {
        return NGX_CONF_ERROR;
      }

      ctx->loc_conf[ngx_modules[i]->ctx_index] = mconf;
    }
  }


  /* the server configuration context */

  cscf = ctx->srv_conf[ngx_http_core_module.ctx_index];
  cscf->ctx = ctx;


  cmcf = ctx->main_conf[ngx_http_core_module.ctx_index];

  cscfp = ngx_array_push(&cmcf->servers);
  if (cscfp == NULL) {
    return NGX_CONF_ERROR;
  }

  *cscfp = cscf;


  /* parse inside server{} */

  pcf = *cf;
  cf->ctx = ctx;
  cf->cmd_type = NGX_HTTP_SRV_CONF;

 // 解析 server 塊配置
  rv = ngx_conf_parse(cf, NULL);

  *cf = pcf;

 // 如果沒有監(jiān)聽任何端口,則監(jiān)聽默認(rèn)的 80 或 8000端口
  if (rv == NGX_CONF_OK && !cscf->listen) {
    ngx_memzero(&lsopt, sizeof(ngx_http_listen_opt_t));

    sin = &lsopt.u.sockaddr_in;

    sin->sin_family = AF_INET;
#if (NGX_WIN32)
    sin->sin_port = htons(80);
#else
    sin->sin_port = htons((getuid() == 0) ? 80 : 8000);
#endif
    sin->sin_addr.s_addr = INADDR_ANY;

    lsopt.socklen = sizeof(struct sockaddr_in);

    lsopt.backlog = NGX_LISTEN_BACKLOG;
    lsopt.rcvbuf = -1;
    lsopt.sndbuf = -1;
#if (NGX_HAVE_SETFIB)
    lsopt.setfib = -1;
#endif
#if (NGX_HAVE_TCP_FASTOPEN)
    lsopt.fastopen = -1;
#endif
    lsopt.wildcard = 1;

    (void) ngx_sock_ntop(&lsopt.u.sockaddr, lsopt.socklen, lsopt.addr,
               NGX_SOCKADDR_STRLEN, 1);

    if (ngx_http_add_listen(cf, cscf, &lsopt) != NGX_OK) {
      return NGX_CONF_ERROR;
    }
  }

  return rv;
} // }}}

location 塊解析 -- ngx_http_core_location

// static char *
// ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
// location 塊配置解析 {{{
static char *
ngx_http_core_location(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
{
  char           *rv;
  u_char          *mod;
  size_t           len;
  ngx_str_t         *value, *name;
  ngx_uint_t         i;
  ngx_conf_t         save;
  ngx_http_module_t     *module;
  ngx_http_conf_ctx_t    *ctx, *pctx;
  ngx_http_core_loc_conf_t *clcf, *pclcf;

  ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_conf_ctx_t));
  if (ctx == NULL) {
    return NGX_CONF_ERROR;
  }

  pctx = cf->ctx;
  ctx->main_conf = pctx->main_conf;
  ctx->srv_conf = pctx->srv_conf;

  ctx->loc_conf = ngx_pcalloc(cf->pool, sizeof(void *) * ngx_http_max_module);
  if (ctx->loc_conf == NULL) {
    return NGX_CONF_ERROR;
  }

  for (i = 0; ngx_modules[i]; i++) {
    if (ngx_modules[i]->type != NGX_HTTP_MODULE) {
      continue;
    }

    module = ngx_modules[i]->ctx;

    if (module->create_loc_conf) {
      ctx->loc_conf[ngx_modules[i]->ctx_index] =
                          module->create_loc_conf(cf);
      if (ctx->loc_conf[ngx_modules[i]->ctx_index] == NULL) {
         return NGX_CONF_ERROR;
      }
    }
  }

  clcf = ctx->loc_conf[ngx_http_core_module.ctx_index];
  clcf->loc_conf = ctx->loc_conf;

  value = cf->args->elts;

  if (cf->args->nelts == 3) {

    len = value[1].len;
    mod = value[1].data;
    name = &value[2];

    if (len == 1 && mod[0] == '=') {

      clcf->name = *name;
      clcf->exact_match = 1;

    } else if (len == 2 && mod[0] == '^' && mod[1] == '~') {

      clcf->name = *name;
      clcf->noregex = 1;

    } else if (len == 1 && mod[0] == '~') {

      if (ngx_http_core_regex_location(cf, clcf, name, 0) != NGX_OK) {
        return NGX_CONF_ERROR;
      }

    } else if (len == 2 && mod[0] == '~' && mod[1] == '*') {

      if (ngx_http_core_regex_location(cf, clcf, name, 1) != NGX_OK) {
        return NGX_CONF_ERROR;
      }

    } else {
      ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                "invalid location modifier \"%V\"", &value[1]);
      return NGX_CONF_ERROR;
    }

  } else {

    name = &value[1];

    if (name->data[0] == '=') {

      clcf->name.len = name->len - 1;
      clcf->name.data = name->data + 1;
      clcf->exact_match = 1;

    } else if (name->data[0] == '^' && name->data[1] == '~') {

      clcf->name.len = name->len - 2;
      clcf->name.data = name->data + 2;
      clcf->noregex = 1;

    } else if (name->data[0] == '~') {

      name->len--;
      name->data++;

      if (name->data[0] == '*') {

        name->len--;
        name->data++;

        if (ngx_http_core_regex_location(cf, clcf, name, 1) != NGX_OK) {
          return NGX_CONF_ERROR;
        }

      } else {
        if (ngx_http_core_regex_location(cf, clcf, name, 0) != NGX_OK) {
          return NGX_CONF_ERROR;
        }
      }

    } else {

      clcf->name = *name;

      if (name->data[0] == '@') {
        clcf->named = 1;
      }
    }
  }

  pclcf = pctx->loc_conf[ngx_http_core_module.ctx_index];

  if (pclcf->name.len) {

    /* nested location */

#if 0
    clcf->prev_location = pclcf;
#endif

    if (pclcf->exact_match) {
      ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                "location \"%V\" cannot be inside "
                "the exact location \"%V\"",
                &clcf->name, &pclcf->name);
      return NGX_CONF_ERROR;
    }

    if (pclcf->named) {
      ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                "location \"%V\" cannot be inside "
                "the named location \"%V\"",
                &clcf->name, &pclcf->name);
      return NGX_CONF_ERROR;
    }

    if (clcf->named) {
      ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                "named location \"%V\" can be "
                "on the server level only",
                &clcf->name);
      return NGX_CONF_ERROR;
    }

    len = pclcf->name.len;

#if (NGX_PCRE)
    if (clcf->regex == NULL
      && ngx_filename_cmp(clcf->name.data, pclcf->name.data, len) != 0)
#else
    if (ngx_filename_cmp(clcf->name.data, pclcf->name.data, len) != 0)
#endif
    {
      ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                "location \"%V\" is outside location \"%V\"",
                &clcf->name, &pclcf->name);
      return NGX_CONF_ERROR;
    }
  }

 // 將 location 配置加入到 locations 配置鏈表中
  if (ngx_http_add_location(cf, &pclcf->locations, clcf) != NGX_OK) {
    return NGX_CONF_ERROR;
  }

  save = *cf;
  cf->ctx = ctx;
  cf->cmd_type = NGX_HTTP_LOC_CONF;

  rv = ngx_conf_parse(cf, NULL);

  *cf = save;

  return rv;
} // }}}

與 server 塊解析函數(shù) ngx_http_core_server 類似,他創(chuàng)建了所有模塊的 loc_conf,為了防止內(nèi)外層具有相同指令,在配置賦值完成后,會通過 merge 函數(shù)合并到一起。

然而,與 server 塊不同,location 塊在 location 后面會通過路徑或正則表達(dá)式指定 location 配置的應(yīng)用 uri,因此,在 ngx_http_core_location 函數(shù)中調(diào)用 PCRE 進(jìn)行了 location 命令的解析。

解析完成后調(diào)用 ngx_http_add_location 將解析結(jié)果加入到 locations 鏈表中。

// ngx_int_t ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
//   ngx_http_core_loc_conf_t *clcf)
// 將 location 配置加入到 locations 配置鏈表中 {{{
ngx_int_t
ngx_http_add_location(ngx_conf_t *cf, ngx_queue_t **locations,
  ngx_http_core_loc_conf_t *clcf)
{
  ngx_http_location_queue_t *lq;

  if (*locations == NULL) {
    *locations = ngx_palloc(cf->temp_pool,
                sizeof(ngx_http_location_queue_t));
    if (*locations == NULL) {
      return NGX_ERROR;
    }

    ngx_queue_init(*locations);
  }

  lq = ngx_palloc(cf->temp_pool, sizeof(ngx_http_location_queue_t));
  if (lq == NULL) {
    return NGX_ERROR;
  }

  if (clcf->exact_match
#if (NGX_PCRE)
    || clcf->regex
#endif
    || clcf->named || clcf->noname)
  {
    lq->exact = clcf;
    lq->inclusive = NULL;

  } else {
    lq->exact = NULL;
    lq->inclusive = clcf;
  }

  lq->name = &clcf->name;
  lq->file_name = cf->conf_file->file.name.data;
  lq->line = cf->conf_file->line;

  ngx_queue_init(&lq->list);

  ngx_queue_insert_tail(*locations, &lq->queue);

  return NGX_OK;
} // }}}

配置解析全部完成后的配置結(jié)構(gòu)。

相關(guān)文章

  • nginx try_files指令判斷文件是否存在實(shí)例

    nginx try_files指令判斷文件是否存在實(shí)例

    這篇文章主要介紹了nginx try_files指令判斷文件是否存在實(shí)例,本文用一個(gè)實(shí)際需求講解try_files指令的使用,并給出配置示例,需要的朋友可以參考下
    2015-02-02
  • linux查找當(dāng)前系統(tǒng)nginx路徑的兩種方法

    linux查找當(dāng)前系統(tǒng)nginx路徑的兩種方法

    工作中有很多服務(wù)器, 它們上面裝的 nginx 的路徑也太不相當(dāng), 當(dāng)我們拿到一個(gè)不熟悉的服務(wù)器時(shí), 我們怎么知道, 當(dāng)前運(yùn)行的nginx的目錄是哪一個(gè)呢,本文小編給大家介紹了兩種linux查找當(dāng)前系統(tǒng)nginx的路徑的方法,需要的朋友可以參考下
    2023-11-11
  • CentOS 6.7下nginx SSL證書部署的方法

    CentOS 6.7下nginx SSL證書部署的方法

    這篇文章主要介紹了在CentOS 6.7下nginx SSL證書部署的方法,文中介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-03-03
  • Debian系統(tǒng)下為PHP程序配置Nginx服務(wù)器的基本教程

    Debian系統(tǒng)下為PHP程序配置Nginx服務(wù)器的基本教程

    這篇文章主要介紹了Debian系統(tǒng)下為PHP程序配置Nginx服務(wù)器的基本教程,這里使用到了FastCGI和php-fpm,需要的朋友可以參考下
    2015-12-12
  • Nginx啟用Brotli算法壓縮的示例

    Nginx啟用Brotli算法壓縮的示例

    這篇文章主要介紹了Nginx啟用Brotli算法壓縮的示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • nginx+rsync+inotify實(shí)現(xiàn)負(fù)載均衡配置方法

    nginx+rsync+inotify實(shí)現(xiàn)負(fù)載均衡配置方法

    這篇文章主要介紹了nginx+rsync+inotify實(shí)現(xiàn)負(fù)載均衡配置方法,需要的朋友可以參考下
    2014-11-11
  • Nginx?配置?HTTPS的詳細(xì)過程

    Nginx?配置?HTTPS的詳細(xì)過程

    這篇文章主要介紹了Nginx?配置?HTTPS?完整過程,包括nginx?的?ssl?模塊安裝,ssl?證書部署,nginx.conf?配置,重啟?nginx無信息提示就成功啦,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • 使用Nginx、Nginx Plus抵御DDOS攻擊的方法

    使用Nginx、Nginx Plus抵御DDOS攻擊的方法

    DDOS 是一種通過大流量的請求對目標(biāo)進(jìn)行轟炸式訪問,導(dǎo)致提供服務(wù)的服務(wù)器資源耗盡進(jìn)而無法繼續(xù)提供服務(wù)的攻擊手段。這篇文章主要介紹了使用Nginx、Nginx Plus抵御DDOS攻擊的方法,感興趣的小伙伴們可以參考一下
    2018-06-06
  • nginx代理服務(wù)器配置雙向證書驗(yàn)證的方法

    nginx代理服務(wù)器配置雙向證書驗(yàn)證的方法

    今天小編就為大家分享一篇關(guān)于nginx代理服務(wù)器配置雙向證書驗(yàn)證的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • nginx 504 Gateway Time-out錯(cuò)誤解決方法

    nginx 504 Gateway Time-out錯(cuò)誤解決方法

    我們經(jīng)常會發(fā)現(xiàn)大量的nginx服務(wù)器訪問時(shí)會提示nginx 504 Gateway Time-out錯(cuò)誤了,下面我來總結(jié)了一些解決辦法,有需要了解的同學(xué)可進(jìn)入?yún)⒖?/div> 2014-11-11

最新評論