How can I match any path containing a dot with nginx? How can I match any path containing a dot with nginx? nginx nginx

How can I match any path containing a dot with nginx?


Matching any dot in URI can be done with this:

location ~* \..*$ { # try_files $uri $uri/ =404; # ...}

Let's take the regex apart:

  • ~* Tells nginx to do case-insensitive regex matching (~ for case-sensitive)
  • \. Matches to literal dot symbol .
  • .* The dot here equals to any symbol except whitespace, asterisk modifies the preceding symbol to "match as many of this as possible"
  • $ Matches end of line

You can use this regex if you want to match even "malformed" uris like log.in

EDIT: In your situation, you would have to place this regex after your location ~* /static/* so it won't match uris with dots like /static/image.png. See notes for explanation.


Notes

Take in mind this location block will match any dot anywhere in passed URI. So it will match URIs like these /a.ssets/images/, /assets/favicon.ico too. Any non-terminating locations (ones without ^~ or =) will not be used even if they should match, if the dot regex matches and it's first matching regex location it takes precedence over anything else.

Important snippet from nginx's docs about matching preference of location:

Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching). To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.