staticngx_int_tngx_http_set_virtual_server(ngx_http_request_t*r,ngx_str_t*host){ngx_int_trc;ngx_http_connection_t*hc;ngx_http_core_loc_conf_t*clcf;ngx_http_core_srv_conf_t*cscf;#if(NGX_SUPPRESS_WARN)cscf=NULL;#endifhc=r->http_connection;#if(NGX_HTTP_SSL&&defined SSL_CTRL_SET_TLSEXT_HOSTNAME)if(hc->ssl_servername){if(hc->ssl_servername->len==host->len&&ngx_strncmp(hc->ssl_servername->data,host->data,host->len)==0){#if(NGX_PCRE)if(hc->ssl_servername_regex&&ngx_http_regex_exec(r,hc->ssl_servername_regex,hc->ssl_servername)!=NGX_OK){ngx_http_close_request(r,NGX_HTTP_INTERNAL_SERVER_ERROR);returnNGX_ERROR;}#endifreturnNGX_OK;}}#endifrc=ngx_http_find_virtual_server(r->connection,hc->addr_conf->virtual_names,host,r,&cscf);if(rc==NGX_ERROR){ngx_http_close_request(r,NGX_HTTP_INTERNAL_SERVER_ERROR);returnNGX_ERROR;}#if(NGX_HTTP_SSL&&defined SSL_CTRL_SET_TLSEXT_HOSTNAME)if(hc->ssl_servername){ngx_http_ssl_srv_conf_t*sscf;if(rc==NGX_DECLINED){cscf=hc->addr_conf->default_server;rc=NGX_OK;}sscf=ngx_http_get_module_srv_conf(cscf->ctx,ngx_http_ssl_module);if(sscf->verify){ngx_log_error(NGX_LOG_INFO,r->connection->log,0,"client attempted to request the server name ""different from the one that was negotiated");ngx_http_finalize_request(r,NGX_HTTP_MISDIRECTED_REQUEST);returnNGX_ERROR;}}#endifif(rc==NGX_DECLINED){returnNGX_OK;}r->srv_conf=cscf->ctx->srv_conf;r->loc_conf=cscf->ctx->loc_conf;clcf=ngx_http_get_module_loc_conf(r,ngx_http_core_module);ngx_set_connection_log(r->connection,clcf->error_log);returnNGX_OK;}
#if(NGX_HTTP_SSL&&defined SSL_CTRL_SET_TLSEXT_HOSTNAME)if(hc->ssl_servername){ngx_http_ssl_srv_conf_t*sscf;if(rc==NGX_DECLINED){cscf=hc->addr_conf->default_server;rc=NGX_OK;}sscf=ngx_http_get_module_srv_conf(cscf->ctx,ngx_http_ssl_module);if(sscf->verify){ngx_log_error(NGX_LOG_INFO,r->connection->log,0,"client attempted to request the server name ""different from the one that was negotiated");ngx_http_finalize_request(r,NGX_HTTP_MISDIRECTED_REQUEST);returnNGX_ERROR;}}#endif
SSL 场景下匹配失败的补救及主机名验证
6 未匹配
if(rc==NGX_DECLINED){returnNGX_OK;}
未找到匹配 返回成功 意味着请求将继续使用之前已设置的 server 配置 (通常是在收到请求前根据 IP/端口选定的默认 server)