淺談Nginx(二)—http下server配置

淺談Nginx(二)—http下server配置

此文介紹Nginx下的http模塊,着重介紹http模塊下的server服務

--------依據”馬哥教育”主講人馬永亮導師的上課筆記整理-------

目錄
 一. http相關的基本配置:
    1) listen           
    2) server_name      
    3) tcp_nodelay
    4) sendfile     
二. 定義路徑相關的配置:
    1)root path
    2)location
    3)alias path
    4)error_page      
    5)try_files
三. 定義客戶端請求的相關配置:
    1)keepalive_timeout
    2)keepalive_requests
    3)keepalive_disable
    4)send_timeout
    5)client_body_buffer_size
    6)client_body_temp_path
四. 對客戶端進行限制的相關配置:
    1) limit_rate
    2) limit_except
五. 文件操作優化的配置:
    1)aio
    2)directio
    3)open_file_cache
    4)open_file_cache_valid
    5)open_file_cache_min_uses
    6)open_file_cache_errors
六. 一些常用的模塊(非core模塊)
    1)ngx_http_access_module模塊
    2)ngx_http_auth_basic_module模塊
    3)ngx_http_stub_status_module模塊
    4)ngx_http_log_module模塊
    5)ngx_http_gzip_module模塊
    6)ngx_http_ssl_module模塊
    7)ngx_http_rewrite_module模塊
    8)ngx_http_referer_module模塊
    9)ngx_http_proxy_module模塊
    10)proxy_set_header field value模塊
    11)ngx_http_headers_module模塊
    12)ngx_http_fastcgi_module模塊

一、http模塊的基本配置格式

            http {
                ... ...
                server {
                    ...
                    server_name
                    root
                    location [OPERATOR] /uri/ {
                        ...
                    }
                }
                server {
                    ...
                }
            }

1.1 server{.......} :

http模塊下可有多個虛擬主機,每個主機定義在單獨的一個server配置段內;
1.1.1 server段的基本配置:
            server {
                        listen address[:PORT]|PORT;
                        server_name SERVER_NAME;
                        root /PATH/TO/DOCUMENT_ROOT;
                    }
        ----------------------------

    1) listen PORT | unix:/PATH/TO/SOCKET_FILE         #設置監聽端口
    2) listen address[:port] [default_server] [ssl] [http2 | spdy] 
        [backlog=number] [rcvbuf=size] [sndbuf=size]   #設置監聽地址

            解釋:
                default_server:設定爲默認虛擬主機;
                ssl:限制僅能夠通過ssl連接提供服務;
                backlog=number:後援隊列長度;
                rcvbuf=size:接收緩衝區大小;
                sndbuf=size:發送緩衝區大小;
            實例:
                server {
                        listen 80;
                        listen 172.16.33.117:8080;
                    }

    *3) server_name  設置主機名稱;
            1)指明虛擬主機的主機名稱;後可跟多個由空白字符分隔的字符串;
            2)支持*通配任意長度的任意字符;server_name   *.magedu.com  www.magedu.*
            3)支持~起始的字符做正則表達式模式匹配;server_name    ~^www\d+\.magedu\.com$
                匹配機制:
                    (1) 首先是字符串精確匹配;
                    (2) 左側*通配符;
                    (3) 右側*通配符;
                    (4) 正則表達式;
            實例:
                server {
                        user_name  www.magedu33.com
                    }

    4) tcp_nodelay on | off;
        在keepalived模式下的連接是否啓用TCP_NODELAY選項;

    5) sendfile on | off;
        是否啓用sendfile功能

