Nginx的location正则表达式匹配任何URI路径并代理到/。

huangapple go评论141阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2023年3月15日 18:38:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743547.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定