Nginx的幾個重要模塊


ngx_http_ssl_module

  讓Nginx可以支持HTTPS的模塊,此模塊下的大多數指令都應用在http,server上下文

  ①ssl on | off;

   是否開啟ssl功能

  ②ssl_certificate file;

   當前虛擬主機使用的PEM格式的證書文件

  ③ssl_certificate_key file;

   當前虛擬主機使用的證書中與公鑰配對的私鑰文件

  ④ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2];

   ssl協議的版本,SSLv2不安全,建議使用TLS,默認使用的是TLS

  ⑤ssl_session_timeout time;

   ssl會話超時時長,指ssl會話中緩存條目的有效時長,默認為5m

  ⑥ssl_session_cache off | none | [builtin[:size]] [shared:name:size];

   指明ssl會話的緩存機制,默認是關閉的

builtin  //使用openssl內建的緩存機制,為worker獨有
shared  //由各worker共享的緩存
         name //緩存空間的名稱
         size  //緩存空間的大小/單位字節,每1MB內存空間可緩存4000個會話

  ⑦ssl_prefer_server_ciphers on | off;

   傾向於使用服務器端的加密算法,默認是關閉的

ngx_http_log_module

  此模塊可以基於給定的格式記錄請求於日志中

  ①access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];

   access_log off;

   用在http,server,location,if in location,limit_except上下文

   默認值是access_log logs/access.log combined;

  ②log_format name string ...;

   定義日志格式

   用在http上下文中

  ③open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];

   open_log_file_cache off;

   定義開啟日志文件緩存功能

   用於http,server,location上下文,默認是關閉的

max //最大緩存條目
inactive=time //非活動時長,默認為10s
min_uses //最少使用次數,默認為1次
valid=time //驗證緩存條目有效性的時長,默認為60s

ngx_http_rewrite_module

  基於此模塊實現對請求的URL進行重寫、重定向

  ①rewrite regex replacement [flag];

   用在server,location,if上下文中

/*①把用戶請求的URI基於regex做檢查,匹配到時將替換為replacement指定的字符串
②在同一個location中存在多個rewrite規則會自上而下逐個被檢查(最多循環10次),可以使用flag控制此循環功能
③如果replacement中有以http或https開頭,則替換結果會直接以重定向的方式返回客戶端*/

[flag]:
last  //重寫完成后停止對當前url在當前Location中的后續其他重寫操作,改為對新url的新一輪處理;
break  //重寫完成后停止對當前url在當前Location中的后續其他重寫操作;(退出rewrite中的規則)
redirect  //重寫完成后以臨時重定向方式直接返回重寫后生成的新的URL給客戶端,由客戶端對新的URL再次請求(響應碼:302)
permanent  //重寫完成后以永久重定向方式直接返回重寫后生成的新的URL給客戶端,由客戶端對新的URL再次請求(響應碼:301)

  ②rewrite_log on | off;

   是否啟用重寫日志;啟用時,日志信息被發往錯誤日志,默認是關閉的

   用在http,server,location,if上下文

  ③if (condition) { ... }

   條件判斷機制,在條件滿足時,執行配置塊中的配置,引入了一個新的配置上下文

   僅用在server,location上下文中

condition:
//比較表達式:
    ==,!=
    ~  //模式匹配,區分字符大小寫
    ~*  //模式匹配,不區分字符大小寫
    !~  //模式不匹配,區分字符大小寫
    !~*  //模式不匹配,不區分字符大小寫
//文件及目錄存在性判斷:
    -f,!-f  //文件
    -d,!-d //目錄
    -e,!-e  //存在
    -x,!-x  //執行權限

//示例:
if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}
if ($request_method = POST) {
    return 405;
}
if ($slow) {
    limit_rate 10k;
}
if ($invalid_referer) {
    return 403;
}

  ④return code [text];

   return code URL;

   return URL;

   以指定的響應碼和URL回應客戶端

   用在server,location,if上下文中

  ⑤set $variable value;

   用戶自定義變量

   用在server,location,if上下文中

