哪种形式的Nginx重写更有效?

时间:2018-10-09 19:04:45

标签: regex nginx url-rewriting processing-efficiency

重构网站时,我在.htaccess中有一堆重定向。现在,我想为nginx重新创建它们。以下是否等效,哪种样式更有效?

# Style A
location / {
    location /foo/123/old {
        rewrite ^(.*) /bar/123/ permanent;
    }
    location /baz/456/old {
        rewrite ^(.*) /bar/456/ permanent;
    }
}

# Style B
location / {
    rewrite ^/foo/123/old\/?$ /bar/123/ permanent;
    rewrite ^/baz/456/old/\/?$ /bar/456/ permanent;
}

1 个答案:

答案 0 :(得分:0)

根据nginx自己的建议,您应该使用位置块和return指令

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#taxing-rewrites