Nginx - location 指令(二)

location directive

location [ = | ~ | ~* | ^~ ] uri {

}

不讨论 location @name {} 形式

官方文档有如下描述:

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.

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.

location 指令由修饰符和匹配项组成,匹配项可以是 前缀字符串(prefix string,也就是仅从uri的开头匹配) 或者 正则表达式(regular expression,uri全局正则匹配),共有以下五种组合

  • prefix-string
  • = prefix-string
  • ~ regular-expression 大小写敏感
  • ~* regular-expression 大小写不敏感
  • ^~ prefix-string

注意查找匹配顺序,查找和使用的优先级并不一样:

  1. 精确匹配= prefix-string,成功则停止;
  2. 匹配其他prefix strings,选中并记住所有匹配成功中的最长匹配(longest matching prefix);如果当前最长匹配有^~修饰符,则停止;
  3. 正则匹配[~* | ~] regular expressions,顺序为在配置文件中的出现顺序,成功则停止;
  4. 使用第2步查找到的最长匹配项,否则404 Not Found。

测试

	location / {
		return 200 '/\n';
	}

	location /images/avatars {
		return 200 '/images/avatars\n';
	}
	
	location = /images {
		return 200 '= /images\n';
	}
	
	# 注意是否是前缀匹配 **成功** 中的 **最长** 项目
	location ^~ /images {
		return 200 '^~ images\n';
	}
	
	location ~* \.(png|jpg)$ {
		return 200 '~* \.(png|jpg)\n';
	}
url location 顺序
curl localhost / 1.2(/).3.4.=>2(/).
curl localhost/x / 1.2(/).3.4.=>2(/).
curl localhost/images = /images 1.
curl localhost/images/me.jpg ^~ images 1. 2(^~ /images).
curl localhost/images/avatars /images/avatars 1.2(/images/avatars).3.4.=>2(/images/avatars).
curl localhost/images/avatars/me.jpg ~* \.(png|jpg)$ 1.2(/images/avatars).3.
curl localhost/images/xxx/me.jpg ^~ images 1.2(^~ /images).

References

相关推荐

  1. Nginx - location 指令

    2024-05-11 20:30:07       35 阅读
  2. linux系统nginx工具location指令

    2024-05-11 20:30:07       53 阅读
  3. Nginx配置文件中Location指令的匹配优先级

    2024-05-11 20:30:07       32 阅读
  4. nginxlocation

    2024-05-11 20:30:07       158 阅读
  5. nginx 正则表达式 location rewirte

    2024-05-11 20:30:07       48 阅读
  6. NGINX location块的用法

    2024-05-11 20:30:07       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-11 20:30:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-11 20:30:07       101 阅读
  3. 在Django里面运行非项目文件

    2024-05-11 20:30:07       82 阅读
  4. Python语言-面向对象

    2024-05-11 20:30:07       91 阅读

热门阅读

  1. Linux监听某个进程,自动重启

    2024-05-11 20:30:07       29 阅读
  2. 数据字典是什么?

    2024-05-11 20:30:07       35 阅读
  3. 【前端每日基础】day2 const var let的区别

    2024-05-11 20:30:07       35 阅读
  4. MySQL学习笔记12——效率和优化

    2024-05-11 20:30:07       147 阅读
  5. Unity 委托与事件、装箱和拆箱

    2024-05-11 20:30:07       32 阅读
  6. React 学习-5

    2024-05-11 20:30:07       34 阅读
  7. 6.5.Docker数据管理和端口映射应用

    2024-05-11 20:30:07       23 阅读
  8. 算法练习17——罗马数字转整数

    2024-05-11 20:30:07       28 阅读
  9. debian apt 更改阿里源

    2024-05-11 20:30:07       23 阅读
  10. android原生开发学习路线

    2024-05-11 20:30:07       30 阅读