-1

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.

1 Answers1

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
}