如何编写NGINX Url代理?

时间:2019-06-21 04:33:14

标签: nginx

我要转换网址

https://example.com/?city=FLorida&type=America&date=2019-06-06-15:15

https://example.com/Florida/America/06-june-2019-1515

当用户请求第二个URL时,使用nginx作为代理来提供第一个URL。

1 个答案:

答案 0 :(得分:1)

困难在于将月份名称转换为数字,可以使用带有12个正则表达式的map来解决。有关详细信息,请参见this document

第一个示例执行map中的所有操作。我不知道您打算如何在内部处理重写的URI,所以已经使用重定向对其进行了测试。

map $uri $newuri {
    default  0;
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-jan(uary)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
       "/?city=$city&type=$type&date=$year-01-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-feb(ruary)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-02-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-mar(ch)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-03-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-apr(il)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-04-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-may-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-05-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-june?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-06-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-july?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-07-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-aug(ust)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-08-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-sep(tember)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-09-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-oct(ober)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-10-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-nov(ember)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-11-$day $hour:$min";
    ~*^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-dec(ember)?-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$
        "/?city=$city&type=$type&date=$year-12-$day $hour:$min";
}

server {
    ...
    if ($newuri) { return 302 $newuri; }
    ...
}

或者,仅使用map来翻译月份名称:

map $month $month_num {
    ~*^jan(uary)?$          "01";
    ~*^feb(ruary)?$         "02";
    ~*^mar(ch)?$            "03";
    ~*^apr(il)?$            "04";
    ~*^may$                 "05";
    ~*^june?$               "06";
    ~*^july?$               "07";
    ~*^aug(ust)?$           "08";
    ~*^sep(tember)?$        "09";
    ~*^oct(ober)?$          "10";
    ~*^nov(ember)?$         "11";
    ~*^dec(ember)?$         "12";
}

server {
    ...
    if ($uri ~* ^/(?<city>\w+)/(?<type>\w+)/(?<day>\d\d)-(?<month>\w+)-(?<year>\d\d\d\d)-(?<hour>\d\d)(?<min>\d\d)$)
    {
        return 302 "/?city=$city&type=$type&date=$year-$month_num-$day $hour:$min";
    }
    ...
}

有关if的使用,请参见this caution