Nginx 配置文件簡介-2(Nginx 常用模塊)


ngx_http_access_module

作用: ngx_http_access_module模塊允許限制對某些客戶端地址的訪問

指令:

     allow:

        語法:allow address | CIDR | unix: | all;

        默認值:空

        應用位置:http, server, location, limit_except

    deny:

        語法:allow address | CIDR | unix: | all;

        默認值:空

        應用位置:http, server, location, limit_except

案例:

location / {
    deny  192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny  all;
}

ngx_http_api_module 

作用:

   ngx_http_api_module模塊(1.13.3)提供REST API,用於訪問各種狀態信息,即時配置上游服務器組以及管理鍵值對,而無需重新配置nginx。

   該模塊取代了ngx_http_status_module和ngx_http_upstream_conf_module模塊。商業訂閱才有,付費的nginx.

ngx_http_auth_basic_module

作用:

   ngx_http_auth_basic_module模塊允許通過使用“HTTP基本身份驗證”協議驗證用戶名和密碼來限制對資源的訪問。

案例:

location / {
    auth_basic           "closed site";  # 提示短語
    auth_basic_user_file conf/htpasswd; # 密碼文件,需要用 htpasswd 生成.
} 

ngx_http_auth_jwt_module

作用:

   ngx_http_auth_jwt_module模塊(1.11.3)通過使用指定的密鑰驗證提供的JSON Web令牌(JWT)來實現客戶端授權。 JWT聲明必須以JSON Web簽名(JWS)結構進行編碼。 該模塊可用於OpenID Connect身份驗證。

案例:

location / {
    auth_jwt          "closed site";
    auth_jwt_key_file conf/keys.json;
}

ngx_http_autoindex_module

作用:

   ngx_http_autoindex_module模塊處理以斜杠字符('/')結尾的請求,並生成目錄列表。 當ngx_http_index_module模塊找不到索引文件時,通常會將請求傳遞給ngx_http_autoindex_module模塊。

案例:

location /test/ {
       root   /usr/share/nginx/html;
       autoindex on;   # 打開索引
       autoindex_exact_size on; # 顯示文件尺寸
       autoindex_format json;   # 以 json 的格式展示
       autoindex_localtime on;  # 顯示創建時間(UTC時間)
    }

ngx_http_fastcgi_module

作用:

   ngx_http_fastcgi_module模塊允許將請求傳遞給FastCGI服務器。

指令詳解:

   fastcgi_bind

     語法:fastcgi_bind address [transparent] | off;

     默認值:無

     應用位置:http, server, location

     作用:指定 nginx 與 fastcgi 通信的 IP 地址,一般不做設置。 與回環 IP 地址通信,需要設置路由表。

  fastcgi_buffer_size

    語法:fastcgi_buffer_size size;

    默認值:fastcgi_buffer_size 4k|8k;

    應用位置:http, server, location

    作用:  設置用於讀取從FastCGI服務器接收的響應的第一部分的緩沖區的大小。 這部分通常包含一個小的響應頭。 默認情況下,緩沖區大小等於一個內存頁面。 這是4K或8K,具體取決於平台。 然而,它可以做得更小。

  fastcgi_buffers

    語法:fastcgi_buffers number size;

    默認值:fastcgi_buffers 8 4k|8k;

    應用位置:http, server, location

    作用:設置用於從FastCGI服務器讀取響應的緩沖區的數量和大小,用於單個連接。 默認情況下,緩沖區大小等於一個內存頁面。 這是4K或8K,具體取決於平台。

  fastcgi_buffering

    語法:fastcgi_buffering on | off;

    默認值:fastcgi_buffering on;

    應用位置:http, server, location

    作用:啟用或禁用緩沖來自FastCGI服務器的響應。
              啟用緩沖后,nginx會盡快從FastCGI服務器接收響應,並將其保存到fastcgi_buffer_size和fastcgi_buffers指令設置的緩沖區中。

              如果整個響應不適合內存,則可以將其中的一部分保存到磁盤上的臨時文件中。 寫入臨時文件由fastcgi_max_temp_file_size和      fastcgi_temp_file_write_size指令控制。
              禁用緩沖時,響應會在收到響應時立即同步傳遞給客戶端。 nginx不會嘗試從FastCGI服務器讀取整個響應。 nginx一次可以從服務器接收的數據的最大大小由fastcgi_buffer_size指令設置。
              也可以通過在“X-Accel-Buffering”響應頭字段中傳遞“是”或“否”來啟用或禁用緩沖。 可以使用fastcgi_ignore_headers指令禁用此功能。

   fastcgi_busy_buffers_size

     語法:fastcgi_busy_buffers_size size;

     默認值:fastcgi_busy_buffers_size 8k|16k;

     應用位置:http, server, location

     作用: 通常設置為 fastcgi_buffer_size 的兩倍,高負荷下內存緩沖區的總大小.

  fastcgi_cache

    語法:fastcgi_cache zone | off;

    默認值:fastcgi_cache off;

    應用位置:http, server, location

    作用:該指令用於定義一個共享內存zone,用於緩存 。off 參數關閉從上層繼承.

  fastcgi_cache_background_update

    語法:fastcgi_cache_background_update on | off;

    默認值:fastcgi_cache_background_update off;

    應用位置:http, server, location

    作用:允許啟動后台子請求來更新過期的緩存項。

  fastcgi_cache_bypass

    語法:fastcgi_cache_bypass string ...;

    默認值:無

    應用位置:http, server, location

    作用:該指令定義一個或多個字符串變量,當非空或非零時,將導致從 FastCGI 服務器獲取而不是從緩存中獲取響應

    案例:

