简体   繁体   English

没有参数的 Nginx request_uri

[英]Nginx request_uri without args

How do I get the value of request_uri without the args appended on the end.如何在没有附加参数的情况下获取request_uri的值。 I know there is a uri variable but I need the original value as the Nginx documentation states:我知道有一个uri变量,但我需要原始值,如 Nginx 文档所述:

request_uri request_uri

This variable is equal to the original request URI as received from the client including the args.此变量等于从客户端收到的原始请求 URI,包括 args。 It cannot be modified.它不能被修改。 Look at $uri for the post-rewrite/altered URI.查看 $uri 以获取重写/更改后的 URI。 Does not include host name.不包括主机名。 Example: "/foo/bar.php?arg=baz"示例:“/foo/bar.php?arg=baz”

I use this map, which works without lua:我使用这张地图,它在没有 lua 的情况下工作:

map $request_uri $request_uri_path {
  "~^(?P<path>[^?]*)(\?.*)?$"  $path;
}

You're looking for $uri.您正在寻找 $uri。 It does not have $args.它没有 $args。 In fact, $request_uri is almost equivalent to $uri$args.事实上,$request_uri 几乎等同于 $uri$args。

If you really want exactly $request_uri with the args stripped, you can do this.如果你真的想要去掉 args 的 $request_uri,你可以这样做。

local uri = string.gsub(ngx.var.request_uri, "?.*", "")

You will need to have lua available but that will do exactly what you're asking.您需要有 lua 可用,但这将完全满足您的要求。

The accepted answer pointed me in the right direction, but I had to figure out where to add that directive, After some time investigating I found set_by_lua_block接受的答案为我指明了正确的方向,但我必须弄清楚在哪里添加该指令,经过一段时间的调查,我发现set_by_lua_block

set_by_lua_block $request_uri_path {
    return string.gsub(ngx.var.request_uri, "?.*", "")
}

I hope it saves some time to those who comes here.我希望它为那些来到这里的人节省一些时间。

Ryan Olson's answer is really good but map cannot be used everywhere. Ryan Olson 的回答非常好,但不能到处使用map

If you need to get that information at a block level that does not accept the use of map , it can be done in a if statement.如果您需要在不接受使用map的块级别获取该信息,则可以在if语句中完成。 But remember that if is evil .但请记住, 如果是邪恶的

set $new_uri "";
if ($request_uri ~ "^([^?]*)(\?.*)?$") {
    set $new_uri $1;
}

This code works out of the box (it does not use Lua or anything else).此代码开箱即用(它不使用 Lua 或其他任何东西)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM