NGINX $request_uri 与 $uri [英] NGINX $request_uri vs $uri

查看:44
本文介绍了NGINX $request_uri 与 $uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何确定何时使用 $request_uri$uri?

How do you determine when to use $request_uri vs $uri?

根据 NGINX 文档,$request_uri 是原始请求(例如,/foo/bar.php?arg=baz 包含参数,不能修改) 但 $uri 指的是更改后的 URI.

According to NGINX documentation, $request_uri is the original request (for example, /foo/bar.php?arg=baz includes arguments and can't be modified) but $uri refers to the altered URI.

如果 URI 没有改变,$uri = $request_uri 吗?

If the URI doesn't change, does $uri = $request_uri?

使用是不正确还是更好或更坏:

Would it be incorrect or better or worse to use:

map $uri $new_uri {
  # do something
}

对比

map $request_uri $new_uri {
  # do something
}

推荐答案

$uri 不等同于 $request_uri.

$uri 变量被设置为 nginx 正在当前处理的 URI - 但它也受到规范化,包括:

The $uri variable is set to the URI that nginx is currently processing - but it is also subject to normalisation, including:

  • 删除? 和查询字符串
  • 连续的 / 字符替换为单个 /
  • URL 编码的字符被解码
  • Removal of the ? and query string
  • Consecutive / characters are replace by a single /
  • URL encoded characters are decoded

$request_uri 的值始终是原始 URI,不受上述任何规范的约束.

The value of $request_uri is always the original URI and is not subject to any of the above normalisations.

大多数时候你会使用 $uri,因为它是标准化的.在错误的地方使用 $request_uri 会导致 URL 编码的字符变成双重编码.

Most of the time you would use $uri, because it is normalised. Using $request_uri in the wrong place can cause URL encoded characters to become doubly encoded.

如果需要匹配 URI 及其查询字符串,请在 map 指令中使用 $request_uri.

Use $request_uri in a map directive, if you need to match the URI and its query string.

这篇关于NGINX $request_uri 与 $uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