fastcgi_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
fastcgi_cache_bypass $http_pragma    $http_authorization;

  fastcgi_cache_key

    語法fastcgi_cache_key string;

    默認值:無

    應用位置:http, server, location

    作用:該指令指定一個字符串,作為存儲和獲取緩存值的 key.

    案例:

fastcgi_cache_key localhost:9000$request_uri;

  fastcgi_cache_lock

    語法:fastcgi_cache_lock on | off;

    默認值:fastcgi_cache_lock off;

    應用位置:http, server, location

    作用:啟用這個指令將會阻止多個請求對同一緩存 key  進行操作.

  fastcgi_cache_lock_timeout

    語法:fastcgi_cache_lock_timeout time;

    默認值:fastcgi_cache_lock_timeout 5s;

    應用位置:http, server, location

    作用:設置fastcgi_cache_lock的超時。當時間過期時,請求將被傳遞給FastCGI服務器,然而,響應將不會被緩存。

  fastcgi_cache_lock_age

    語法:fastcgi_cache_lock_age time;

    默認值:fastcgi_cache_lock_age 5s;

    應用位置:http, server, location

    作用:如果傳遞給FastCGI服務器的用於填充新緩存元素的最后一個請求在指定的時間內沒有完成,那么可以再向FastCGI服務器傳遞一個請求。不太確定,最好不用.

  fastcgi_cache_min_uses

    語法:fastcgi_cache_min_uses number;

    默認值:fastcgi_cache_min_uses 1;

    應用位置:http, server, location

    作用:該指令指定在一個響應被緩存之前最少的請求數。

  fastcgi_cache_path

    語法: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];

   默認值:無

   應用位置: http

   案例:

fastcgi_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;  # key_zone 存儲的內存區域為 one, 分配內存 10M

fastcgi_cache_path /data/nginx/cache keys_zone=cache_zone:10m;

map $request_method $purge_method {
    PURGE   1;
    default 0;
}

server {
    ...
    location / {
        fastcgi_pass        backend;
        fastcgi_cache       cache_zone;
        fastcgi_cache_key   $uri;
        fastcgi_cache_purge $purge_method;
    }
}

  fastcgi_cache_purge

   語法:fastcgi_cache_purge string ...;

   默認值:無

   應用位置:http, server, location

   作用:待定,目前看不明白.

  fastcgi_cache_use_stale

   語法:fastcgi_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_503 | http_403 | http_404 | http_429 | off ...;

   默認值:fastcgi_cache_use_stale off;

   應用位置:http, server, location

   作用:確定在與FastCGI服務器通信期間發生錯誤時,在哪些情況下可以使用過時的緩存響應。指令的參數與fastcgi_next_upstream指令的參數匹配。

  fastcgi_cache_valid

    語法: fastcgi_cache_valid [code ...] time;

    默認值:無

    應用位置:http, server, location

    作用: 為不同的響應代碼設置緩存時間。

    案例:

fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301      1h;
fastcgi_cache_valid any      1m;  # 響應任意狀態碼

  fastcgi_connect_timeout

    語法:fastcgi_connect_timeout time;

    默認值:fastcgi_connect_timeout 60s;

    應用位置:http, server, location

    作用:nginx 向 fastcgi 服務器生成一個請求后,該指令指定等待 nginx 接收連接的最長時間。

  fastcgi_hide_header

     語法:fastcgi_hide_header field;

     默認值:無

     應用位置:http, server, location

     作用:默認情況下,nginx不會傳遞頭字段“Status”和“X-Accel-…”從FastCGI服務器到客戶端的響應。fastcgi_hide_header指令設置不會傳遞的其他字段。相反,如果需要允許字段的傳遞,則可以使用fastcgi_pass_header指令。

  fastcgi_ignore_client_abort

    語法:fastcgi_ignore_client_abort on | off;

    默認值:fastcgi_ignore_client_abort off;

    應用位置:http, server, location

    作用:確定當客戶端不等待響應而關閉連接時,是否應該關閉與FastCGI服務器的連接。如果設置為 on,客戶端放棄連接后,nginx 將不會放棄同 FastCGI 服務器的連接。

  fastcgi_intercept_errors

    語法:fastcgi_intercept_errors on | off;

    默認值:fastcgi_intercept_errors off;

    應用位置:http, server, location

    作用:如果啟用該指令,nginx 將會顯示 error_page 指令配置信息,而不是直接來自 FastCGI 服務器的響應

  fastcgi_keep_conn

   語法:fastcgi_keep_conn on | off;

   默認值:fastcgi_keep_conn off;

   應用位置:http, server, location

   作用:默認情況下,FastCGI服務器將在發送響應后立即關閉連接。 但是,當此偽指令設置為on時,nginx將指示FastCGI服務器保持連接打開。 特別是,這對於FastCGI服務器的keepalive連接起作用是必要的。

  fastcgi_max_temp_file_size

   語法:fastcgi_max_temp_file_size size;

   默認值:fastcgi_max_temp_file_size 1024m;

   應用位置:http, server, location

   作用:當啟用來自FastCGI服務器的響應緩沖,並且整個響應不適合fastcgi_buffer_size和fastcgi_buffers指令設置的緩沖區時,響應的一部分可以保存到臨時文件中。 該指令設置臨時文件的最大大小。 一次寫入臨時文件的數據大小由fastcgi_temp_file_write_size指令設置。零值禁用緩沖對臨時文件的響應。

  fastcgi_temp_path

    語法:fastcgi_temp_path path [level1 [level2 [level3]]];

    默認值:fastcgi_temp_path fastcgi_temp;

    應用位置:http, server, location

    作用:該指令指定緩存臨時文件的目錄,作為從 FastCGI 代理而來文件的緩存,可選多級目錄深度.

  fastcgi_temp_file_write_size

   語法:fastcgi_temp_file_write_size size;

   默認值:fastcgi_temp_file_write_size 8k|16k;

   應用位置:http, server, location

   作用:當啟用從FastCGI服務器到臨時文件的響應緩沖時,限制一次寫入臨時文件的數據大小。 默認情況下,size由fastcgi_buffer_size和fastcgi_buffers指令設置的兩個緩沖區限制。 臨時文件的最大大小由fastcgi_max_temp_file_size directi設置

  fastcgi_next_upstream

    語法:fastcgi_next_upstream error | timeout | invalid_header | http_500 | http_503 | http_403 | http_404 | http_429 | non_idempotent | off ...;

   默認值:fastcgi_next_upstream error timeout;

   應用位置:http, server, location

   作用:該指令指示下一個 FastCGI 服務器被選中接收提供相應的條件,如果nginx 客戶端已經被發送了某些信息,那么這種條件將不會被使用。

     error
        與服務器建立連接,向其傳遞請求或讀取響應頭時發生錯誤;

    timeout
        在與服務器建立連接,向其傳遞請求或讀取響應頭時發生超時;
    invalid_header
         服務器返回空或無效響應;
    HTTP_500
        服務器返回代碼為500的響應;
    http_503
        服務器返回代碼為503的響應;
    HTTP_403
        服務器返回代碼為403的響應;
    http_404
       服務器返回代碼為404的響應;
    http_429
       服務器返回代碼為429的響應(1.11.13);

    off
       禁用將請求傳遞給下一個server

  fastcgi_next_upstream_timeout

    語法:fastcgi_next_upstream_timeout time;

    默認值:fastcgi_next_upstream_timeout 0;

    應用位置:http, server, location

    作用:限制請求可以傳遞到下一個服務器的時間。 0值關閉此限制。

   fastcgi_next_upstream_tries

     語法:fastcgi_next_upstream_tries number;

     默認值:fastcgi_next_upstream_tries 0;

     應用位置:http, server, location

     作用:限制將請求傳遞到下一個服務器的可能嘗試次數。 0值關閉此限制。

   fastcgi_no_cache

     語法:fastcgi_no_cache string ...;

     默認值:無

     應用位置:http, server, location

     作用:該指令指定一個或者多個字符串變量,當變量非空或非零,將指導nginx 不會將來自FastCGI 服務器的響應保存到緩存中。

    案例:

