原文链接:http://nginx.org/en/docs/http/ngx_http_core_module.html

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default:
Context: serverlocation
项目 说明 优先级
= 精确匹配 1
  模糊匹配 4
区分大小写 3
~* 不分区大小写 3
^~ 从开头部分匹配 2

Sets configuration depending on a request URI.

The matching is performed against a normalized URI, after decoding the text encoded in the “%XX” form, resolving references to relative path components “.” and “..”, and possible compression of two or more adjacent slashes into a single slash.

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). 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 blocks can be nested, with some exceptions mentioned below.

For case-insensitive operating systems such as macOS and Cygwin, matching with prefix strings ignores a case (0.7.7). However, comparison is limited to one-byte locales.

Regular expressions can contain captures (0.7.40) that can later be used in other directives.

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 the first comparison. Such a location cannot obviously contain nested locations.

In versions from 0.7.1 to 0.8.41, if a request matched the prefix location without the “=” and “^~” modifiers, the search also terminated and regular expressions were not checked.

Let’s illustrate the above by an example:

location = / {
[ configuration A ]
} location / {
[ configuration B ]
} location /documents/ {
[ configuration C ]
} location ^~ /images/ {
[ configuration D ]
} location ~* \.(gif|jpg|jpeg)$ {
[ configuration E ]
}

1.当URI是“/index.html”时,匹配B
2.当URI是“/documents/document.html”时,匹配C
3.当URI是“/images/1.gif”时,匹配D
4.当URI是“/documents/1.jpg”时,匹配E
5.当URI是“/”时,匹配A

The “/” request will match configuration A, the “/index.html” request will match configuration B, the “/documents/document.html” request will match configuration C, the “/images/1.gif” request will match configuration D, and the “/documents/1.jpg” request will match configuration E.

The “@” prefix defines a named location. Such a location is not used for a regular request processing, but instead used for request redirection. They cannot be nested, and cannot contain nested locations.

If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_passfastcgi_passuwsgi_passscgi_pass, or memcached_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:

location /user/ {
proxy_pass http://user.example.com;
} location = /user {
proxy_pass http://login.example.com;
}

使用模糊匹配时,如果URI最后没有加上“/”,在跳转的时候会自在URI后面加上“/”。如果不想这样,可以使用上面两种方法。

nignx的location正则匹配的更多相关文章

  1. nginx location 正则匹配

    nginx 统计语句1.根据访问IP统计UV awk '{print $1}' access.log|sort | uniq -c |wc -l2.统计访问URL统计PV awk '{print $7 ...

  2. Nginx 笔记与总结(7)Location:正则匹配

    在 /usr/local/nginx/conf/nginx.conf 的默认 server 段中,保留默认的 location 信息(之前测试的 location 配置删除): location / ...

  3. 07 nginx Location之正则匹配

    一:Location之正则匹配 再来看,正则也来参与. location / { root   /usr/local/nginx/html; index  index.html index.htm; ...

  4. nginx location模块--匹配规则

    Location语法语法:location [=|~|~*|^~] /uri/ { … } = --> 开头表示精确匹配 ^~ --> 开头表示uri以某个常规字符串开头,理解为匹配url ...

  5. Aho-Corasick算法、多模正则匹配、Snort入门学习

    希望解决的问题 . 在一些高流量.高IO的WAF中,是如何对规则库(POST.GET)中的字符串进行多正则匹配的,是单条轮询执行,还是多模式并发执行 . Snort是怎么组织.匹配高达上千条的正则规则 ...

  6. iOS 中的正则匹配(工具类方法)

    正则表达式 正则表达式是对字符串操作的一种逻辑公式, 用事先定义好的一些特定字符.及这些特定字符的组合, 组成一个"规则字符串", 这个"规则字符串"用来表达对 ...

  7. js:通过正则匹配获取页面的url中的参数

    简介:获取页面参数 原生js: //通过正则匹配获取当前页面的url中的参数 function getUrlParam(name){ var reg = new RegExp("(^|&am ...

  8. Nignx入门location、root配置

    nginx的配置.首当其冲的就是location配置了,下面是笔记参考的博文链接 http://www.cnblogs.com/sunkeydev/p/5225051.html   location匹 ...

  9. Nginx location 正则篇

    location 前缀 没有前缀               匹配以指定模式开头的location =                          精准匹配,不是以指定模式开头 ~       ...

随机推荐

  1. Alternating Strings Gym - 100712D 简单dp && Alternating Strings II Gym - 100712L 数据结构优化dp

    比赛链接:https://vjudge.net/contest/405905#problem/D 题意: 给你一个长度为n的由0或1构成的串s,你需要切割这个串,要求切割之后的每一个子串长度要小于等于 ...

  2. java随机数的产生

    两种产生随机数的方法: 1.通过import java.util.Random来实现 2.Math.random() 一.第一种的话就是导入Random之后,先生成一个Random对象r,之后再利用r ...

  3. Codeforces Round #652 (Div. 2) B. AccurateLee(思维)

    题意: 给你一个01字符串,现在你可以删除其中的一些子序列,要求如下:当遇到1 0的俩个连续子字符串后,可以删除其中的一个字符,现在要求把他删到尽量最短并且字典序最小,输出最后的字符串 题解: 刚开始 ...

  4. HDU-3081-Marriage Match II 二分图匹配+并查集 OR 二分+最大流

    二分+最大流: 1 //题目大意:有编号为1~n的女生和1~n的男生配对 2 // 3 //首先输入m组,a,b表示编号为a的女生没有和编号为b的男生吵过架 4 // 5 //然后输入f组,c,d表示 ...

  5. python程序配置守护进程

    参考博客 python Supervisor 使用与配置_a35155的博客-CSDN博客 Ubuntu系统下:apt-get install supervisor,通过这种方式安装后,自动设置为开机 ...

  6. Linux-文件查看命令

    目录 系统文件查看命令-cat 系统文件查看命令-more 系统文件查看命令-less 系统文件查看命令-head 系统文件查看命令-tail 系统文件查看命令-grep 文件上传下载命令-rz,sz ...

  7. ArcGIS处理栅格数据(二)

    五.栅格数据的配准 1.有参考图层 ① 在ArcMap里面添加需要配准的栅格数据集和参考数据集. ② 在ArcMap里面添加"地理配准"工具条. 添加成功后如下图所示: ③ 将需要 ...

  8. 2.hello rabbitmq

    作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-22 22:49:50 星期一 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...

  9. ElasticSearch 7.x 学习

    目录 ElasticSearch 7.x 一.前言 1.1.正向索引和倒排索引 1.1.1.正向索引 1.1.2.倒排索引 二.安装 三.ES 基本概念 3.1.索引 3.2.文档 3.4.mappi ...

  10. 超详细 DNS 协议解析

    尽人事,听天命.博主东南大学研究生在读,热爱健身和篮球,正在为两年后的秋招准备中,乐于分享技术相关的所见所得,关注公众号 @ 飞天小牛肉,第一时间获取文章更新,成长的路上我们一起进步 本文已收录于 C ...