Table of contents
Location syntax
- Exact Match: = uri
- Preferential prefix match: ^~ uri
- Regex match: ~* uri
- Prefix match: uri
Inheritance & directive types
- Standard directive
- Array directive
- Action directive
server {
root /site/demo;
location {
root /site/demo; #inherited root
}
}
Prefix match
without location
=
server {
listen 80;
server_name 198.255.25.1;
root /sites/demo;
location /any-path {
return 200 "hello from nginx";
# return 307 'https://anything.example.com/anything';
# determine how NGINX wants to respond when request coming to specific path.
}
}
Exact match
defined with
location =
server {
listen 80;
server_name 198.255.25.1;
root /sites/demo;
location = /any-path {
return 200 "hello from nginx"
}
}
Regex match - case sensitive
server {
listen 80;
server_name 198.255.25.1;
root /sites/demo;
location ~ /any-path[0-9] {
# ~ for 'any-path' + a number between 0 to 9
# ~# for case insensitive
# ^~ preferential prefix match
return 200 "hello from nginx"
}
}