LAOX PowerTech
The LAOX News provides the latest information.
en ja zh

Introduction to nginx configuration (location)

2023-10-04

The location block is defined within the server block.
A basic location block looks like this

location / {    
    root /data/www; }    
}    

Format

location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }

prefix meaning
= none forward matching
= exact match
~ regular expression (case sensitive)
~* regular expression (case insensitive)
^~ regular expression is not evaluated after a match

Note that no prefix is only a forward match.

location /foo { ... }

In the above case,

/foo = match of course
/foo/ = match
/foobar = match also

If you want to match only /foo,

location = /foo { ... }

to match only /foo, use the = equal prefix as in location = /foo { ... }.
In the above case /foo/ will also be mismatched.

This is often seen in PHP processing

location ~ \.php$ { ... }

is the prefix of the ~ tilde, so the URL part is case-sensitive in regular expressions.
The ~ tilde prefix in the URL part is case-sensitive. is an escape to treat it as a period character itself, not as a regular expression period (any single character). . is escaped to treat it as a period character itself, not a period (an arbitrary character) as a regular expression. The $ inphp$` indicates the end of a regular expression, so it will not match, for example, phpx.
Taken together, this means that any URL ending in .php will match this location block.

Recommended articles