二. 定義路徑相關的配置

    *1) root path
        設置web資源路徑映射;用於指明用戶請求的url所對應的本地文件系統上的文檔所在目
        錄路徑;可用的位置:http, server, location, if in location

    *2) location [ = | ~ | ~* | ^~ ] uri { ... }
                location @name { ... }
            解釋:
                在一個server中location配置段可存在多個,用於實現從uri到文件系統的路徑映
            射;ngnix會根據用戶請求的URI來檢查定義的所有location,並找出一個最佳匹配,而
            後應用其配置;
                如在網頁中輸入你想訪問的地址:www.magedu.com/picture,這個uri是“picture”,
            nginx的http模塊接收到此uri後,尋找與“/picture”最匹配的location,然後執行此location
            下內容,並返回給用戶


            =:對URI做精確匹配;
            ~:對URI做正則表達式模式匹配,區分字符大小寫;
            ~*:對URI做正則表達式模式匹配,不區分字符大小寫;
            ^~:對URI的左半部分做匹配檢查,不區分字符大小寫,一般用來匹配目錄;
            不帶符號:匹配起始於此uri的所有的url;

            匹配優先級:=, ^~, ~/~*,不帶符號;

            例:
                location  = / {
                    # 只匹配"/".
                [ configuration A ] 
                }

                location  / {
                # 匹配任何請求,因爲所有請求都是以"/"開始
                # 但是更長字符匹配或者正則表達式匹配會優先匹配
                [ configuration B ] 
                }

                location ^~ /p_w_picpaths/ {
                # 匹配任何以 /p_w_picpaths/ 開始的請求,並停止匹配 其它location
                [ configuration C ] 
                }

                location ~* \.(gif|jpg|jpeg)$ {
                # 匹配以 gif, jpg, or jpeg結尾的請求,不區分大小寫. 
                # 但是所有 /p_w_picpaths/ 目錄的請求將由 [Configuration C]處理.   
                [ configuration D ] 
                }


    *3) alias path;
        定義路徑別名,文檔映射的另一種機制;僅能用於location上下文;

        注意:location中使用root指令和alias指令的意義不同;
                (a) root,給定的路徑對應於location中的/uri/左側的/;
                (b) alias,給定的路徑對應於location中的/uri/右側的/;
        例:
            server {
                    location /admin/{
                        alias /data/heml/;
                    }
            }

    *4) error_page code ... [=[response]] uri;     #定義錯誤頁面

        例:
            server {
                    error_page  404   /404.html;  
                     #  error_page 404 =200 /404.html;此設置是錯誤項改爲200,欺騙作用.
                    location = /404.html{
                        root "自定義錯誤頁面存放路徑"
                    }
            }

三. 定義客戶端請求的相關配置

    1) keepalive_timeout timeout [header_timeout];
        設定保持連接的超時時長,0表示禁止長連接;默認爲75s;

    2) eepalive_requests number;
        在一次長連接上所允許請求的資源的最大數量,默認爲100;

    3) keepalive_disable none | browser ...;
        對哪種瀏覽器禁用長連接;

    4) send_timeout time;
        向客戶端發送響應報文的超時時長,此處,是指兩次寫操作之間的間隔時長;

    5) client_body_buffer_size size;
        用於接收客戶端請求報文的body部分的緩衝區大小;默認爲16k;超出此大小時,
        其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置;

    6) client_body_temp_path path [level1 [level2 [level3]]];
        設定用於存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量;

            例:
            client_body_temp_path path  /var/tmp/client_body  1 2 2

四. 對客戶端進行限制的相關配置

    1)limit_rate rate;
        限制響應給客戶端的傳輸速率,單位是bytes/second,0表示無限制;

    2)limit_except method ... { ... }
        限制對指定的請求方法之外的其它方法的使用客戶端;

            例:除了192.168.1.0/24以外的主機禁止訪問     
                limit_except GET {
                        allow 192.168.1.0/24;
                        deny  all;
                }

五. 文件操作優化的配置

    1) aio on | off | threads[=pool];        # aio 異步IO,內核把內存中的數據做緩存,過段時間後在把他們寫到磁盤,這就叫異步IO
        是否啓用異步IO功能;

    2) directio size | off;             # 直接IO, 內核直接寫數據到磁盤,不過效率比較低
        在Linux主機啓用O_DIRECT標記,此處意味文件大於等於給定的大小時使用,例如directio 4m;

    3) open_file_cache off;         #可加速訪問速率,使用內存空間;可緩存否定答案,非權威答案
        open_file_cache max=N [inactive=time];     # 定義可定義緩存上限
        nginx可以緩存以下三種信息:
            (1) 文件的描述符、文件大小和最近一次的修改時間;
            (2) 打開的目錄結構;檢測目錄存在與否
            (3) 沒有找到的或者沒有權限訪問的文件的相關信息;

            max=N:可緩存的緩存項上限;達到上限後會使用LRU算法實現緩存管理;

            inactive=time:緩存項的非活動時長,在此處指定的時長內未被命中的或命中的次 
            數少於open_file_cache_min_uses指令所指定的次數的緩存項即爲非活動項;

    4) open_file_cache_valid time;
            緩存項有效性的檢查頻率;默認爲60s; 每隔60s檢測一次緩存項是否小於給定值

    5) open_file_cache_min_uses number;
            在open_file_cache指令的inactive參數指定的時長內,至少應該被命中多少次方可被
            歸類爲活動項;

    6) open_file_cache_errors on | off;      # 緩存否定答案的值
            是否緩存查找時發生錯誤的文件一類的信息;