ngx_http_gzip_module

  過濾器,對指定類型的資源壓縮傳輸以節約帶寬

  ①gzip on | off;

   啟用或禁用gzip壓縮響應報文

   用在http,server,location,if in location上下文中

  ②gzip_comp_level level;

   指定壓縮比(1-9),默認為1

   用在http,server,location上下文

  ③gzip_disable regex ...;

   regex是匹配客戶端瀏覽器類型的模式,表示對匹配到的瀏覽器不執行壓縮響應報文

   用在http,server,location上下文

  ④gzip_min_length length;

   觸發啟用壓縮功能的響應報文的最小長度

   用在http,server,location上下文

  ⑤gzip_http_version 1.0 | 1.1;

   設定啟用壓縮功能時,協議的最小版本,如果設置為1.1,1.0則不壓縮

   用在http,server,location上下文

  ⑥gzip_types mime-type ...;

   指定僅執行壓縮的資源內容類型,默認為text/html

   用在http,server,location上下文

  ⑦gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any ...;

   對代理的請求基於何種屬性判斷其是否應該啟用壓縮功能

   用在http,server,location上下文

ngx_http_fastcgi_module

  基於此模塊實現與php-fpm結合

  ①fastcgi_pass address;

   指明后端的服務器,address是fpm服務器監聽的地址和端口

   用在location和if in location上下文

  ②fastcgi_index name;

   fastcgi應用的主頁名稱

   用在http,server,location上下文

  ③fastcgi_param parameter value [if_not_empty];

   傳遞給fpm服務器的參數及其值

   用在http,server,location上下文

fastcgi_param SCRIPT_FILENAME /home/www/scripts/php$fastcgi_script_name;
fastcgi_param QUERY_STRING    $query_string;
fastcgi_param REQUEST_METHOD  $request_method;
fastcgi_param CONTENT_TYPE    $content_type;
fastcgi_param CONTENT_LENGTH  $content_length;

//更多參數查閱fastcgi_params

  ④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的緩存相關,僅能定義在http上下文

path //文件系統路徑,用於存儲緩存的文件數據
max_size=size //定義此路徑下的多大空間用於存儲緩存數據
levels=#[:#[:#]] //緩存目錄層級定義
    //如:levels=1:2
keys_zone=name:size //內存空間中用於緩存k/v映射關系的空間名稱及大小
inactive=time //非活動時間

  ⑤fastcgi_cache zone | off;

   啟用緩存功能,存於哪個zone中,依賴於上個配置中的定義

   用在http,server,location上下文

  ⑥fastcgi_cache_key string;

   定義要使用的緩存鍵

   用在http,server,location上下文  

//示例
fastcgi_cache_key localhost:9000$request_uri;

  ⑦fastcgi_cache_methods GET | HEAD | POST ...;

   緩存基於哪些請求方法的相關數據

   用在http,server,location上下文

  ⑧fastcgi_cache_min_uses number;

   指定時間內緩存的最少使用次數

   用在http,server,location上下文

  ⑨fastcgi_cache_valid [code ...] time;

   對不同響應碼設定其可緩存時長

//示例
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 404      1m;

  注意:啟用緩存功能時,至少應該指定的三個參數:fastcgi_cache,fastcgi_cache_key,fastcgi_cache_valid

/*
Nginx緩存的實現機制

分兩部分存儲緩存信息,
一部分放於內存中:key-value形式
    其中key是url,value是緩存信息文件的校驗碼(md5)
另一部分是將緩存信息的文件置於磁盤上,並基於md5校驗碼實現分級存儲,以便查找
*/

ngx_http_proxy_module

  基於此模塊實現反代客戶端請求至后端服務器

  ①proxy_pass URL;

   代理指令,后跟后端主機的URL

   用在location,if in location,limit_except上下文

//①proxy_pass后面的路徑不帶uri時,會把location的uri傳遞給后端主機
location /uri/ {
    proxy_pass http://HOST;
}
//假如location中的/uri/是/bbs/,訪問的站點是www.a.com,則通過proxy_pass傳遞給后端主機的URL是:http://www.a.com/bbs/

//②proxy_pass后面路徑是一個uri時,其會將location的uri替換為proxy_pass后端主機的uri
location /uri/ {
    proxy_pass http://HOST/new_uri/;
}
//假如location中的/uri/是/bbs/,訪問的站點是www.a.com,proxy_pass后面的new_uri是/BBS/,則通過proxy_pass傳遞給后端主機的URL是:http://www.a.com/BBS/;

