(Nginx位置regex问题)需要匹配特定文件扩展名或无扩展名的regex



我需要一个Nginx位置regex,它可以匹配特定的文件扩展名(如html、css、js(,也可以不匹配扩展名。尾部斜杠应该无关紧要。例如,https://example.com/blahhttps://example.com/blah.csshttps://example.com/blah.css/https://example.com/.hidden-dir/blah.css应全部通过,但https://example.com/blah.xxhttps://example.com/blah.xx/应失败。它需要遍历才能到达文件的路径可以有任何扩展名,因为只有文件本身才重要。

这就是我想到的。它似乎是有效的,减去了结尾的斜杠不重要的要求。如果其他人能想出更好的解决方案,请告诉我。

# file extensions
location ~ [^/]*.[^/]*$ {
location ~* (?<=.html|.css|.js)$ {
# process request
}
return 404;
}
# the rest (i.e. no file extensions)
location / {
# process request
}

最新更新