nginx重写规则(转换segments获取参数)

nginx rewrite rule (convert segments to get paramters)

我需要

/secure/token/timestamp/path/to/the/resource/

以这种格式重写:

/s/path/to/the/resource/?st=token&e=timestamp

我想做的是:

location /secure/(?<token>)/(?<timestamp>)/(?<path>) {
    rewrite ^ /s/$path/?st=$token&e=$timestamp
}

但它显然不起作用。此语法似乎不正确。我怎么能在 nginx 规则中这样玩 urls 呢?

编辑: 现在尝试这样的事情:相同的结果:

     location /secure/(.*)/(.*)/(.*) {
         rewrite ^ /s/?st=token&e=timestamp;
    }

您有多个问题。您将前缀位置与正则表达式位置混淆了。您命名的捕获不包含任何内容。而且您的数字捕获可能太宽泛了。

有关位置语法,请参阅 this

您可能应该使用前缀位置,然后在重写中捕获:

location /secure/ {
    rewrite ^/secure/([^/]*)/([^/]*)/(.*)$ /s/?st=&e= last;
}