//③如果location定義uri時使用了正則表達式匹配機制,則proxy_pass后的路徑必須不能使用uri
location ~|~* PATTERN {
    proxy_pass http://HOST;
}

  ②proxy_set_header field value;

   設定向后端主機發送的請求報文的首部及其值

   用在http,server,location上下文

proxy_set_header Real_Client $remote_addr(client addr);
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
//保留原來的值再附加新值(建議使用)

  ③proxy_cache_path path [levels=levels] 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上下文

//示例
proxy_cache_path /var/cache levels=2:1 keys_zone=mycache:10m max_size=10m;

  ④proxy_cache zone | off;

   調用緩存,默認為off

   用在http,server,location上下文

  ⑤proxy_cache_key string;

   定義緩存鍵;

   用在http,server,location上下文

//示例
proxy_cache_key $request_uri(一般是請求uri)
proxy_cache_key $scheme$proxy_host$request_uri

  ⑥proxy_cache_valid [code ...] time;

   為不同的響應碼設定其緩存的時長

   用在http,server,location上下文

//示例
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;

  ⑦proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ...;

   向后端請求不到相應資源時基於哪種響應碼或錯誤,使用過期緩存響應客戶端

   用在http,server,location上下文

  ⑧proxy_connect_timeout time;

   與后端建立連接的超時時長,默認60s,最長為75s

   用在http,server,location上下文

  ⑨proxy_read_timeout time;

   等待后端主機發送響應報文的超時時長,默認為60s

   用在http,server,location上下文

  ⑨+①proxy_send_timeout time;

   向后端服務器發送請求報文的超時時長,默認為60s

   用在http,server,location上下文

ngx_http_headers_module

  基於此模塊在響應給客戶端的報文中添加首部

  ①add_header name value [always];

   向響應報文添加自定義首部,並賦值

   用在http,server,location,if in location上下文  

add_header X-Cache $upstream_cache_status;

//可查看是否命中緩存
//示例:
add_header x-via $server_addr(接收請求報文的服務器地址)

  ②expires [modified] time;

   expires epoch | max | off;

   用於添加Expire及Cache-Control首部或修改其值

   用在http,server,location,if in location上下文

ngx_http_upstream_module

  基於此模塊實現nginx的負載均衡:定義服務器組,將多個后端主機定義為服務器組,而后可由proxy_pass,fastcgi_pass,memcached_pass等調用

  ①upstream name { ... };

   定義后端服務器組,引入新的上下文,只能用於http上下文

  ②server address [parameters];

   定義后端服務器的地址和相關參數

   僅用於在upstream上下文

地址格式:
    IP[:port]
    HOSTNAME[:port]
    unix:/PATH/TO/SOME_SOCK_FILE
參數:
    weight=number //服務器調度權重
    max_fails=number //最大失敗嘗試次數
    fail_timeout=time //設置服務器失敗(不可用)的超時時長
    backup //備用主機,相當於sorry_server
    down //手動標記其不再處理任何用戶請求

  ③ip_hash;

   指定調度算法:源地址哈希

   僅用於在upstream上下文

  ④least_conn;

   指定調度算法:最少連接

   僅用於在upstream上下文

  ⑤keepalive connections;

   保持連接的個數

   僅用於在upstream上下文

  ⑥health_check [parameters];

   定義后端主機的健康狀態檢測

   只能在location中使用

//可用參數:
    interval=# //檢測的頻度,默認為5s
    fails=# //判定為失敗的檢測次數
    passes=# //判定為成功的檢測次數
    uri=uri //執行檢測時請求的uri,默認為主頁
    match=name //基於那個match做檢測結果為'ok'或'not ok'的判定
    port=# //向服務器的那個端口發起檢測請求

  ⑦match name {...};

   對后端主機做檢測時,定義其結果的判斷標准,nginx2.0+才支持

   僅用於http上下文

//專用指令:
status:期望的響應碼
    status CODE
    status !CODE
    status CODE-CODE
header:基於響應首部進行判斷
    header HEADER=VALUE
    header HEADER!=VALUE
    header [!]HEADER
    header HEADER~VALUE
body:期望的響應報文的主體部分該有的內容
    body ~"CONTENT"
    body !~"CONTENT"

  ⑧hash key [consistent];

   定義調度方法,可自定義基於何種信息(key)進行綁定

   僅用於在upstream上下文

hash $remote_addr
hash $request_uri
hash $cookie_username

 


免責聲明!

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



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