(Nginx 位置正则表达式问题)需要匹配特定文件扩展名或没有扩展名的正则表达式
(Nginx location regex issue) Need regex that matches either specific file extensions OR no extension
我需要一个匹配特定文件扩展名(如 html、css、js)或不匹配扩展名的 Nginx 位置正则表达式。结尾的斜线应该无关紧要。例如,https://example.com/blah
、https://example.com/blah.css
、https://example.com/blah.css/
和 https://example.com/.hidden-dir/blah.css
都应该通过,但 https://example.com/blah.xx
和 https://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
}
我需要一个匹配特定文件扩展名(如 html、css、js)或不匹配扩展名的 Nginx 位置正则表达式。结尾的斜线应该无关紧要。例如,https://example.com/blah
、https://example.com/blah.css
、https://example.com/blah.css/
和 https://example.com/.hidden-dir/blah.css
都应该通过,但 https://example.com/blah.xx
和 https://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
}