【问题标题】:Nginx redirecting for specific list of argumentsNginx 重定向特定参数列表
【发布时间】:2017-03-16 19:57:44
【问题描述】:

我有一个 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 没有参数可以被正则表达式匹配。

【问题讨论】:

    标签: nginx nginx-location


    【解决方案1】:

    假设参数顺序是确定性的,您可以针对 $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 指令的信息请参见this document,有关if 的使用请参见this note

    【讨论】:

      猜你喜欢
      • 2015-01-20
      • 2014-02-08
      • 1970-01-01
      • 2014-12-10
      • 2014-04-07
      • 2016-04-16
      • 1970-01-01
      • 2023-02-25
      相关资源
      最近更新 更多