六、一些常用模塊(非core模塊)

6.1 ngx_http_access_module模塊

作用: 基於ip的訪問控制功能

    1) allow address | CIDR | unix: | all;
    2) deny address | CIDR | unix: | all;
        例:
            location /admin{
                deny 192.168.0.1;       # 拒絕192.168.0.1訪問
                allow 192.168.0.0/24;   # 允許192.168.0.0/24訪問,除了上一行定義的192.168.0.1
                allow 10.1.1.0/16;      # 允許10.1.1.0/16這一網段中的主機訪問
                deny all                # 除了已定義規則外,所有主機不可訪問,默認策略
            }
        注:規則時自上而下檢查,定義規則時,範圍小的應該放在上面,範圍大的放下面

6.2 ngx_http_auth_basic_module模塊

作用:實現基於用戶的訪問控制,使用basic機制進行用戶認證

    1)auth_basic string | off;          # 提供登錄時的提示頁面
    2)auth_basic_user_file file;        # 用戶登錄文件認證

        例:      
        location /admin/ {
                alias /webapps/app1/data/;
                auth_basic "Admin Area";
                auth_basic_user_file /etc/nginx/.ngxpasswd;
        }

        ps:htpasswd命令由httpd-tools所提供;htpasswd生成一個加密賬戶文件,存放至一個目錄

6.3 ngx_http_stub_status_module模塊

作用:用於輸出nginx的基本狀態信息

    訪問網頁後加uri “/basic_status”s時出現的內容:
        Active connections: 291                         #活動鏈接數
        server accepts handled requests
        16630948 16630948 31070465 
        Reading: 6 Writing: 179 Waiting: 106

        解釋:     
            Active connections: 活動狀態的連接數;
            accepts:已經接受的客戶端請求的總數;
            handled:已經處理完成的客戶端請求的總數;
            requests:客戶端發來的總的請求數;
            Reading:處於讀取客戶端請求報文首部的連接的連接數;
            Writing:處於向客戶端發送響應報文過程中的連接數;
            Waiting:處於等待客戶端發出請求的空閒連接數;
        例:
            location   /basic_status {
                    stub_status;
            }

6.4 ngx_http_log_module模塊 (重要)

作用:nginx的以指定的格式保存訪問日誌,利於分析用戶習慣

    log_format name string ...;

    string可以使用nginx核心模塊及其它模塊內嵌的變量;
    1)access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
        access_log off;

        訪問日誌文件路徑,格式及相關的緩衝的配置;
            buffer=size
            flush=time

    2)open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
        open_log_file_cache off;
        緩存各日誌文件相關的元數據信息;

            max:緩存的最大文件描述符數量;
            min_uses:在inactive指定的時長內訪問大於等於此值方可被當作活動項;
            inactive:非活動時長;
            valid:驗正緩存中各緩存項是否爲活動項的時間間隔;

            例:
                http {

                    log_format main  '$remote_addr - $remote_user [time_local] "request"';
                                     '$status $body_bytes_sent "$http_referer"';
                                     ' "$http_user_agent" "$http_x_forwarded_for" ';

                    access_log  /var/log/nginx/access.log   main;
                    open_log_file_cache max=100 inactive=120s min_uses=2 valid=120s;
                }

6.5 ngx_http_gzip_module模塊

