以这个配置为例
location /test {
proxy_pass http://localhost/
}
当请求 /test/api?a=1 时 实际向上游发起的地址为 http://localhost/api?a=1
这由 nginx 的内置机制自动完成
现在,是否有 nginx 变量或者其他方案可以获取到这个 http://localhost/api?a=1 (目前是人工算的)
如果自己利用 $uri $request_uri 之类的拼接很麻烦
location /test {
proxy_pass http://localhost/
}
当请求 /test/api?a=1 时 实际向上游发起的地址为 http://localhost/api?a=1
这由 nginx 的内置机制自动完成
现在,是否有 nginx 变量或者其他方案可以获取到这个 http://localhost/api?a=1 (目前是人工算的)
如果自己利用 $uri $request_uri 之类的拼接很麻烦
9 条回复 • 2025-03-21 14:20:38 +08:00
|
1
JohnZorn 2025 年 3 月 21 日 ```conf
location /test/ { proxy_pass http://localhost/; proxy_set_header Host $proxy_host; } ``` |
|
3
66z 2025 年 3 月 21 日 add herader 的功能,加上内置变量拼接应该可以,写法问下 AI
|
|
4
a33291 OP @66z 不好弄,自带的指令能力非常有限,同时比如 proxy_pass 尾部带/和不带/区别也很明显,这些细节在自己拼接 URL 非常难受,所以想要直接拿到 nginx 完全处置完成之后的结果 path
|
|
5
Hanada 2025 年 3 月 21 日 |
|
6
Hanada 2025 年 3 月 21 日 这个靠配置是无法实现的,如果你要准确的上游 uri 的话,其他很多上游变量都需要单独导出。我主要是为了给我自己开发的上游日志模块做变量支撑才实现的: https://github.com/HanadaLee/ngx_http_upstream_log_module
|
|
7
a33291 OP @Hanada #6 感谢大佬,请教一下 $upstream_uri 就是已经规范化之后的最终上游地址了吗?
另外这个变量貌似 openresty 还是 nginx 官方自己的 upstream 模块也输出这个变量,如果同时用的话感觉会冲突(主要是他默认版本就包含了 upstream 模块) |
› NGINX
› NGINX Trac
› 3rd Party Modules
› Security Advisories
› CHANGES
› OpenResty
› ngx_lua
› Tengine
在线学习资源
› NGINX 开发从入门到精通
NGINX Modules
› ngx_echo