4

In Nginx, according to this question's answer

https://stackoverflow.com/questions/59846238/guide-on-how-to-use-regex-in-nginx-location-block-section

when using no modifier between the location directive and the URI it means that the URI must begin with the specified pattern. That would mean that the following location statement should match every single URI that begins with / but it only matches a URI that consists of nothing or /. Why is that?

location / {
   ...
}

Just to clarify, I'm not trying to find a way to match all URIs that start with /, I'm trying to understand what makes the above statement special and work differently than expected.

1
  • It does match (you can check the "access.log" to see for yourself), however, the reason which may have led you to think it's not working could be your server application which wasn't properly handling responding to those paths.
    – aderchox
    Commented Jul 13 at 12:21

1 Answer 1

3

The location block you included does not list a modifier. Therefore, it is just matching a location. It does match a specified pattern of /.

If no modifiers are present, the location is interpreted as a prefix match. This means that the location given will be matched against the beginning of the request URI to determine a match.

Via Digital Ocean: Understanding Nginx Server and Location Block Selection Algorithms

# --------------------------------------------------------------------------------------------------------------------------------------------
# Search-Order       Modifier       Description                                                        Match-Type        Stops-search-on-match
# --------------------------------------------------------------------------------------------------------------------------------------------
#     1st               =           The URI must match the specified pattern exactly                  Simple-string              Yes
#     2nd               ^~          The URI must begin with the specified pattern                     Simple-string              Yes
#     3rd             (None)        The URI must begin with the specified pattern                     Simple-string               No
#     4th               ~           The URI must be a case-sensitive match to the specified Rx      Perl-Compatible-Rx      Yes (first match)                 
#     4th               ~*          The URI must be a case-insensitive match to the specified Rx    Perl-Compatible-Rx      Yes (first match)
#     N/A               @           Defines a named location block.                                   Simple-string              Yes
# --------------------------------------------------------------------------------------------------------------------------------------------

So, in the context of a configuration file / would correspond to wherever you set the document root.

1
  • IMO this does not answer the main question. According to the Nginx' docs, the / location must match any locations, but it doesn't, even if there is a single location configured only.
    – aderchox
    Commented Jul 13 at 11:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.