location 有”定位”的意思, 根据Uri来进行不一样的定位. 在虚拟主机的配置中,是必不可少的,location能够把网站的不一样部分,定位到不一样的处理方式上.php
好比, 碰到.php, 如何调用PHP解释器? --这时就须要locationhtml
location [=|~|~*|^~] patt { }
- 中括号能够不写任何参数,此时称为通常匹配 - 也能够写参数
根据参数的特性,能够大体分为如下三种:java
location = patt {} [精准匹配] location patt {} [通常匹配] location ~ patt {} [正则匹配]
location /{ root /var/www/html/; index index.htm index.html; }
注意:/ 后能够跟参数,记录最长的匹配结果。nginx
location = /{ root /var/www/html/; index index.htm index.html; }
注意:/ 后能够跟参数,匹配成功后直接返回精确结果。网站
location ~ /image{ root /var/www/html/; index index.htm index.html; }
注意:/ 后能够跟参数,优先级略低于等值匹配,任一正则命中,返回正则命中结果,并中止匹配。注意:正则匹配会在root目录后加上image, 即若是访问:http://localhost/image/mm.jpg, 则访问的是/var/www/html/image/mm.jpgurl
代码以下:code
location = /{ root /var/www/html/; index index.htm index.html; } location / { root html; index index.html index.htm; }
上述配置后访问:http://localhost/,注意其它location配置先不要写,以避免影响结果。server
所获得的访问文件是:/nginx/html/index.htm 或者 /nginx/html/index.html ;htm
先进行等值匹配,由于访问的url是一个“/”,是个目录,因此nginx内部会发送一次请求,访问:http://localhost/index.htm (若访问不到,会访问第二个文件index.html);get
内部访问不会再走这个已经匹配了的等值location,会访问默认目录,在默认目录 /nginx/html 下查找 index.htm文件;
nginx官网server配置:https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/#a-default-catch-all-server-block