首先我来大概的介绍一下location的种类和匹配规则,以nginxwiki的例子做说明:
#matchesanyquery,sinceallqueriesbeginwith/,butregular
#expressionsandanylongerconventionalblockswillbe
#matchesanyquerybeginningwith/images/andhaltssearching,
#soregularexpressionswillnotbechecked.
location~*\.(gif|jpg|jpeg)${
#matchesanyrequestendingingif,jpg,orjpeg.however,all
#requeststothe/images/directorywillbehandledby
#suchlocationsarenotusedduringnormalprocessingofrequests,
#theyareintendedonlytoprocessinternallyredirectedrequests(forexampleerror_page,try_files).
}
ngx_http_location_tree_node_t*static_locations;
ngx_http_core_loc_conf_t**regex_locations;
if
从这2个字段的类型可以看出,字符串匹配的location被组织成了一个locationtree,而正则匹配的location只是一个数组,
locationtree和regex_locations数组建立过程在ngx_http_block中:
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){
if(ngx_http_init_static_location_trees(cf,clcf)!=ngx_ok){
}
staticngx_int_t
ngx_http_init_locations(ngx_conf_t*cf,ngx_http_core_srv_conf_t*cscf,
ngx_http_core_loc_conf_t*pclcf)
locations=pclcf->locations;
ngx_queue_sort(locations,ngx_http_cmp_locations);
for(q=ngx_queue_head(locations);
q!=ngx_queue_sentinel(locations);
lq=(ngx_http_location_queue_t*)q;
clcf=lq->exact?lq->exact:lq->inclusive;
if(ngx_http_init_locations(cf,null,clcf)!=ngx_ok){
if(q!=ngx_queue_sentinel(locations)){
ngx_queue_split(locations,q,&tail);
clcfp=ngx_palloc(cf->pool,
(n+1)*sizeof(ngx_http_core_loc_conf_t**));
cscf->named_locations=clcfp;
q!=ngx_queue_sentinel(locations);
lq=(ngx_http_location_queue_t*)q;
ngx_queue_split(locations,named,&tail);
clcfp=ngx_palloc(cf->pool,
(r+1)*sizeof(ngx_http_core_loc_conf_t**));
pclcf->regex_locations=clcfp;
q!=ngx_queue_sentinel(locations);
lq=(ngx_http_location_queue_t*)q;
ngx_queue_split(locations,regex,&tail);
}
staticngx_int_t
ngx_http_init_static_location_trees(ngx_conf_t*cf,
ngx_http_core_loc_conf_t*pclcf)
ngx_queue_t*q,*locations;
ngx_http_core_loc_conf_t*clcf;
ngx_http_location_queue_t*lq;
locations=pclcf->locations;
if(ngx_queue_empty(locations)){
for(q=ngx_queue_head(locations);
q!=ngx_queue_sentinel(locations);
lq=(ngx_http_location_queue_t*)q;
clcf=lq->exact?lq->exact:lq->inclusive;
if(ngx_http_init_static_location_trees(cf,clcf)!=ngx_ok){

ngx_http_create_locations_list(locations,ngx_queue_head(locations));
pclcf->static_locations=ngx_http_create_locations_tree(cf,locations,0);
if(pclcf->static_locations==null){
}
staticngx_http_location_tree_node_t*
ngx_http_create_locations_tree(ngx_conf_t*cf,ngx_queue_t*locations,
q=ngx_queue_middle(locations);
lq=(ngx_http_location_queue_t*)q;
len=lq->name->len-prefix;
node=ngx_palloc(cf->pool,
offsetof(ngx_http_location_tree_node_t,name)+len);
node->exact=lq->exact;
node->inclusive=lq->inclusive;
node->auto_redirect=(u_char)((lq->exact&&lq->exact->auto_redirect)
||(lq->inclusive&&lq->inclusive->auto_redirect));
node->len=(u_char)len;
ngx_memcpy(node->name,&lq->name->data[prefix],len);
ngx_queue_split(locations,q,&tail);
if(ngx_queue_empty(locations)){
node->left=ngx_http_create_locations_tree(cf,locations,prefix);
if(ngx_queue_empty(&tail)){
node->right=ngx_http_create_locations_tree(cf,&tail,prefix);
if(node->right==null){
if(ngx_queue_empty(&lq->list)){
node->tree=ngx_http_create_locations_tree(cf,&lq->list,prefix+len);
locationtree节点的ngx_http_location_tree_node_s结构:
structngx_http_location_tree_node_s{
ngx_http_location_tree_node_t*left;
ngx_http_location_tree_node_t*right;
ngx_http_location_tree_node_t*tree;
ngx_http_core_loc_conf_t*exact;
ngx_http_core_loc_conf_t*inclusive;
};
最终建立的locationtree如下(为了方便阅读,图中列出了tree节点的完整名字):
ps:关于locationmodifier1.=这会完全匹配指定的pattern,且这里的pattern被限制成简单的字符串,也就是说这里不能使用正则表达式。
http://jb51.net/abcd#正好完全匹配
http://jb51.net/abcd#如果运行nginxserver的系统本身对大小写不敏感,比如windows,那么也匹配
http://jb51.net/abcd?param1¶m2#忽略查询串参数(querystringarguments),这里就是/abcd后面的?param1¶m2
http://jb51.net/abcd/#不匹配,因为末尾存在反斜杠(trailingslash),nginx不认为这种情况是完全匹配
http://jb51.net/abcde#不匹配,因为不是完全匹配
2.(none)可以不写locationmodifier,nginx仍然能去匹配pattern。这种情况下,匹配那些以指定的patern开头的uri,注意这里的uri只能是普通字符串,不能使用正则表达式。
http://jb51.net/abcd#正好完全匹配
http://jb51.net/abcd#如果运行nginxserver的系统本身对大小写不敏感,比如windows,那么也匹配
http://jb51.net/abcd?param1¶m2#忽略查询串参数(querystringarguments),这里就是/abcd后面的?param1¶m2
http://jb51.net/abcd/#末尾存在反斜杠(trailingslash)也属于匹配范围内
http://jb51.net/abcde#仍然匹配,因为uri是以pattern开头的
3.~这个locationmodifier对大小写敏感,且pattern须是正则表达式
http://jb51.net/abcd#完全匹配
http://jb51.net/abcd#不匹配,~对大小写是敏感的
http://jb51.net/abcd?param1¶m2#忽略查询串参数(querystringarguments),这里就是/abcd后面的?param1¶m2
http://jb51.net/abcd/#不匹配,因为末尾存在反斜杠(trailingslash),并不匹配正则表达式^/abcd$
http://jb51.net/abcde#不匹配正则表达式^/abcd$
注意:对于一些对大小写不敏感的系统,比如windows,~和~*都是不起作用的,这主要是操作系统的原因。
4.~*与~类似,但这个locationmodifier不区分大小写,pattern须是正则表达式
http://jb51.net/abcd#完全匹配
http://jb51.net/abcd#匹配,这就是它不区分大小写的特性
http://jb51.net/abcd?param1¶m2#忽略查询串参数(querystringarguments),这里就是/abcd后面的?param1¶m2
http://jb51.net/abcd/#不匹配,因为末尾存在反斜杠(trailingslash),并不匹配正则表达式^/abcd$
http://jb51.net/abcde#不匹配正则表达式^/abcd$
5.^~匹配情况类似2.(none)的情况,以指定匹配模式开头的uri被匹配,不同的是,一旦匹配成功,那么nginx就停止去寻找其他的location块进行匹配了(与location匹配顺序有关)
6.@用于定义一个location块,且该块不能被外部client所访问,只能被nginx内部配置指令所访问,比如try_filesorerror_page
以上就是Nginx服务器中location配置实例分析的详细内容,更多请关注主机测评网其它相关文章!
本文来源:国外服务器--Nginx服务器中location配置实例分析(nginx的location配置详解)
本文地址:https://www.idcbaba.com/guowai/6194.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 1919100645@qq.com 举报,一经查实,本站将立刻删除。




