对“位置"的不同行为会导致“位置"改变.和"proxy_pass"在nginx上的x-accel重定向 [英] Different behavour of "location" and "proxy_pass" on nginx x-accel-redirect
问题描述
我有我的nginx conf:
I have my nginx conf like :
location ^~ /mount_points/mount_point1 {
internal;
alias /repos/mount_point_one;
}
location ^~ /to_proxy {
internal;
proxy_pass http://myproxy:5000;
}
当我请求' http://localhost/mount_points/mount_point1/myfile.zip '我按预期得到了"/repos/mount_point_one/myfile.zip".
When I request for 'http://localhost/mount_points/mount_point1/myfile.zip' I get "/repos/mount_point_one/myfile.zip" as expected.
当请求' http://localhost/to_proxy/myfile2.html '时,我得到了" http://myproxy:5000/to_proxy/myfile2.html ".
While request for 'http://localhost/to_proxy/myfile2.html', I get "http://myproxy:5000/to_proxy/myfile2.html".
在第一种情况下,"/mount_points/mount_point1"部分已删除,在第二种情况下,"/to_proxy"部分仍然存在,我必须在上游服务器中伪造"/to_proxy"地址才能找到出来这个.
In the first case, the "/mount_points/mount_point1" part was removed, and in the second case, the "/to_proxy" part still there, I have to fake a "/to_proxy" address in the upstream server to find out this.
我错过了什么吗?如果只需要重写URL,如何删除上游服务器中的"/to_proxy"部分问题?
Did I missed something? If I just have to rewrite the url, how can I delete the "/to_proxy" part issue to the upstream server?
谢谢.
推荐答案
proxy_pass
指令可以执行别名功能,但前提是必须提供可选的URI.
The proxy_pass
directive can perform an aliasing function, but only if an optional URI is provided.
location ^~ /to_proxy/ {
internal;
proxy_pass http://myproxy:5000/;
}
为使别名映射正常工作,还在location
参数后添加了结尾的/
.
To make the alias mapping work correctly, a trailing /
is also added to the location
parameter.
有关详细信息,请参见此文档.
See this document for details.
如果location
参数上的尾随/
引起问题,则可以使用rewrite ... break
代替:
If the trailing /
on the location
parameter causes problems, you can use a rewrite ... break
instead:
location ^~ /to_proxy {
internal;
rewrite ^/to_proxy(?:/(.*))?$ /$1 break;
proxy_pass http://myproxy:5000;
}
这篇关于对“位置"的不同行为会导致“位置"改变.和"proxy_pass"在nginx上的x-accel重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!