Nginx配置詳情-2

http段的配置:

server{
    listen [端口]----監聽所有地址的指定端口;
        [IP地址:端口]---指定地址的指定端口
        [unix:socket]---指定的套接字文件上
        [default_server]:定義此server爲http中默認server,如果所有的
        server中沒有任何一個listen使用此參數,那麼第一個server及爲默認server

    rcvbuf=值:接受緩衝大小

    sndbuf=值:發送緩衝大小

    ssl:https server

    server_name  [主機名]可跟多個主機名,當nginx收到一個請求時,會取出其首部的
        Host的值,而後跟衆server_name進行比較,名稱中可以使用通配符!
        比較方式:
            1 先做精確匹配 -- www.dk.com
            2 左側通配符匹配 --*.dk.com
            3 右側通配符匹配 --www.*
            4 正則表達式匹配(通常以~開頭)
              ~^.*\.dk\.com  
            \d---表示[0--9]       
    &server_name_hash_bucket_size 32|64|128;      
    爲了實現快速主機查找,nginx使用hash表來保存主機名;

    root [定義web資源路徑]:
    用於指定請求的根文檔目錄

    bacjlog=n  排隊等候隊列

    tcp_nodelay on | off;  
    在keepalived模式下的連接是否啓用TCP_NODELAY選項  
    當爲off時,延遲發送,合併多個請求後再發送  
    默認On時,不延遲發送可用於:http,server,location

    sendfile on|off[默認off應該啓用]
    是否啓用sendfile功能,高效傳輸文件模式

    sendfile: 設置爲on表示啓動高效傳輸文件的模式。
    sendfile可以讓Nginx在傳輸文件時直接在磁盤和tcp socket之間傳輸數據。   
    如果這個參數不開啓,會先在用戶空間(Nginx進程空間)申請一個buffer,
    用read函數把數據從磁盤讀到cache,再從cache讀取到用戶空間的buffer,
    再用write函數把數據從用戶空間的buffer寫入到內核的buffer,
    最後到tcp socket。開啓這個參數後可以讓數據不用經過用戶buffer。

    server_tokens [on|off];
    是否在響應報文的Server首部顯示nginx版本 
    可以設置在http server location中使用

    location uri/@name {
     ...........
     }
    功能:允許根據用戶請求中URI來匹配指定的
    各location以進行訪問配置,匹配到時,將被
    location塊中的配置所處理

    =:做精確匹配
    ^~:只需要前半部分與uri匹配即可,不檢查正則表達式
    ~:做正則表達式模式匹配,匹配區分字符大小寫
    ~*:做正則表達式模式匹配。忽略大小寫
    不帶符號:匹配起始於此uri的所有的uri 

    匹配優先級:
        1 字符字面量--字符精確匹配
        2 正則表達式檢索,當有多個時,將被第一個匹配
          到的處理!
        3 按字符字面量,從左開始
    注意:
    每個location中的root可以不一樣!
    location中root的路徑優先級要高於主配置段中的root

    alias [路徑別名],只能用於location中

    index file....:定義默認頁面
    可以跟多個值,自左而右進行匹配

    error_page code ...[=[response]] uri;
    當對於某個請求返回錯誤時,如果匹配上了error_page指令中的設定的code
    具體應用:
    error_page 404 /404.html
    如果想讓返回碼更改 使用 404 =200
    http|server|location

    try_files path1 path2 uri; 
    自左至右嘗試讀取由path所指定路徑,在第一次找到即停止並返回,如果所有path
    均不存在,則返回最後一個uri
    具體使用:
    location /images/ {      
        try_files $uri /images/default.gif <--訪問的uri不存在就返回這個頁面;   
    }
}

alias使用案例:
實驗案例:確定資源訪問目錄
 1 location / {
        root /www/html;
        index index.html;

 }
 2 location ^~ /images/ {
           root /web;
    資源位置:/web/images/*            
       }
 3 location ^~ /images/<--有斜線 {
           alias /web/<--必須要有斜線
 }
    資源位置:/web/*

下面我們就通過具體操作來展示用法:

1 基本虛擬機主機選項:
[root@www20:11:51conf.d]#cat www.conf 
server {
    server_name www.a.com;
    listen 80;
    root /web/a.com;
    index index.html;
    server_tokens off;

    location /test {
        root /www/html;
    }
}
訪問測試:
[root@www20:12:47conf.d]#curl http://www.a.com
<h1>test a.com page for nginx virt</h1>
[root@www20:13:27conf.d]#curl http://www.a.com/test/
<h1>test location page for nginx</h1>

查看報文頭部版本信息:
[root@www20:14:32conf.d]#curl -I 172.20.23.48
HTTP/1.1 200 OK
Server: nginx/1.12.2 <--帶有版本信息
[root@www20:13:51conf.d]#curl -I http://www.a.com/test/
HTTP/1.1 200 OK
Server: nginx <---沒有版本信息

路徑別名:

location /test1 {
    alias /mydata/html;
}
訪問測試:
[root@www20:20:49nginx]#curl www.a.com/test1/
<h1>test alias for nginx</h1>

此時網頁文件所在位置:
[root@www20:22:00html]#pwd
/mydata/html
[root@www20:22:05html]#cat index.html 
<h1>test alias for nginx</h1>
注意:
此配置只能用於location中

錯誤頁面重定向:

server {
    server_name www.c.com;
    listen 80;
    index index.html;
    root /web/c.com;
    error_page 404 /404.html;
}

測試訪問不存在頁面:
[root@www20:29:01c.com]#curl www.c.com/dsadsa
<h1>error page for nginx c.com<h1>

更改錯誤響應碼:
server {
    server_name www.c.com;
    listen 80;
    index index.html;
    root /web/c.com;
    error_page 404 =302 /404.html;
}

測試:
[root@www20:30:37c.com]#curl -I www.c.com/oosdosdos
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.12.2
Date: Thu, 08 Nov 2018 12:31:54 GMT
Content-Type: text/html
Content-Length: 40
Connection: keep-alive
ETag: "5be42c0d-28"

設置請求內容不存在時默認返回頁面:


server {
    server_name www.c.com;
    listen 80;
    index index.html;
    root /web/c.com;
    error_page 404 =302 /404.html;
    location /test2/ {
        try_files $uri /test2/test.html;
    }
}

$uri --nginx內置變量獲取你請求的uri資源

訪問存在頁面:
[root@www20:41:53test2]#curl www.c.com/test2/index.html
<h1>hello world</h1>
訪問不存在頁面:
[root@www20:42:06test2]#curl www.c.com/test2/sdsd.html
<h1>test try_files for nginx at www.c.com</h1>

location /test2/ {
    try_files $uri /test2/test.html;
    這裏是不可以寫成別的路徑,默認返回頁面一定
    要在test2目錄下
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章