如何从nginx中的位置获取变量?

sat*_*els 14 debian nginx

来自 nginx conf 的位置:

location ~/photos/resize/(\d+)/(\d+) {
    # Here var1=(\d+), var2=(\d+)
}
Run Code Online (Sandbox Code Playgroud)

如何从nginx中的位置获取变量?

Nex*_*rus 20

正则表达式的工作方式与其他所有拥有它的地方非常相似。

location ~/photos/resize/(\d+)/(\d+) {
  # use $1 for the first \d+ and $2 for the second, and so on.
}
Run Code Online (Sandbox Code Playgroud)

查看 nginx wiki 上的示例也可能有所帮助,http://wiki.nginx.org/Configuration


小智 17

除了前面的答案,您还可以设置正则表达式捕获组的名称,以便以后更容易引用它们;

location ~/photos/resize/(?<width>(\d+))/(?<height>(\d+)) {
  # so here you can use the $width and the $height variables
}
Run Code Online (Sandbox Code Playgroud)

参见NGINX:检查 $remote_user 是否等于位置的第一部分以获取用法示例。

  • 我认为你可以只写 `(?&lt;width&gt;\d+)` 而不是 `(?&lt;width&gt;(\d+))`,或者是否有其他原因 - 也许也可以得到 `$1`作为`$width`? (2认同)