NGINX $ request_uri vs $ uri

tek*_*ova 25 uri nginx

你如何确定何时使用$request_urivs $uri

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

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

使用时是不正确的,更好的还是更糟的:

map $uri $new_uri {
  # do something
}
Run Code Online (Sandbox Code Playgroud)

VS

map $request_uri $new_uri {
  # do something
}
Run Code Online (Sandbox Code Playgroud)

Ric*_*ith 37

$uri不等于$request_uri.

$uri变量设置nginx当前正在处理的URI - 但它也需要进行规范化,包括:

  • 删除?和查询字符串
  • 连续/字符由单个字符替换/
  • URL编码的字符被解码

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

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

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

  • 另外,在`proxy_pass`指令中意外使用$ uri可以打开HTTP标头注入漏洞。始终在proxy_pass中使用`$ request_uri`。 (4认同)
  • @VictorSchröder说`/ static / *`被proxy_pass到了另一个目的地,但是不小心使用了$ uri而不是$ request_uri。您可以导航到`/static/X%20HTTP/1.1%0d%0aHost:hijacked-host-header%0d%0a%0d%0a`,然后注入Host:hijacked-host-header标头。 (4认同)
  • @fotinakis,您能举个这样的例子吗?我真的无法想象一个单一的方案。 (2认同)
  • @fotinakis 你可以通过设置 `proxy_pass_request_headers off;` 来解决这个问题吗? (2认同)

Rod*_*rry 6

关于另一个不同之处$uri,并$request_uriproxy_cache_keyIS $request_uri将包括anchor tags part,但$uri$is_args$args会将其忽略

进行卷曲操作curl -I static.io/hello.htm?id=1#/favor/goods::

proxy_cache_key $scheme://$host$uri$is_args$args; => Cache KEY: http://static.io/hello.htm?id=1
proxy_cache_key $scheme://$host$request_uri; => Cache KEY: http://static.io/hello.htm?id=1#/favor/goods
Run Code Online (Sandbox Code Playgroud)

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

  • 据我所知,没有浏览器将片段标识符发送到服务器:faqs.org/rfcs/rfc1808.html 2.4.1。解析片段标识符 (14认同)