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

查看:136
本文介绍了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

以及没有参数的相同URL:

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!

推荐答案

您不能在locationrewrite表达式中匹配查询字符串(从?开始的任何内容),因为它不是规范化的一部分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;
    }
    ...
}

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

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天全站免登陆