cation匹配命令
~ #波浪线表示执行一个正则匹配,区分大小写
~* #表示执行一个正则匹配,不区分大小写
^~ #^~表示普通字符匹配,不是正则匹配。如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
= #进行普通字符精确匹配
@ #"@" 定义一个命名的 location,使用在内部定向时,例如 error_page, try_files
参考:https://segmentfault.com/a/1190000002797606
location 优先级官方文档
1. Directives with the = prefix that match the query exactly. If found, searching stops.
2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
3. Regular expressions, in order of definition in the configuration file.
4. If #3 yielded a match, that result is used. Else the match from #2 is used.
1. =前缀的指令严格匹配这个查询。如果找到,停止搜索。
2. 所有剩下的常规字符串,最长的匹配。如果这个匹配使用^前缀,搜索停止。
3. 正则表达式,在配置文件中定义的顺序。
4. 如果第3条规则产生匹配的话,结果被使用。否则,如同从第2条规则被使用。
顺序 no优先级: (location =) > (location 完整路径) > (location ^~ 路径) > (location ~,~* 正则顺序) > (location 部分起始路径) > (/)
例如:
location = / {
# 只匹配"/".
如果URI中含有参数(/app/test.php?id=5),默认情况下参数会被自动附加到替换串上,可以通过在替换串的末尾加上?标记来解决这一问题。
例如:
复制代码代码示例:
rewrite ^/test(.*)$ http://www.it.net.cn/home permanent;
访问http://www.it.net.cn/test?id=5 会跳转到 http://www.it.net.cn/home?id=5
例如:如果将类似URL /photo/123456 重定向到 /path/to/photo/12/1234/123456.png
复制代码代码示例:
Rewrite "/photo/([0-9]{2})([0-9]{2})([0-9]{2})" /path/to/photo/$1/$1$2/$1$2$3.png ;
set指令。这里的变量名和php的语法差不多。变量名前面定义$代表定义变量(set)或者引用变量。
Syntax:set$variablevalue;Default:—Context:server, location, if Sets a value for the specified variable. The value can contain text, variables, and their combination.