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