# /etc/Nginx/Nginx.conf
events {} # event context have to be defined to consider config valid
http {
server {
listen 80;
server_name lidihuo.co www.lidihuo.co *.lidihuo.co;
return 200 "Hello";
}
}
server {
listen 80;
server_name lidihuo.co;
root /var/www/lidihuo.co;
}
lidihuo.co:80/index.html # returns /var/www/lidihuo.com/index.html
lidihuo.co:80/foo/index.html # returns /var/www/lidihuo.com/foo/index.html
location [modifier] path
location /foo {
# ...
}
/foo
/fooo
/foo123
/foo/bar/index.html
...
server {
listen 80;
server_name lidihuo.co;
root /var/www/lidihuo.co;
location / {
return 200 "root";
}
location /foo {
return 200 "foo";
}
}
lidihuo.co:80 / # => "root"
lidihuo.co:80 /foo # => "foo"
lidihuo.co:80 /foo123 # => "foo"
lidihuo.co:80 /bar # => "root"
= -Exact match
^~ -Preferential match
~ && ~* -Regex match
no modifier-Prefix match
location /match {
return 200 'Prefix match: will match everything that starting with /match';
}
location ~* /match[0-9] {
return 200 'case insensitive regex match';
}
location ~ /MATCH[0-9] {
return 200 'case sensitive regex match';
}
location ^~ /match0 {
return 200 'Preferential match';
}
location = /match {
return 200 'Exact match';
}
/match # => 'Exact match'
/match0 # => 'Preferential match'
/match1 # => 'case insensitive regex match'
/MATCH1 # => 'case sensitive regex match'
/match-abc # => 'Prefix match: matches everything that starting with /match'
try_files $uri index.html =404;
$uri(/foo.html);
index.html
如果没有找到: 404
server {
try_files $uri /index.html =404;
location / {
}
}
server {
location / {
try_files $uri /index.html =404;
}
}