Nginx是Igor Sysoev用C語言編寫的一個web服務器,一般用於負載均衡、反向代理和HTTP緩存等。Nginx用異步的事件驅動(event-driven)的方式來處理請求,所以負載能力很強。html
Nginx使用Block(如 server block
, location block
)來組成配置文件的層級結構,並在接收到客戶端請求以後根據請求的域名(domain name),端口(port),IP地址判斷處理該請求的server block
,而後根據請求的資源和URI決定處理該請求的location block
web
管理員能夠定義多個server block
做爲不相關的虛擬web服務器實體,而後經過listen
和server_name
決定處理請求的server block
正則表達式
listen
指令Nginx首先會檢查請求的IP地址和端口,並根據全部server block
創建一個列表來處理請求。每一個server block
中的listen
定義了這個server block
能處理的IP和端口(root用戶運行默認爲0.0.0.0:80,非root用戶運行的爲0.0.0.0:8080)緩存
listen
後能夠指定:服務器
IP:port
的IP地址和端口Unix socket
的路徑(在服務器間轉發請求的時候會用到)在將listen
的值與請求進行匹配以前,Nginx會先將listen
的值中所缺省的部分補充完整。而後將優先匹配準確的IP,若是不存在徹底準確匹配的IP纔會匹配到0.0.0.0
,若是有多個IP:port
匹配度相同,Nginx將會繼續檢查server_name
負載均衡
server_name
指令Nginx將server_name
與請求頭中的Host
進行匹配,匹配的順序:dom
優先選擇第一個精確匹配到的block。異步
server { listen 80; server_name host.example.com; ... }
選擇以*開頭的進行匹配,並優先選擇最長的。socket
server { listen 80; server_name *.example.com; ... }
選擇以*結尾的進行匹配,並優先選擇最長的。oop
server { listen 80; server_name www.example.*; ... }
選擇以~開頭的用正則表達式進行匹配,並優先選擇第一個。
server { listen 80; server_name ~^(www|host).*\.example\.com$; ... }
若是以上規則都沒法匹配,則選擇default_server
定義的默認的server_block
(每一個server_block
只能有一個default_server
),默認的default_server
是localhost
server { listen 80 default_server; server_name _; ... }
location block
是server block
的一部分,決定了如何處理請求的URI,格式:
location [modifier] location_match { ... }
modifier
是一個可選的參數,決定了如何解析後面的location match
,modifier
可選的值有:
(none)
前綴匹配, 如
location /site { ... }
將匹配以/site
開頭的URI
=(equal sign)
完整匹配,如
location = /page { ... }
將匹配/page
,而不會響應/page/index.html
的請求
~(tilde)
大小寫敏感的正則匹配, 如
location ~ \.(jpe?g|png|gif|ico)$ { ... }
將匹配以.jpg/.jpeg/.png/.gif/.ico
結尾的URI, 但不會響應.JPG
~*(tilde + asterisk)
大小寫無關的正則匹配, 如
location ~* \.(jpe?g|png|gif|ico)$ { ... }
.jpg
和.JPG
都會匹配
^~(carat + tilde)
非正則匹配,如
location ^~ /page { ... }
可以匹配/page/index.html
Nginx優先選擇正則表達式進行匹配,可是使用=
和^~
這兩個modifier
能夠覆蓋這一特性。排序對匹配過程也有必定的影響,由於Nginx在匹配到最長最精確的location以後就會中止匹配。
location_match
與請求的URI進行對比。modifier
爲=
的進行完整匹配。location_match
前綴進行匹配,若是modifier
爲^~
則匹配成功。index
語法:index file ...;
默認爲index index.html;
index
指令指定了被做爲index的文件,好比上面的index.html
可是在下面這種狀況下,對/index.html
的請求將會被第二個location block
處理,由於第一個與/index.html
並非徹底匹配。
location = / { index index.html; } location / { ... }
try_files
root /var/www/main; location / { try_files $uri $uri.html $uri/ /fallback/index.html; } location /fallback { root /var/www/another; }
對/page
的請求將會首先進入第一個location, 而後嘗試在/var/www/main
下依次查找page
, page.html
, page/
,若是都沒有找到的話將會被重定向到/fallback/index.html
,並由第二個location提供/var/www/another/fallback/index.html
rewrite
經過Perl兼容的正則表達式改變請求的URI,語法:rewrite regex replacement [flag];
flag的值能夠是:
結束當前的rewrite指令,並用修改過的URI去匹配其餘的location block
。
結束當前的rewrite指令。
當替換的URI(replacement
)不以 「http://」
, 「https://」
, 「$scheme」
開頭時進行狀態碼爲302的暫時性的重定向。
返回一個狀態碼爲301的永久重定向。
error_page
root /var/www/main; location / { error_page 404 /another/whoops.html; } location /another { root /var/www; }
除了/another
以外的請求都會在/var/www/main
查找請求的資源,若是沒有找到相關資源將會重定向到/another/whoops.html
,由第二個location block
處理,查找/var/www/another/whoops.html