在配置nginx.conf文件的時候,我們很容易發現,有部分配置項是既可以配置在http塊,也可以配置在server塊,還可以配置在location塊中。但是並不是所有的配置項都可以在任意位置進行配置的,根據配置項所起到的作用,nginx對各個配置塊所能使用的位置進行了定義。既然一個配置項可以配置在多個配置塊中,那麼這裏就涉及到一個問題就是,在處理請求的時候是以哪一個配置項爲準。本文主要講解nginx是如何實現配置項的合併的。
在前面的文章中,我們講解了nginx http模塊的基本存儲結構,在閱讀本文之前強烈建議讀者朋友先閱讀這篇文章。如下是nginx http模塊的存儲結構示意圖:
這裏我們不再贅述nginx是如何解析各個配置項,從而形成這樣的一個存儲結構的。nginx對配置項的合併主要是通過ngx_http_merge_servers()
方法進行的,如下是該方法的源碼:
/**
* @param cf 整個nginx運行的ngx_conf_t對象
* @param cmcf 核心模塊對應的配置對象
* @param module 外層遍歷時,當前遍歷的模塊
* @param ctx_index 外層遍歷時,當前遍歷的模塊的配置對應的存儲位置
*/
static char * ngx_http_merge_servers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf,
ngx_http_module_t *module, ngx_uint_t ctx_index) {
char *rv;
ngx_uint_t s;
ngx_http_conf_ctx_t *ctx, saved;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t **cscfp;
// 這裏獲取所有server配置塊對應的ngx_http_core_srv_conf_t結構體,每個結構體都對應了配置文件中
// 一個server塊的配置
cscfp = cmcf->servers.elts;
// 這裏的cf->ctx就是解析http配置塊得到的ngx_http_conf_ctx_t結構體,需要注意的是,其中只有main_conf
// 對應的數組數據有意義,因爲http塊的數據只存儲在main_conf中
ctx = (ngx_http_conf_ctx_t *) cf->ctx;
// 這裏需要注意的是,在執行saved=*ctx;之後,會將當前*ctx的結構體對象完全複製而得到一個新的,從而存儲到
// saved中,也就是說ctx和&saved的值是不一樣的。在下面的for循環中會執行
// ctx->srv_conf = cscfp[s]->ctx->srv_conf;語句,這裏其實改變
// 的是ctx指針所指向的結構體對象的srv_conf屬性的值,而並沒有影響到saved->srv_conf屬性值,
// 因爲saved和*ctx是兩個各個屬性值雖然相同,但是本身並不同的結構體對象。
saved = *ctx;
rv = NGX_CONF_OK;
for (s = 0; s < cmcf->servers.nelts; s++) {
/* merge the server{}s' srv_conf's */
ctx->srv_conf = cscfp[s]->ctx->srv_conf;
if (module->merge_srv_conf) {
// 這裏主要是進行配置的合併,saved.srv_conf[ctx_index]指向的是http塊中解析出的SRV類型的配置,
// cscfp[s]->ctx->srv_conf[ctx_index]解析的則是當前server塊中的配置,合併的一般規則是通過對應
// 模塊實現的merge_srv_conf()來進行的,具體將會使用http塊中的配置,還是使用server塊中的配置,
// 或者說合並兩者,則是由具體的實現來決定的。這裏合併的效果本質上就是對
// cscfp[s]->ctx->srv_conf[ctx_index]中的屬性賦值,如果該值不存在,則將http塊對應的值寫到
// 該屬性中
rv = module->merge_srv_conf(cf, saved.srv_conf[ctx_index],
cscfp[s]->ctx->srv_conf[ctx_index]);
if (rv != NGX_CONF_OK) {
goto failed;
}
}
if (module->merge_loc_conf) {
ctx->loc_conf = cscfp[s]->ctx->loc_conf;
// 需要注意的是,這裏合併的主要是http塊和當前server塊中對應的LOC類型的配置數據,此時並沒有開始
// 合併server塊下的各個location配置
rv = module->merge_loc_conf(cf, saved.loc_conf[ctx_index],
cscfp[s]->ctx->loc_conf[ctx_index]);
if (rv != NGX_CONF_OK) {
goto failed;
}
clcf = cscfp[s]->ctx->loc_conf[ngx_http_core_module.ctx_index];
// 這裏clcf->locations存儲了當前server塊下的所有location塊的配置,而cscfp[s]->ctx->loc_conf
// 則指向的是將http塊和server塊合併之後的配置數據,從這裏就可以看出,當前方法的結果就是合併了
// http塊、server塊和location塊的配置
rv = ngx_http_merge_locations(cf, clcf->locations, cscfp[s]->ctx->loc_conf,
module, ctx_index);
if (rv != NGX_CONF_OK) {
goto failed;
}
}
}
failed:
*ctx = saved;
return rv;
}
這裏ngx_http_merge_servers()
方法主要完成了如下幾個工作:
- 調用當前模塊的
merge_srv_conf()
方法將http配置塊和server配置塊中NGX_HTTP_SRV_CONF_OFFSET
類型的配置項進行合併,也即將http配置塊結構體中的srv_conf
數組與server配置塊結構體中的srv_conf
數組進行合併,如此就完成了NGX_HTTP_SRV_CONF_OFFSET
類型配置項的合併; - 調用當前模塊的
merge_loc_conf()
方法將http配置塊和server配置塊中NGX_HTTP_LOC_CONF_OFFSET
類型的配置項進行合併,也即將http配置塊結構體中的loc_conf
數組與server配置塊結構體中的loc_conf
數組進行合併。不過不同於NGX_HTTP_SRV_CONF_OFFSET
類型的配置項,NGX_HTTP_LOC_CONF_OFFSET
類型的配置項是還可以配置在location配置塊中的,因而還需要將合併的結果與location配置塊中的配置項進行合併; - 調用
ngx_http_merge_locations()
方法將前一步http配置塊與server配置塊合併的NGX_HTTP_LOC_CONF_OFFSET
類型的配置項繼續與location配置塊中的配置項進行合併,從而得到最終合併結果。不過這裏需要注意的一點是,location配置塊中還可以繼續配置配置子location配置塊,如此不斷往復下去,而這裏的ngx_http_merge_locations()
方法則可以用於遞歸調用,以便於進行子location的合併。
這裏我們繼續看ngx_http_merge_locations()
方法是如何合併子location的:
/**
* 合併location塊的配置
*
* @param cf 指向當前http塊的配置結構體ngx_http_conf_ctx_t
* @param locations 當前server塊下所有的location塊的配置
* @param loc_conf
* @param module
* @param ctx_index
* @return
*/
static char * ngx_http_merge_locations(ngx_conf_t *cf, ngx_queue_t *locations, void **loc_conf,
ngx_http_module_t *module, ngx_uint_t ctx_index) {
char *rv;
ngx_queue_t *q;
ngx_http_conf_ctx_t *ctx, saved;
ngx_http_core_loc_conf_t *clcf;
ngx_http_location_queue_t *lq;
if (locations == NULL) {
return NGX_CONF_OK;
}
ctx = (ngx_http_conf_ctx_t *) cf->ctx;
saved = *ctx;
for (q = ngx_queue_head(locations);
q != ngx_queue_sentinel(locations);
q = ngx_queue_next(q)) {
lq = (ngx_http_location_queue_t *) q;
clcf = lq->exact ? lq->exact : lq->inclusive;
ctx->loc_conf = clcf->loc_conf;
// 合併location塊的配置,這裏的loc_conf[ctx_index]存儲的是http塊與server塊配置合併的數據,
// 而clcf->loc_conf則是當前server塊下的某個location塊的數據,這裏其實就是將當前loc_conf中的
// 數據合併到clcf->loc_conf中
rv = module->merge_loc_conf(cf, loc_conf[ctx_index], clcf->loc_conf[ctx_index]);
if (rv != NGX_CONF_OK) {
return rv;
}
// 由於location塊中還可以配置子location塊,因而這裏會遞歸的進行合併
rv = ngx_http_merge_locations(cf, clcf->locations, clcf->loc_conf, module, ctx_index);
if (rv != NGX_CONF_OK) {
return rv;
}
}
*ctx = saved;
return NGX_CONF_OK;
}
這裏對location以及子location的合併過程其實比較簡單,就是依次彈出當前配置塊(server或者父location)下的各個子location,然後調用當前模塊的merge_loc_conf()
方法將當前配置塊的配置與彈出的子location進行合併。在合併完成後,繼續對該子location進行遞歸的合併,也即將該子location與其子location進行合併。如此不斷往復下去,直到所有的location都被合併完成。
nginx的配置項合併是一個比較重要的功能,比如某個既可以在http塊中也可以在server塊中配置的配置項,如果nginx.conf中僅僅只是配置了http塊中的配置,那麼所有的server塊的配置就都可以繼承http塊的配置,但如果其中某個server自身也進行了該配置項的配置,那麼進行合併的時候該配置項就可以直接使用其自身的配置,而不需要繼承http配置塊的配置,如此就可以實現非常靈活的功能。