如何使 nginx 位置匹配带有斜线和不带斜线的路径?

how to make nginx location match path both with slash and without slash?

目前我的 nginx 文件中有这个位置,它不适用于 http://mydomain/ab/cd。当用户同时输入 http://mydomain/ab/cdhttp://mydomain/ab/cd/ 时,如何让浏览器转到同一页面?

location /ab/cd/ {
}

你可以试试

location ~* ^/ab/cd(|\/) {...}

它是一个前缀匹配正则表达式,用于检查它是否有尾部斜线。

就性能而言,最快的只是两个 精确 位置:

location = /ab/cd {
   ...
}

location = /ab/cd/ {
   ...
}