作用:壓縮內容

    1)gzip on | off;
        啓用或禁用“gzip”功能;

    2)gzip_comp_level level;
        壓縮級別,範圍從1-9;

    3)gzip_disable regex ...;
        對哪些瀏覽器禁用功能
        Disables gzipping of responses for requests with “User-Agent” header fields matching any of the specified regular expressions.

    4)gzip_min_length length;
        啓用壓縮功能的響應報文大小閾值; 如:文件至少10K在執行壓縮

    5)gzip_buffers number size;
        支持實現壓縮功能時爲其配置的緩衝區數量及每個緩存區的大小;

    6)gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;
        nginx作爲代理服務器接收到從被代理服務器發送的響應報文後,在何種條件下啓用壓縮功能的;
        off:對代理的請求不啓用
        no-cache, no-store,private:表示從被代理服務器收到的響應報文首部的Cache-Control的值爲此三者中任何一個,則啓用壓縮功能;

    7)gzip_types mime-type ...;
        壓縮過濾器,僅對此處設定的MIME類型的內容啓用壓縮功能;

        示例:
            gzip  on;                   # 啓用壓縮功能
            gzip_comp_level 6;          # 定義壓縮比,數值越大,壓縮越高
            gzip_min_length 64;         # 至少64個字節才壓縮
            gzip_proxied any;           # 任何代理請求都執行壓縮功能
            gzip_types text/xml text/css  application/javascript;           # 僅對何種資源做壓縮

6.6 ngx_http_ssl_module模塊

作用:使得網址使用加密傳輸,即https

    1)ssl on | off;
        Enables the HTTPS protocol for the given virtual server.

    2)ssl_certificate file;
        當前虛擬主機使用PEM格式的證書文件;

    3)ssl_certificate_key file;
        當前虛擬主機上與其證書匹配的私鑰文件;

    4)ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];
        支持ssl協議版本,默認爲後三個;

    5)ssl_session_cache off | none | [builtin[:size]] [shared:name:size];
        builtin[:size]:使用OpenSSL內建的緩存,此緩存爲每worker進程私有;

        [shared:name:size]:在各worker之間使用一個共享的緩存;

    6)ssl_session_timeout time;
        客戶端一側的連接可以複用ssl session cache中緩存 的ssl參數的有效時長;

        配置示例:
                server {
                    listen 443 ssl;
                    server_name www.magedu33.com;
                    root /vhosts/ssl/htdocs;
                    ssl on;         # 啓用ssl功能
                    ssl_certificate /etc/nginx/ssl/nginx.crt;           # 證書路徑
                    ssl_certificate_key /etc/nginx/ssl/nginx.key;       # 私鑰路徑
                    ssl_session_cache shared:sslcache:20m;      # 會話緩存時間
                }

6.7 ngx_http_rewrite_module

作用:網頁重載,當你輸入一個地址如www.magedu.com訪問時,nginx服務可以通過此模塊功能,將網址重載成www.magedu.com/bbs/,訪問的內容也會指向www.magedu.com/bbs/

    1)rewrite regex replacement [flag]
        將用戶請求的URI基於regex所描述的模式進行檢查,匹配到時將其替換爲replacement指定的新的URI;

        注意:     
            如果在同一級配置塊中存在多個rewrite規則,那麼會自下而下逐個檢查;被某條件
            規則替換完成後,會重新一輪的替換檢查,因此,隱含有循環機制;[flag]所表示
            的標誌位用於控制此循環機制;

            如果replacement是以http://或https://開頭,則替換結果會直接以重向返回給客戶端;

        [flag]:
            last:重寫完成後停止對當前URI在當前location中後續的其它重寫操作,而後對新的URI啓動新一輪重寫檢查;提前重啓新一輪循環; 
            break:重寫完成後停止對當前URI在當前location中後續的其它重寫操作,而後直接跳轉至重寫規則配置塊之後的其它配置;結束循環;
            redirect:重寫完成後以臨時重定向方式直接返回重寫後生成的新URI給客戶端,由客戶端重新發起請求;不能以http://或https://開頭;
            permanent:重寫完成後以永久重定向方式直接返回重寫後生成的新URI給客戶端,由客戶端重新發起請求;

    2)return
        return code [text];
        return code URL;
        return URL;

        Stops processing and returns the specified code to a client. 
        中止此次請求,並返回一個錯誤頁;

    3)rewrite_log on | off;
        是否開啓重寫日誌;

    4)if (condition) { ... }
        引入一個新的配置上下文 ;條件滿足時,執行配置塊中的配置指令;server, location;

        condition:
            比較操作符:
                ==
                !=
                ~:模式匹配,區分字符大小寫;
                ~*:模式匹配,不區分字符大小寫;
                !~:模式不匹配,區分字符大小寫;
                !~*:模式不匹配,不區分字符大小寫;
            文件及目錄存在性判斷:
                -e, !-e
                -f, !-f
                -d, !-d
                -x, !-x

    5)set $variable value;
        用戶自定義變量 ;

        例1:
            location /bbs/ {
                    rewrite  ^/bbs/(.*)$   /forum/$1 break;            #用戶請求訪問“bbs”時,此命令把它替換成/forum/開頭,後面接任意字符(任意字符和原bbs後字符一致)
                    return 403;     #返回狀態值200
            }

        例2:
            if ($http_user_agent ~ MSIE){
                rewite ^(.*)$ /msie/$1 break;       #判斷模式是否是IE瀏覽器,匹配則把原地址改成以"/msie/"開頭的字符
            }

