NGINX $request_uri 与 $uri
如何确定何时使用 $request_uri
与 $uri
?
根据 NGINX 文档, $request_uri
是原始请求(例如 /foo/bar.php?arg=baz
包含参数且无法修改),但是 < code>$uri 指的是更改后的 URI。
如果 URI 没有改变,$uri = $request_uri 吗?
是不正确的还是更好还是更差?
map $uri $new_uri {
# do something
}
使用: vs
map $request_uri $new_uri {
# do something
}
How do you determine when to use $request_uri
vs $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.
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
}
vs
map $request_uri $new_uri {
# do something
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$uri
不等于$request_uri
。$uri
变量设置为nginx
当前正在处理的 URI - 但它也需要进行规范化,包括:/
字符被替换为单个/
$request_uri
的值始终是原始 URI,并且不受任何上述标准化。大多数时候您会使用
$uri
,因为它是标准化的。在错误的位置使用$request_uri
可能会导致 URL 编码字符变成双重编码。如果需要匹配 URI 及其查询字符串,请在
map
指令中使用$request_uri
。$uri
is not equivalent to$request_uri
.The
$uri
variable is set to the URI thatnginx
is currently processing - but it is also subject to normalisation, including:?
and query string/
characters are replace by a single/
The value of
$request_uri
is always the original URI and is not subject to any of the above normalisations.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.Use
$request_uri
in amap
directive, if you need to match the URI and its query string.proxy_cache_key
中$uri
和$request_uri
的另一个区别是$request_uri
将包含锚标记部分< /code>,但
$uri$is_args$args
会忽略它执行卷曲操作:
curl -I static.io/hello.htm?id=1#/favor/goods
:Nginx 文档:http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri
$request_uri
:完整原始请求 URI(带参数)$uri
:请求中的当前 URI,已标准化 $uri 的值可能会更改
在请求处理期间,例如在进行内部重定向时,或者
使用索引文件时。
代理缓存密钥:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key
Another difference about
$uri
and$request_uri
inproxy_cache_key
is$request_uri
will includeanchor tags part
, but$uri$is_args$args
will ignore itDo a curl operation :
curl -I static.io/hello.htm?id=1#/favor/goods
:Nginx Document: http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri
$request_uri
: full original request URI (with arguments)$uri
:current URI in request, normalized The value of $uri may change
during request processing, e.g. when doing internal redirects, or
when using index files.
Proxy Cache key:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key