Skip to content

Nginx

Location matching

Locations are matched against the normalized URI ($uri), with %-encoded bytes decoded, . and .. resolved, and consecutive forward slashes merged.

There are three kinds of locations, matched in this order:

  1. Exact match: location = /. This has the highest priority.
  2. Prefix location: This comes in two forms:

    • location /prefix
    • location ^~ /prefix (note that this is not a regex)

    During matching, these two forms are treated equally, and the longest prefix wins.

  3. Regex location: This also comes in two forms:

    • location ~ regex (case-sensitive)
    • location ~* regex (case-insensitive)

    Regexes are matched in the order they appear in the config file, and the first match wins.

    Regex location has higher priority than prefix locations, but only when the matched (i.e. longest) prefix does not have the ^~ modifier (think it as a flag).

A clearer process for matching would be like:

  1. Check for exact matches (location = /). If one is found, the matching stops here.
  2. Find the longest prefix location (location / and location ^~ /)
  3. If the longest prefix location has the ^~ flag, the matching stops here.
  4. Match all regex locations. If one regex matches, use it. If no regex matches, use the prefix location from step 2.

See also:

Relative directory handling

A few directives that specify a path may accept relative paths. For example, you can include another file in your configuration. Documentation on how relative paths are resolved is scarce.

  • include and ssl_certificate directives are relative to the main nginx.conf file.
  • root directives are relative to the "prefix". Use nginx -V to see its compiled prefix path.

In general, if the directive specifies a file for Nginx to use for itself, it will be relative to the main nginx.conf file. If the directive specifies a directory for Nginx to serve files to HTTP clients, it will be relative to the prefix path.

One notable exception is load_module, which, according to testing, is relative to the prefix directory.

Configurations

multi_accept

Just leave it off unless your server is handling millions of connections per second.

Reference: Why is 'multi_accept' 'off' as default in Nginx?