资讯专栏INFORMATION COLUMN

nginx url rewriting: difference between break and

BDEEFE / 2712人阅读

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 /documents
Explanation:

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 /notes
Explanation:

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)

Example 3: Inside location block - "break"
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

Example 4: Inside location block - "last"
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 /notes
Explanation:

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!

Summary:

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!

Final Note:

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

相关文章

  • nginx rewrite配置解读

    序 本文主要解析一下ngx_http_rewrite_module中的rewrite相关配置。 directives 名称 默认配置 作用域 官方说明 中文解读 模块 break 无 server, location, if Stops processing the current set of ngx_http_rewrite_module directives. 中断当前的重写 ng...

    IamDLY 评论0 收藏0
  • nginx(二):进阶配置介绍--rewrite用法,压缩,https虚拟主机等

    摘要:经由超文本传输协议通信,但是数据包由安全协议加密,实现加密数据与认证功能。该模块指令定义相关设置证书文件,私钥文件,会话缓存等内容。允许在客户端建立会话时传递请求服务器名称,这样服务器就会知道该发送哪个虚拟主机下的证书文件。 1、nginx基本状态信息页面 配置示例: location /basic_status { stub_...

    JayChen 评论0 收藏0
  • nginx 常用配置记录

    摘要:如果状态码附带文字段落,该文本将被放置在响应主体。相反,如果状态码后面是一个,该将成为头部值。没有状态码的将被视为一个状态码,这种情况下需要以或者开头。因为和不能简单的只返回状态码,还必须有重定向的,这就是指令无法返回的原因了。 HTTP模块(核心模块,也是主要用到的模块) server模块 server模块是http的子模块,它用来定义一个虚拟主机 例子: server { ...

    Youngs 评论0 收藏0
  • Nginx rewrite配置规则

    摘要:语法规则定向路径重写类型规则可以是字符串或者正则来表示想匹配的目标定向路径表示匹配到规则后要定向的路径,如果规则里有正则,则可以使用来表示正则里的捕获分组重写类型相当于里德标记,表示完成,浏览器地址栏地址不变本条规则匹配完成后,终止匹配,不 rewrite语法 server { rewrite {规则} {定向路径} {重写类型} ; } 1、规则:可以是字符串或者正则来表示想...

    gggggggbong 评论0 收藏0
  • 搞懂nginx的rewrite模块

    摘要:非标准码关闭连接而不发送响应报头。指令按照它们在配置文件中出现的顺序执行。可以使用标志来终止指令的进一步处理。返回永久重定向。发送如下请求控制是否记录有关未初始化变量的警告。 之前在配置nginx时,总是遇到rewrite指令的last和break标识的问题,看到的资料大都是last 基本上都用这个 Flag,break 中止 Rewirte,不在继续匹配。看完之后还是有点懵,后来看了...

    wangshijun 评论0 收藏0

发表评论

0条评论

BDEEFE

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<