nginx:用参数重写很多(2000+)个 url [英] nginx: rewrite a LOT (2000+) of urls with parameters

查看:22
本文介绍了nginx:用参数重写很多(2000+)个 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须迁移很多带有参数的 URL,如下所示:

I have to migrate a lot of URLs with params, which look like that:

/somepath/somearticle.html?p1=v1&p2=v2 --> /some-other-path-a

还有没有参数的相同网址:

and also the same URL without params:

/somepath/somearticle.html --> /some-other-path-b

棘手的部分是两个目标 URL 在新系统中是完全不同的页面,而在旧系统中,参数只是指示默认打开哪个选项卡.

The tricky part is that the two destination URLs are totally different pages in the new system, whereas in the old system the params just indicated which tab to open by default.

我尝试了不同的重写规则,但得出的结论是nginx重写不考虑参数.我找到了一种使用位置指令的方法,但是有 2000 多个位置指令感觉不对.

I tried different rewrite rules, but came to the conclusion that parameters are not considered by nginx rewrites. I found a way using location directives, but having 2000+ location directives just feels wrong.

有人知道如何优雅地完成这项工作吗?值得注意的是,除了那些 2000 多个重定向之外,我还有另外 200.000(!) 个重定向.它们已经起作用了,因为它们相当简单.所以我想强调的是,性能应该是关键!

Does anybody know an elegant way how to get this done? It may be worth noting that beside those 2000+ redirects, I have another 200.000(!) redirects. They already work, because they're rather simple. So what I want to emphasize is that performance should be key!

推荐答案

您无法匹配location 中的查询字符串(从? 开始的任何内容)重写 表达式,因为它不是规范化 URI 的一部分.有关详细信息,请参阅本文档.

You cannot match the query string (anything from the ? onwards) in location and rewrite expressions, as it is not part of the normalized URI. See this document for details.

整个 URI 在 $request_uri 参数中可用.如果参数的发送顺序不一致,则使用 $request_uri 可能会出现问题.

The entire URI is available in the $request_uri parameter. Using $request_uri may be problematic if the parameters are not sent in a consistent order.

要处理多个 URI,请使用 map 指令,例如:

To process many URIs, use a map directive, for example:

map $request_uri $redirect {
    default 0;
    /somepath/somearticle.html?p1=v1&p2=v2  /some-other-path-a;
    /somepath/somearticle.html              /some-other-path-b;
}

server {
    ...
    if ($redirect) {
        return 301 $redirect;
    }
    ...
}

您还可以在 map 中使用正则表达式,例如,如果 URI 还包含可选的不匹配参数.请参阅本文档了解更多信息.

You can also use regular expressions in the map, for example, if the URIs also contain optional unmatched parameters. See this document for more.

这篇关于nginx:用参数重写很多(2000+)个 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