Nginx location 配对次序梳理

configure arguments: user=nginx group=nginx prefix=/application/nginx1.6.2 with-http_stub_status_module with-http_ssl_module


location 命令的功效是依据客户恳求的URI来实行不一样的运用,URI便是依据客户恳求到的网站地址URL开展配对,配对取得成功了开展有关的实际操作。


A location can either be defined by a prefix string, or by a regular expression.


Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching)


一个location能够用prefix string(作为前缀标识符串)界定,还可以根据regular expression(正则表达式表述式而定义)


通俗化的说也便是:大家能够根据应用不一样的作为前缀,表述不一样的含意,针对不一样的作为前缀能够分成几大类:一般location和正则表达式location


留意:假如你的uri用正则表达式,则你的正则表达式前边务必加上~或是~*,以前我还在这儿存有错误观念,认为能够不用~或是~*


To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.


Nginx网络服务器会最先会查验好几个location中是不是有一般的uri配对,假如有好几个配对,会先记牢配对度最大的哪个。随后再查验正则表达式配对,这儿谨记正则表达式配对是有次序的,从上到下先后配对,一旦配对取得成功,则完毕查验,并便会应用这一location块解决此恳求。假如正则表达式配对所有不成功,便会应用刚刚纪录一般uri配对度最大的哪个location块解决此恳求。


If the longest matching prefix location has the “^~” modifier then regular expressions are not checked.


Also, using the “=” modifier it is possible to define an exact match of URI and location. If an exact match is found, the search terminates. For example, if a “/” request happens frequently, defining “location = /” will speed up the processing of these requests, as search terminates right after parison. Such a location cannot obviously contain nested locations.


应用标记“=”装饰符能够界定一个精准配对的URI和部位,假如寻找了一个精准的配对,则检索停止,比如,假如一个”/”恳求经常产生,界定“location =/”将加速这种恳求的解决,一旦精准配对仅有就完毕,那样的location显而易见不可以包括嵌套循环location


“location / {}”是一般的较大作为前缀配对,一切的uri毫无疑问是以“/”开始,因此location / {} 能够说成默认设置配对,当别的也不配对了,则配对默认设置配对


a. ”=”用以一般uri前,规定精准配对,假如配对取得成功,则终止检索并且用当今location解决此恳求


d. ”^~”表明在一般uri前应求Nginx网络服务器寻找一般uri配对度最大的哪个location后,马上解决此恳求,其实不再开展正则表达式配对


e. ”^~”和“=”都可以以阻拦再次配对正则表达式location二者的差别:“^~”仍然遵循较大作为前缀标准,随后“=”是必须严苛配对


先配对一般uri,随后记牢配对度最大的哪个(官方网站原话:To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered.)随后配对正则表达式,假如正则表达式配对则完毕搜索,假如正则表达式不配对,则配对以前一般配对中配对度最大的哪个将实行该恳求(官方网站原话:Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.)


因此:location 的配对次序是“先配对正则表达式,再配对一般” 这句话话毫无疑问是不正确的,更何况这儿并沒有包括”^~”和“=”


假如是一般uri 配对,这一情况下是沒有次序的,可是正则表达式配对则是有次序的,是以上到下先后配对,一旦有配对取得成功,则终止后边的配对。


nginx: [emerg] invalid location modifier ~*\.(gif|jpg|jpeg) in /application/nginx1.6.2/conf/extra/conf:19


nginx: [emerg] directive location has no opening { in /application/nginx1.6.2/conf/extra/conf:23


这儿根据配备把试验三和试验四的比照便可以看得出来由于一般配对里有“^~”,而且配对来到images,因此这一便是不开展正则表达式配对


没有过量的做测验,依据上边的逻辑性图应当能够处理遇到的难题了全部的勤奋都非常值得期许,每一份理想都应当浇灌!