【发布时间】:2017-08-14 20:11:51
【问题描述】:
我正在尝试将捕获的正则表达式组作为 URI 传递。
配置:
location ~ /proxy/(.*?)$ {
proxy_pass http://$1;
}
需要:
http://127.0.0.1:9999/proxy/example.com/test/asd.html
代理应该传递给:http://example.com/test/asd.html
结果:
> curl http://127.0.0.1:9999/proxy/example.com/test/asd.html
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
错误日志为空。
我在这里想念什么?谢谢
【问题讨论】:
-
好吧,这就是我必须为正则表达式做的事情。我拿了你的输入字符串并尝试使用 JS 正则表达式 let str = "127.0.0.1:9999/proxy/example.com/test/asd.html";让结果 = str.match(/proxy\/(.*)$/);控制台.log(结果[1]); ==> example.com/test/asd.html 所以你要么像我做的那样在代理之后转义'/'来获取example.com/test/asd.html,要么在分组中捕获它并使用proxy_pass http://$1 ; ==>请注意只有一个正斜杠
-
看看下面的代码块是否有效..我将它粘贴在答案块中以清楚地显示代码...
标签: regex nginx nginx-location