6.8 ngx_http_referer_module模塊

作用:限定引用參考功能,用戶請求報文的值是一個非法引用,可以通過此模塊禁用該請求。即可定義合法引用。

    1)valid_referers none | blocked | server_names | string ...;
        定義referer首部的合法可用值;

        none:請求報文首部沒有referer首部;
        blocked:請求報文的referer首部沒有值;
        server_names:參數,其可以有值作爲主機名或主機名模式;
        arbitrary_string:直接字符串,但可使用*作通配符;
        regular expression:被指定的正則表達式模式匹配到的字符串;要使用~打頭,例如 ~.*\.magedu\.com;

        配置示例:

            valid_referers none block server_names *.magedu.com *.mageedu.com magedu.* mageedu.* ~\.magedu\.;               # 定義關鍵字

            if($invalid_referer) {          # 如果請求的值不在關鍵字內,就拒絕訪問,並返回值403,並跳轉至百度(可設成任意網址)
                return 403 www.baidu.com;
            }

6.9 ngx_http_proxy_module模塊

作用:代理模塊,可以讓nginx具有代理功能,可以正向代理,也可反向代理;
官方定義:The ngx_http_proxy_module module allows passing requests to another server.用於實現把一個請求轉發給其他服務器;

        1)proxy_pass URL;       # 完成URL映射
            Context:    location, if in location, limit_except

            注意:proxy_pass後面的路徑不帶uri時,其會將location的uri傳遞給後端主機;

                例:              
                server {
                    ...
                    server_name HOSTNAME;
                    location /uri/ {
                        proxy http://hos[:port];
                    }
                    ...
                }

                http://HOSTNAME/uri --> http://host/uri

       ======================================================================       
            proxy_pass後面的路徑是一個uri時,其會將location的uri替換爲proxy_pass的uri;

                server {
                    ...
                    server_name HOSTNAME;
                    location /uri/ {
                        proxy http://host/new_uri/;
                    }
                    ...
                }

                http://HOSTNAME/uri/ --> http://host/new_uri/
       =====================================================================


    ===============================================================                 
            如果location定義其uri時使用了正則表達式的模式,則proxy_pass之後必須不能使用uri; 用戶請求時傳遞的uri將直接附加代理到的服務的之後;

                server {
                    ...
                    server_name HOSTNAME;
                    location ~|~* /uri/ {
                        proxy http://host;
                    }
                    ...
                }

                http://HOSTNAME/uri/ --> http://host/uri/;
    ==================================================================

        2)proxy_set_header field value;
            設定發往後端主機的請求報文的請求首部的值;Context:   http, server, location

            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        3)proxy_cache_path
            定義可用於proxy功能的緩存;Context:    http

            proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];

        4)proxy_cache zone | off;
            指明要調用的緩存,或關閉緩存機制;Context:   http, server, location

        5)  proxy_cache_key string;
            緩存中用於“鍵”的內容;

            默認值:proxy_cache_key $scheme$proxy_host$request_uri;

        6)proxy_cache_valid [code ...] time;
            定義對特定響應碼的響應內容的緩存時長;

            定義在http{...}中;
            proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;

            定義在需要調用緩存功能的配置段,例如server{...};
            proxy_cache pxycache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 302 301 1h;
            proxy_cache_valid any 1m;

        7)proxy_cache_use_stale
            定義用過期的內容響應客戶端,例如:服務器錯誤或無響應,nginx可用緩存響應客戶端
            proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;

            Determines in which cases a stale cached response can be used when an error occurs during communication with the proxied server.

        8)proxy_cache_methods GET | HEAD | POST ...;
            針對方法做代理
            If the client request method is listed in this directive then the response will be cached. “GET” and “HEAD” methods are always added to the list, though it is recommended to specify them explicitly.

        9)proxy_hide_header field;
            請求或響應中可以隱藏後端服務器報文內容
            By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-...” from the response of a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed.
            例:
                location  / {
                    proxy_hide_header Server;
                }

        10)proxy_connect_timeout time;
            nginx代理服務器等待後端服務器響應時長;
            Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.

            默認爲60s;

