I need a Nginx location regex that matches either specific file extensions (like html, css, js) OR no extension. A trailing slash should not matter. For example, https://example.com/blah
, https://example.com/blah.css
, https://example.com/blah.css/
, and https://example.com/.hidden-dir/blah.css
should all pass, but https://example.com/blah.xx
and https://example.com/blah.xx/
should fail. The path that it needs to traverse to get to the file can have any extension, because only the file itself matters.
(Nginx location regex issue) Need regex that matches either specific file extensions OR no extension
Asked
Active
Viewed 186 times
-1

Starscream512
- 103
- 7
-
Will you please show us what you tried? – virolino Oct 13 '20 at 06:51
1 Answers
0
This is what I came up with. It seems to work, minus the requirement that a trailing slash at the end shouldn't matter. If someone else can come up with a better solution, let me know.
# file extensions
location ~ [^\/]*\.[^\/]*$ {
location ~* (?<=\.html|\.css|\.js)$ {
# process request
}
return 404;
}
# the rest (i.e. no file extensions)
location / {
# process request
}

Starscream512
- 103
- 7