Nginx重定向以获取特定的参数列表 [英] Nginx redirecting for specific list of arguments

查看:308
本文介绍了Nginx重定向以获取特定的参数列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在URL中列出了一些过时的参数.

arg1
arg2
arg3
...
argn

我需要将链接的查询部分中具有任何自变量的任何请求重定向到特定站点.

/page.html?arg1=sxx&arg2=uuu  -> http://xxx.x.xxx.x
/page.php?arg3=  -> http://xxx.x.xxx.x
/dir/dir/page/?arg2=111&& argn=xyyyy  -> http://xxx.x.xxx.x
/page.html (is not redirected but matched to other existing rules in nginx)

有什么想法可以很好地表达它吗?由于某些原因,location没有参数要与正则表达式匹配.

解决方案

假设参数顺序是确定性的,则可以针对$ request_uri变量测试多个正则表达式.

map指令可用于列出多个规则和目标.

例如:

map $request_uri $redirect {
    default                          0;
    ~^/page\.html\?arg1=sxx&arg2=uuu http://xxx.x.xxx.x;
    ~^/page\.php\?arg3=              http://xxx.x.xxx.x;
    ...
}

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

您当然希望通过插入空格(.*)和单词边界(\b)来改进上述正则表达式.

有关map指令,请参见本文档,以及关于使用if的注释. /p>

I have a list of some obsolete arguments in URLs.

arg1
arg2
arg3
...
argn

I need to redirect any of the request having any of those arguments in the query part of the link, to a specific site.

/page.html?arg1=sxx&arg2=uuu  -> http://xxx.x.xxx.x
/page.php?arg3=  -> http://xxx.x.xxx.x
/dir/dir/page/?arg2=111&& argn=xyyyy  -> http://xxx.x.xxx.x
/page.html (is not redirected but matched to other existing rules in nginx)

Any idea how to express it nicely? For some reasons location has no arguments to be matched by regular expression.

解决方案

Assuming that the argument order is deterministic, you can test multiple regular expressions against the $request_uri variable.

The map directive can be used to list multiple rules and destinations.

For example:

map $request_uri $redirect {
    default                          0;
    ~^/page\.html\?arg1=sxx&arg2=uuu http://xxx.x.xxx.x;
    ~^/page\.php\?arg3=              http://xxx.x.xxx.x;
    ...
}

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

You will of course want to improve the regular expressions above, by inserting gaps (.*) and word boundaries (\b).

See this document for the map directive, and this note on the use of if.

这篇关于Nginx重定向以获取特定的参数列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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