Nginx怎么做域名解析?怎么在你自己开发的模块里面使用Nginx提供的方法解析域名?它内部实现是什么样的?
本文以Nginx 1.5.1为例,从nginx_mail_smtp模块如何进行域名解析出发,分析Nginx进行域名解析的过程。为了简化流程,突出重点,在示例代码中省掉了一些异常部分的处理,比如内存分配失败等。
DNS查询分为两种:根据域名查询地址和根据地址查询域名,在代码结构上这两种方式非常相似,这里只介绍根据域名查询地址这一种方式。本文将从以下几个方面进行介绍:
一、域名查询的函数接口介绍
在使用同步IO的情况下,调用gethostbyname()或者gethostbyname_r()就可以根据域名查询到对应的IP地址, 但因为可能会通过网络进行远程查询,所以需要的时间比较长。
为了不阻塞当前线程,Nginx采用了异步的方式进行域名查询。整个查询过程主要分为三个步骤,这点在各种异步处理时都是一样的:
另外,为了尽量减少查询花费的时间,Nginx还对查询结果做了本地缓存。为了初始化DNS Server地址和本地缓存等信息,需要在真正查询前需要先进行一些全局的初始化操作。
下面先从调用者的角度对每个步骤做详细的分析:
#nginx.conf resolver 8.8.8.8 #nginx 默认会根据DNS请求结果里的TTL值来进行缓存, #当然也可以通过一个可选的参数valid来设置过期时间,如: #resolver 127.0.0.1 [::1]:5353 valid=30s;
static char * ngx_mail_core_resolver(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { ngx_mail_core_srv_conf_t *cscf = conf; ngx_str_t *value; value = cf->args->elts; cscf->resolver = ngx_resolver_create(cf, &value[1], cf->args->nelts - 1); return NGX_CONF_OK; }
static void ngx_mail_smtp_resolve_name(ngx_event_t *rev) { ngx_connection_t *c; ngx_mail_session_t *s; ngx_resolver_ctx_t *ctx; ngx_mail_core_srv_conf_t *cscf; c = rev->data; s = c->data; cscf = ngx_mail_get_module_srv_conf(s, ngx_mail_core_module); ctx = ngx_resolve_start(cscf->resolver, NULL); if (ctx == NULL) { ngx_mail_close_connection(c); return; } ctx->name = s->host; ctx->type = NGX_RESOLVE_A; ctx->handler = ngx_mail_smtp_resolve_name_handler; ctx->data = s; ctx->timeout = cscf->resolver_timeout; //根据名字进行IP地址查询 if (ngx_resolve_name(ctx) != NGX_OK) { ngx_mail_close_connection(c); } }
static void ngx_mail_smtp_resolve_name_handler(ngx_resolver_ctx_t *ctx) { in_addr_t addr; ngx_uint_t i; ngx_connection_t *c; struct sockaddr_in *sin; ngx_mail_session_t *s; s = ctx->data; c = s->connection; if (ctx->state) { ngx_log_error(NGX_LOG_ERR, c->log, 0, ""%V" could not be resolved (%i: %s)", &ctx->name, ctx->state, ngx_resolver_strerror(ctx->state)); } else { /* AF_INET only */ sin = (struct sockaddr_in *) c->sockaddr; for (i = 0; i < ctx->naddrs; i++) { addr = ctx->addrs[i]; ngx_log_debug4(NGX_LOG_DEBUG_MAIL, c->log, 0, "name was resolved to %ud.%ud.%ud.%ud", (ntohl(addr) >> 24) & 0xff, (ntohl(addr) >> 16) & 0xff, (ntohl(addr) >> 8) & 0xff, ntohl(addr) & 0xff); if (addr == sin->sin_addr.s_addr) { goto found; } } s->host = smtp_unavailable; } found: //不管成功失败都要执行 ngx_resolve_name_done(ctx); }
二、域名解析流程分析
通过Nginx进行域名查询的流程图如下,颜色越深花费的时间越长。调用过程分为三种:
三、查询场景分析及实现介绍
查询的地址是IP v4地址
比如74.125.128.100, nginx会在ngx_resolve_start中通过ngx_inet_addr方法进行判断,如果是IPv4的地址,就设置好标志位 ngx_resolver_ctx_t->quick,在接下来的ngx_resolve_name中会对这个标志位进行判断,如果为1,就直接调用ngx_resolver_ctx_t->handler
ngx_resolver_ctx_t * ngx_resolve_start(ngx_resolver_t *r, ngx_resolver_ctx_t *temp) { in_addr_t addr; ngx_resolver_ctx_t *ctx; if (temp) { addr = ngx_inet_addr(temp->name.data, temp->name.len); if (addr != INADDR_NONE) { temp->resolver = r; temp->state = NGX_OK; temp->naddrs = 1; temp->addrs = &temp->addr; temp->addr = addr; temp->quick = 1; return temp; } } ... }
static void ngx_resolver_timeout_handler(ngx_event_t *ev) { ngx_resolver_ctx_t *ctx; ctx = ev->data; ctx->state = NGX_RESOLVE_TIMEDOUT; ctx->handler(ctx); }
static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx) { ngx_resolver_node_t *rn; rn = ngx_resolver_alloc(r, sizeof(ngx_resolver_node_t)); ngx_rbtree_insert(&r->name_rbtree, &rn->node); ngx_resolver_create_name_query(rn, ctx); ngx_resolver_send_query(r, rn); rn->cnlen = 0; rn->naddrs = 0; rn->valid = 0; rn->waiting = ctx; ctx->state = NGX_AGAIN; } //收到DNS查询结果后的回调方法 static void ngx_resolver_read_response(ngx_event_t *rev) { ssize_t n; ngx_connection_t *c; u_char buf[NGX_RESOLVER_UDP_SIZE]; c = rev->data; do { n = ngx_udp_recv(c, buf, NGX_RESOLVER_UDP_SIZE); if (n < 0) { return; } ngx_resolver_process_response(c->data, buf, n); } while (rev->ready); } static void ngx_resolver_process_a(ngx_resolver_t *r, u_char *buf, size_t last, ngx_uint_t ident, ngx_uint_t code, ngx_uint_t nan, ngx_uint_t ans) { hash = ngx_crc32_short(name.data, name.len); rn = ngx_resolver_lookup_name(r, &name, hash); //copy addresses to cached node rn->u.addrs = addrs; //回调所有等待本域名解析的请求 next = rn->waiting; rn->waiting = NULL; while (next) { ctx = next; ctx->state = NGX_OK; ctx->naddrs = naddrs; ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs; ctx->addr = addr; next = ctx->next; ctx->handler(ctx); } }
static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx) { uint32_t hash; in_addr_t addr, *addrs; ngx_uint_t naddrs; ngx_resolver_ctx_t *next; ngx_resolver_node_t *rn; hash = ngx_crc32_short(ctx->name.data, ctx->name.len); rn = ngx_resolver_lookup_name(r, &ctx->name, hash); if (rn) { if (rn->valid >= ngx_time()) { naddrs = rn->naddrs; if (naddrs) { ctx->next = rn->waiting; rn->waiting = NULL; do { ctx->state = NGX_OK; ctx->naddrs = naddrs; ctx->addrs = (naddrs == 1) ? &ctx->addr : addrs; ctx->addr = addr; next = ctx->next; ctx->handler(ctx); ctx = next; } while (ctx); return NGX_OK; } } } }
void ngx_process_events_and_timers(ngx_cycle_t *cycle) { ngx_uint_t flags; ngx_msec_t timer, delta; //处理各种网络事件 (void) ngx_process_events(cycle, timer, flags); //处理各种timer事件,其中包含了查询超时 ngx_event_expire_timers(); }
void ngx_close_xxx_session(ngx_xxx_session_t *s) { if(s->resolver_ctx != NULL) { s->resolver_ctx->state = NGX_RESOLVE_TIMEDOUT; ngx_resolve_name_done(s->resolver_ctx); s->resolver_ctx = NULL; } } void ngx_resolve_name_done(ngx_resolver_ctx_t *ctx) { uint32_t hash; ngx_resolver_t *r; ngx_resolver_ctx_t *w, **p; ngx_resolver_node_t *rn; r = ctx->resolver; if (ctx->state == NGX_AGAIN || ctx->state == NGX_RESOLVE_TIMEDOUT) { hash = ngx_crc32_short(ctx->name.data, ctx->name.len); rn = ngx_resolver_lookup_name(r, &ctx->name, hash); if (rn) { p = &rn->waiting; w = rn->waiting; while (w) { if (w == ctx) { *p = w->next; goto done; } p = &w->next; w = w->next; } } } done: ngx_resolver_free_locked(r, ctx); }
static void ngx_resolver_expire(ngx_resolver_t *r, ngx_rbtree_t *tree, ngx_queue_t *queue) { time_t now; ngx_uint_t i; ngx_queue_t *q; ngx_resolver_node_t *rn; now = ngx_time(); for (i = 0; i < 2; i++) { if (ngx_queue_empty(queue)) { return; } q = ngx_queue_last(queue); rn = ngx_queue_data(q, ngx_resolver_node_t, queue); if (now <= rn->expire) { return; } ngx_log_debug2(NGX_LOG_DEBUG_CORE, r->log, 0, "resolver expire "%*s"", (size_t) rn->nlen, rn->name); ngx_queue_remove(q); ngx_rbtree_delete(tree, &rn->node); ngx_resolver_free_node(r, rn); } }
static ngx_int_t ngx_resolve_name_locked(ngx_resolver_t *r, ngx_resolver_ctx_t *ctx) { if (naddrs) { if (naddrs != 1) { addr = 0; addrs = ngx_resolver_rotate(r, rn->u.addrs, naddrs); if (addrs == NULL) { return NGX_ERROR; } } else { addr = rn->u.addr; addrs = NULL; } } } static in_addr_t * ngx_resolver_rotate(ngx_resolver_t *r, in_addr_t *src, ngx_uint_t n) { void *dst, *p; ngx_uint_t j; dst = ngx_resolver_alloc(r, n * sizeof(in_addr_t)); j = ngx_random() % n; if (j == 0) { ngx_memcpy(dst, src, n * sizeof(in_addr_t)); return dst; } p = ngx_cpymem(dst, &src[j], (n - j) * sizeof(in_addr_t)); ngx_memcpy(p, src, j * sizeof(in_addr_t)); return dst; }
#nginx.conf resolver 8.8.8.8 8.8.4.4
static ngx_int_t ngx_resolver_send_query(ngx_resolver_t *r, ngx_resolver_node_t *rn) { ssize_t n; ngx_udp_connection_t *uc; uc = r->udp_connections.elts; uc = &uc[r->last_connection++]; if (r->last_connection == r->udp_connections.nelts) { r->last_connection = 0; } ...
end:如果你觉得本文对你有帮助的话,记得关注点赞转发,你的支持就是我更新动力。