Nginx配置淺析

Nginx是Igor Sysoev用C語言編寫的一個web服務器,一般用於負載均衡、反向代理和HTTP緩存等。Nginx用異步的事件驅動(event-driven)的方式來處理請求,所以負載能力很強。html

Nginx使用Block(如 server block, location block)來組成配置文件的層級結構,並在接收到客戶端請求以後根據請求的域名(domain name)端口(port)IP地址判斷處理該請求的server block,而後根據請求的資源URI決定處理該請求的location blockweb

Server Block

管理員能夠定義多個server block做爲不相關的虛擬web服務器實體,而後經過listenserver_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後能夠指定:服務器

  1. IP:port的IP地址和端口
  2. 僅IP(端口將默認爲80)
  3. 僅port,將監聽全部接口的這個port
  4. 到某個Unix socket的路徑(在服務器間轉發請求的時候會用到)

在將listen的值與請求進行匹配以前,Nginx會先將listen的值中所缺省的部分補充完整。而後將優先匹配準確的IP,若是不存在徹底準確匹配的IP纔會匹配到0.0.0.0,若是有多個IP:port匹配度相同,Nginx將會繼續檢查server_name負載均衡

server_name指令

Nginx將server_name與請求頭中的Host進行匹配,匹配的順序:dom

  1. 優先選擇第一個精確匹配到的block。異步

    server {
        listen 80;
        server_name host.example.com;
        ...
    }
  2. 選擇以*開頭的進行匹配,並優先選擇最長的。socket

    server {
        listen 80;
        server_name *.example.com;
        ...
    }
  3. 選擇以*結尾的進行匹配,並優先選擇最長的。oop

    server {
        listen 80;
        server_name www.example.*;
        ...
    }
  4. 選擇以~開頭的用正則表達式進行匹配,並優先選擇第一個。

    server {
        listen 80;
        server_name ~^(www|host).*\.example\.com$;
        ...
    }
  1. 若是以上規則都沒法匹配,則選擇default_server定義的默認的server_block(每一個server_block只能有一個default_server),默認的default_serverlocalhost

    server {
        listen 80 default_server;
        server_name _;
        ...
    }

Location Block

location blockserver block的一部分,決定了如何處理請求的URI,格式:

location [modifier] location_match {
    ...
}

modifier

modifier是一個可選的參數,決定了如何解析後面的location matchmodifier可選的值有:

  1. (none)

    前綴匹配, 如

    location /site {
        ...
    }

    將匹配以/site開頭的URI

  2. =(equal sign)

    完整匹配,如

    location = /page {
        ...
    }

    將匹配/page,而不會響應/page/index.html的請求

  3. ~(tilde)

    大小寫敏感的正則匹配, 如

    location ~ \.(jpe?g|png|gif|ico)$ {
    ...
    }

    將匹配以.jpg/.jpeg/.png/.gif/.ico結尾的URI, 但不會響應.JPG

  4. ~*(tilde + asterisk)

    大小寫無關的正則匹配, 如

    location ~* \.(jpe?g|png|gif|ico)$ {
        ...
    }

    .jpg.JPG都會匹配

  5. ^~(carat + tilde)

    非正則匹配,如

    location ^~ /page {
        ...
    }

    可以匹配/page/index.html

匹配順序

Nginx優先選擇正則表達式進行匹配,可是使用=^~這兩個modifier能夠覆蓋這一特性。排序對匹配過程也有必定的影響,由於Nginx在匹配到最長最精確的location以後就會中止匹配。

  1. 將全部非正則表達式的location_match與請求的URI進行對比。
  2. modifier=的進行完整匹配。
  3. 選擇最長location_match前綴進行匹配,若是modifier^~則匹配成功。
  4. 進行正則表達式匹配
  5. 用其餘前綴匹配

其餘指令

  1. index

    語法:index file ...; 默認爲index index.html;

    index指令指定了被做爲index的文件,好比上面的index.html

    可是在下面這種狀況下,對/index.html的請求將會被第二個location block處理,由於第一個與/index.html並非徹底匹配。

    location = / {
        index index.html;
    }
    
    location / {
        ...
    }
  2. 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

  3. rewrite

    經過Perl兼容的正則表達式改變請求的URI,語法:rewrite regex replacement [flag];

    flag的值能夠是:

    • last

    結束當前的rewrite指令,並用修改過的URI去匹配其餘的location block

    • break

    結束當前的rewrite指令。

    • redirect

    當替換的URI(replacement)不以 「http://」, 「https://」, 「$scheme」開頭時進行狀態碼爲302的暫時性的重定向。

    • permanent

    返回一個狀態碼爲301的永久重定向。

  4. 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

相關文章
相關標籤/搜索