Example 1: No (break or last) flags
server { server_name example.com; root "path/to/somewhere"; location / { echo "finally matched location /"; } location /notes { echo "finally matched location /notes"; } location /documents { echo "finally matched location /documents"; } rewrite ^/([^/]+.txt)$ /notes/$1; rewrite ^/notes/([^/]+.txt)$ /documents/$1; }Result:
#curl example.com/test.txt finally matched location /documentsExplanation:
For rewrite, the flags are optional!
Example 2: Outside location block (break or last)server { server_name example.com; root "path/to/somewhere"; location / { echo "finally matched location /"; } location /notes { echo "finally matched location /notes"; } location /documents { echo "finally matched location /documents"; } rewrite ^/([^/]+.txt)$ /notes/$1 break; # or last rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed }Result:
#curl example.com/test.txt finally matched location /notesExplanation:
Outside the location block, both break and last behave in the exact manner...
no more parsing of rewrite conditions
Nginx internal engine goes to the next phase (searching for location match)
server { server_name example.com; root "path/to/somewhere"; location / { echo "finally matched location /"; rewrite ^/([^/]+.txt)$ /notes/$1 break; rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed } location /notes { echo "finally matched location /notes"; } location /documents { echo "finally matched location /documents"; } }Result:
# curl example.com/test.txt finally matched location /Explanation:
Inside a location block, break flag would do the following...
no more parsing of rewrite conditions
Nginx internal engine continues to parse the current location block
server { server_name example.com; root "path/to/somewhere"; location / { echo "finally matched location /"; rewrite ^/([^/]+.txt)$ /notes/$1 last; rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed } location /notes { echo "finally matched location /notes"; rewrite ^/notes/([^/]+.txt)$ /documents/$1; # this is not parsed, either! } location /documents { echo "finally matched location /documents"; } }Result:
# curl example.com/test.txt finally matched location /notesExplanation:
Inside a location block, last flag would do the following...
no more parsing of rewrite conditions
Nginx internal engine starts to look for another location match based on the result of the rewrite result.
no more parsing of rewrite conditions, even on the next location match!
When a rewrite condition with the flag break or last matches, Nginx stops parsing any more rewrites!
Outside a location block, with break or last, Nginx does the same job (stops processing anymore rewrite conditions).
Inside a location block, with break, Nginx only stops processing anymore rewrite conditions
Inside a location block, with last, Nginx stops processing anymore rewrite conditions and then starts to look for a new matching of location block! Nginx also ignores any rewrites in the new location block!
missed to include some more edge cases (actually common problem with rewrites, such as 500 internal error). But, that"d be out of scope of this question. Probably, example 1 is out of scope, too!
链接:https://serverfault.com/quest...
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/39884.html
序 本文主要解析一下ngx_http_rewrite_module中的rewrite相关配置。 directives 名称 默认配置 作用域 官方说明 中文解读 模块 break 无 server, location, if Stops processing the current set of ngx_http_rewrite_module directives. 中断当前的重写 ng...
摘要:经由超文本传输协议通信,但是数据包由安全协议加密,实现加密数据与认证功能。该模块指令定义相关设置证书文件,私钥文件,会话缓存等内容。允许在客户端建立会话时传递请求服务器名称,这样服务器就会知道该发送哪个虚拟主机下的证书文件。 1、nginx基本状态信息页面 配置示例: location /basic_status { stub_...
摘要:如果状态码附带文字段落,该文本将被放置在响应主体。相反,如果状态码后面是一个,该将成为头部值。没有状态码的将被视为一个状态码,这种情况下需要以或者开头。因为和不能简单的只返回状态码,还必须有重定向的,这就是指令无法返回的原因了。 HTTP模块(核心模块,也是主要用到的模块) server模块 server模块是http的子模块,它用来定义一个虚拟主机 例子: server { ...
摘要:语法规则定向路径重写类型规则可以是字符串或者正则来表示想匹配的目标定向路径表示匹配到规则后要定向的路径,如果规则里有正则,则可以使用来表示正则里的捕获分组重写类型相当于里德标记,表示完成,浏览器地址栏地址不变本条规则匹配完成后,终止匹配,不 rewrite语法 server { rewrite {规则} {定向路径} {重写类型} ; } 1、规则:可以是字符串或者正则来表示想...
摘要:非标准码关闭连接而不发送响应报头。指令按照它们在配置文件中出现的顺序执行。可以使用标志来终止指令的进一步处理。返回永久重定向。发送如下请求控制是否记录有关未初始化变量的警告。 之前在配置nginx时,总是遇到rewrite指令的last和break标识的问题,看到的资料大都是last 基本上都用这个 Flag,break 中止 Rewirte,不在继续匹配。看完之后还是有点懵,后来看了...
阅读 1312·2021-11-24 09:39
阅读 1321·2021-11-04 16:12
阅读 2670·2021-09-24 09:47
阅读 3304·2021-09-01 10:50
阅读 1450·2019-08-30 15:55
阅读 1407·2019-08-30 15:43
阅读 625·2019-08-30 11:08
阅读 3561·2019-08-23 18:33