fastcgi_no_cache $cookie_nocache $arg_nocache$arg_comment;
fastcgi_no_cache $http_pragma    $http_authorization;

   fastcgi_param

     語法:fastcgi_param parameter value [if_not_empty];

     默認值:無

     應用位置:http, server, location

     作用:該指令設置傳遞到 FastCGI 服務器的參數和它的值,在值為非空的時候,如果僅傳送參數,那么  if_not_empty 應該設置額外的參數.

     案例:

fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_param QUERY_STRING    $query_string;

fastcgi_param HTTPS   $https if_not_empty;

#如果PHP是使用--enable-force-cgi-redirect配置參數構建的,則還應使用值“200”傳遞REDIRECT_STATUS參數:
fastcgi_param REDIRECT_STATUS 200;

  fastcgi_pass

    語法:fastcgi_pass address;

    默認值:無

    應用位置:location, if in location

    作用:該指令指定 FastCGI 服務器如何傳遞請求,可以是 address:port 組合的 TCP 套接字,也可以是 unix:path UNIX 域套接字.

   fastcgi_pass_header

     語法:fastcgi_pass_header field;

     默認值:無

     應用位置:http, server, location

     作用:該指令覆蓋在 Fastcgi_hide_header 中設置的禁用頭,允許他們發送到客戶端.

   fastcgi_pass_request_body

     語法:fastcgi_pass_request_body on | off;

     默認值:fastcgi_pass_request_body on;

     應用位置:http, server, location

     作用:指示是否將原始請求主體傳遞給FastCGI服務器。

   fastcgi_pass_request_headers

     語法:fastcgi_pass_request_headers on | off;

     默認值:fastcgi_pass_request_headers on;

     應用位置:http, server, location

     作用:指示原始請求的標頭字段是否傳遞給FastCGI服務器。

   fastcgi_read_timeout

     語法:fastcgi_read_timeout time;

     默認值:fastcgi_read_timeout 60s;

     應用位置:http, server, location

     作用:該指令指定在連接關閉之前從FastCGI服務器兩次成功讀操作之間的時間長度.

               定義從FastCGI服務器讀取響應的超時。 僅在兩個連續的讀操作之間設置超時,而不是為整個響應的傳輸。 如果FastCGI服務器在此時間內未傳輸任何內容,則關閉連接。

   fastcgi_send_timeout

     語法:fastcgi_send_timeout time;

     默認值:fastcgi_send_timeout 60s;

     應用位置:http, server, location

     作用:設置將請求傳輸到FastCGI服務器的超時。 僅在兩個連續的寫操作之間設置超時,而不是為整個請求的傳輸。 如果FastCGI服務器在此時間內未收到任何內容,則關閉連接。

   fastcgi_split_path_info

     語法:fastcgi_split_path_info regex;

     默認值:無

     應用位置:location

     作用:定義捕獲$ fastcgi_path_info變量值的正則表達式。 正則表達式應該有兩個捕獲:第一個變為$ fastcgi_script_name變量的值,第二個變為$ fastcgi_path_info變量的值。只有依靠 PATH_INFO 的應用程序才是必須的,例如,使用這些設置

location ~ ^(.+\.php)(.*)$ {
    fastcgi_split_path_info       ^(.+\.php)(.*)$;
    fastcgi_param SCRIPT_FILENAME /path/to/php$fastcgi_script_name;
    fastcgi_param PATH_INFO       $fastcgi_path_info;

    and the “/show.php/article/0001” request, the SCRIPT_FILENAME parameter will be equal to “/path/to/php/show.php”, and the PATH_INFO parameter will be equal to “/article/0001”.

   fastcgi_store

     語法:fastcgi_store on | off | string;

     默認值:fastcgi_store off;

     應用位置:http, server, location

     作用:該指令啟用將從 FastCGI 服務器獲取來的響應作為文件存儲在磁盤上。設置為 on,那么將會使用 alias 或者 root 指令的值作為存儲文件的基礎路徑,指定一個字符串可以取而代之成為指示文件的存儲位置的選擇.

     案例:

location /fetch/ {
    internal;

    fastcgi_pass         backend:9000;
    ...

    fastcgi_store        on;
    fastcgi_store_access user:rw group:rw all:r;
    fastcgi_temp_path    /data/temp;

    alias                /data/www/;
}

   fastcgi_store_access

     語法:fastcgi_store_access users:permissions ...;

     默認值:fastcgi_store_access user:rw;

     應用位置:http, server, location

     作用:該指令為新創建的,存儲在 FastCGI_store 中的文件設置訪問權限.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM