英文:
Nginx location regex for any uri path and proxy to /
问题
我有一个网站 http://example.com,我需要将所有路径请求,如 http://example.com/*,重定向到 /
我已经有适用于精确两个路径的工作代码:
location ~ ^/(test1|test2) {
rewrite ^/(test1|test2)$ / break;
proxy_pass http://127.0.0.1;
}
但对于通配符,它不起作用:
location ~ ^/(.*) {
rewrite ^/(.*)$ / break;
proxy_pass http://127.0.0.1;
}
是正则表达式错误吗?
英文:
I have a site http://example.com and I need to route all requests for any path like http://example.com/* to /
I have working code for exact 2 paths:
location ~ ^/(test1|test2) {
rewrite ^/(test1|test2)$ / break;
proxy_pass http://127.0.0.1;
}
But for wildcard it doesn't work:
location ~ ^/(.*) {
rewrite ^/(.*)$ / break;
proxy_pass http://127.0.0.1;
}
Is it wrong regex or what?
答案1
得分: 1
这个位置会匹配除根路径以外的任何路径。
英文:
Try this:
location ~ /(.+) {
proxy_pass http://127.0.0.1/;
}
This location includes any path except for the root.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论