6.10 proxy_set_header field value模塊

        設定發往後端主機的請求報文的請求首部的值;Context:   http, server, location
            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

6.11 ngx_http_headers_module模塊

作用:The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.
向由代理服務器響應給客戶端的響應報文添加自定義首部,或修改指定首部的值;

        1) add_header name value [always];
            添加自定義首部;

            add_header X-Via  $server_addr;
            add_header X-Accel $server_name;

        2) expires [modified] time;
            expires epoch | max | off;

            用於定義Expire或Cache-Control首部的值;

6.12 ngx_http_fastcgi_module模塊

作用:The ngx_http_fastcgi_module module allows passing requests to a FastCGI server.

        1)fastcgi_pass address;
            address爲fastcgi server的地址;  location, if in location;

        2)fastcgi_index name;
            fastcgi默認的主頁資源;

        3)fastcgi_param parameter value [if_not_empty];     # 定義重載路徑
            Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination.

        配置示例1:
            前提:配置好fpm server和mariadb-server服務;
                location ~* \.php$ {
                    root           /usr/share/nginx/html;
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index  index.php;
                    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html $fastcgi_script_name;          #定義路徑映射請求URL的須補在/usr/share/nginx/html後,構成新的URL,形成絕對路徑傳遞給後端服務器
                    include        fastcgi_params;
                }

        配置示例2:通過/pm_status和/ping來獲取fpm server狀態信息;
            location ~* ^/(pm_status|ping)$ {
                include        fastcgi_params;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME  $fastcgi_script_name;
            }

        4)fastcgi_cache_path path [levels=levels] [use_temp_path=on|off] 
            keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] 
            [manager_sleep=time] [manager_threshold=time] [loader_files=number] 
            [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number]
            [purger_sleep=time] [purger_threshold=time];

            定義fastcgi的緩存;緩存位置爲磁盤上的文件系統,由path所指定路徑來定義;

                levels=levels:緩存目錄的層級數量,以及每一級的目錄數量;levels=ONE:TWO:THREE
                    leves=1:2:2
                keys_zone=name:size
                    k/v映射的內存空間的名稱及大小
                inactive=time
                    非活動時長
                max_size=size
                    磁盤上用於緩存數據的緩存空間上限

        5)fastcgi_cache zone | off;
            調用指定的緩存空間來緩存數據;http, server, location

        6)fastcgi_cache_key string;
            定義用作緩存項的key的字符串;

        7)fastcgi_cache_methods GET | HEAD | POST ...;
            爲哪些請求方法使用緩存;

        8) fastcgi_cache_min_uses number;
            緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到此處所指定的次數方可被認作活動項;

        9) fastcgi_cache_valid [code ...] time;
            不同的響應碼各自的緩存時長;

            示例:
                http {
                    ...
                    fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2:1 keys_zone=fcgi:20m inactive=120s;
                    ...
                    server {
                        ...
                        location ~* \.php$ {
                            ...
                            fastcgi_cache fcgi;
                            fastcgi_cache_key $request_uri;
                            fastcgi_cache_valid 200 302 10m;
                            fastcgi_cache_valid 301 1h;
                            fastcgi_cache_valid any 1m; 
                            ...
                        }
                        ...
                    }
                    ...
                }

            10) fastcgi_keep_conn on | off;
                By default, a FastCGI server will close a connection right after sending the response. However, when this directive is set to the value on, nginx will instruct a FastCGI server to keep connections open.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章