nginx模塊在編譯文件下的odjs目錄下
cat 查看ngx_modules.c文件
可以看到一些基本extern模塊
常用標(biāo)準(zhǔn)模塊
1. 性能相關(guān)配置
worker進(jìn)程的數(shù)量;通常應(yīng)該為當(dāng)前主機(jī)的cpu的物理核心數(shù)
worker_cpu_affinity auto [cpumask] #將work進(jìn)程綁定在固定cpu上提高緩存命中率
例:
worker_cpu_affinity 0001 0010 0100 1000;
worker_cpu_affinity 0101 1010;
worker_priority number
指定worker進(jìn)程的nice值,設(shè)定worker進(jìn)程優(yōu)先級: [-20,20]
worker_rlimit_nofile number
worker進(jìn)程所能夠打開的文件數(shù)量上限,默認(rèn)較小,生產(chǎn)中需要調(diào)大如65535
2. 時(shí)間驅(qū)動(dòng)events相關(guān)的配置
worker_connections number
每個(gè)worker進(jìn)程所能夠打開的最大并發(fā)連接數(shù)數(shù)量,如10240
總最大并發(fā)數(shù): worker_processes * worker_connections
use method
指明并發(fā)連接請求的處理方法,默認(rèn)自動(dòng)選擇最優(yōu)方法不用調(diào)整
如:use epoll;
accept_mutex on | off 互斥;
處理新的連接請求的方法; on指由各個(gè)worker輪流處理新請求
, Off指每個(gè)新請求的到達(dá)都會(huì)通知(喚醒)所有的worker進(jìn)程,但
只有一個(gè)進(jìn)程可獲得連接,造成“驚群”,影響性能,默認(rèn)on
3. http核心模塊相關(guān)配置ngx_http_core_module
3.1web服務(wù)模板
server { … }
配置一個(gè)虛擬主機(jī)
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root /PATH/TO/DOCUMENT_ROOT;
}
注意:
(1) 基于port;
listen PORT; 指令監(jiān)聽在不同的端口
(2) 基于ip的虛擬主機(jī)
listen IP:PORT; IP 地址不同
(3) 基于hostname
server_name fqdn; 指令指向不同的主機(jī)名
3.2套接字相關(guān)配置
listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
default_server 設(shè)定為默認(rèn)虛擬主機(jī)
ssl 限制僅能夠通過ssl連接提供服務(wù)
backlog=number 超過并發(fā)連接數(shù)后,新請求進(jìn)入后援隊(duì)列的長度
rcvbuf=size 接收緩沖區(qū)大小
sndbuf=size 發(fā)送緩沖區(qū)大小
3.3 server_name
server_name name …;
支持*通配任意長度的任意字符
server_name *.magedu.com www.magedu.*
支持~起始的字符做正則表達(dá)式模式匹配,性能原因慎用
server_name ~^wwwd+.magedu.com$ #d 表示 [0-9]
匹配優(yōu)先級機(jī)制從高到低:
(1) 首先是字符串精確匹配 如: www.magedu.com
(2) 左側(cè)*通配符 如: *.magedu.com
(3) 右側(cè)*通配符 如: www.magedu.*
(4) 正則表達(dá)式 如: ~^.*.magedu.com$
(5) default_server
3.4 延遲發(fā)送選項(xiàng)
tcp_nodelay on | off;
tcp_nopush on | off;
在keepalived模式下的連接是否啟用TCP_NODELAY選項(xiàng)
當(dāng)為off時(shí),延遲發(fā)送,合并多個(gè)請求后再發(fā)送
默認(rèn)On時(shí),不延遲發(fā)送
可用于: http, server, location
3.5 sendfile
sendfile on | off;
是否啟用sendfile功能,在內(nèi)核中封裝報(bào)文直接發(fā)送
默認(rèn)Off
3.6 隱藏版本信息
server_tokens on | off | build | string
是否在響應(yīng)報(bào)文的Server首部顯示nginx版本
3.7 location匹配
location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
在一個(gè)server中l(wèi)ocation配置段可存在多個(gè),用于實(shí)現(xiàn)從uri到文件系統(tǒng)的路徑映射; ngnix會(huì)根據(jù)
用戶請求的URI來檢查定義的所有l(wèi)ocation,并找出一個(gè)最佳匹配,而后應(yīng)用其配置
示例:
server {…
server_name www.magedu.com;
location /images/ {
root /data/imgs/;
}
}
http://www.magedu.com/images/logo.jpg
–> /data/imgs/images/logo.jpg
=:對URI做精確匹配;
^~:對URI的最左邊部分做匹配檢查,不區(qū)分字符大小寫
~:對URI做正則表達(dá)式模式匹配,區(qū)分字符大小寫
~*:對URI做正則表達(dá)式模式匹配,不區(qū)分字符大小寫
不帶符號:匹配起始于此uri的所有的uri
匹配優(yōu)先級從高到低:
=, ^~, ~/~*, 不帶符號
3.7 路徑別名alias path
示例:
http://www.magedu.com/bbs/index.php
location /bbs/ {
alias /web/forum/;
} –> /web/forum/index.html
location /bbs/ {
root /web/forum/;
} –> /web/forum/bbs/index.html
注意: location中使用root指令和alias指令的意義不同
(a) root,相當(dāng)于追加在root目錄后面
(b) alias,相當(dāng)于對location中的url進(jìn)行替換
3.8 錯(cuò)誤頁面顯示
error_page code … [=[response]] uri;
模塊: ngx_http_core_module
定義錯(cuò)誤頁, 以指定的響應(yīng)狀態(tài)碼進(jìn)行響應(yīng)
可用位置: http, server, location, if in location
error_page 404 /404.html
error_page 404 =200 /404.html <span class=”token comment”>#防止404頁面被劫持
3.9 長連接相關(guān)配置
keepalive_timeout timeout [header_timeout];
設(shè)定保持連接超時(shí)時(shí)長, 0表示禁止長連接, 默認(rèn)為75s
keepalive_requests number;
在一次長連接上所允許請求的資源的最大數(shù)量,默認(rèn)為100
keepalive_disable none | browser …
對哪種瀏覽器禁用長連接
send_timeout time;
向客戶端發(fā)送響應(yīng)報(bào)文的超時(shí)時(shí)長,此處是指兩次寫操作之間的間隔時(shí)長,而非
整個(gè)響應(yīng)過程的傳輸時(shí)長
3.10 請求報(bào)文緩存
client_body_buffer_size size;
用于接收每個(gè)客戶端請求報(bào)文的body部分的緩沖區(qū)大?。荒J(rèn)為16k;超出此大小時(shí),
其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置
client_body_temp_path path [level1 [level2 [level3]]];
設(shè)定用于存儲客戶端請求報(bào)文的body部分的臨時(shí)存儲路徑及子目錄結(jié)構(gòu)和數(shù)量
目錄名為16進(jìn)制的數(shù)字;
client_body_temp_path /var/tmp/client_body 1 2 2
1 1級目錄占1位16進(jìn)制,即2^4=16個(gè)目錄 0-f
2 2級目錄占2位16進(jìn)制,即2^8=256個(gè)目錄 00-ff
2 3級目錄占2位16進(jìn)制, 即2^8=256個(gè)目錄 00-ff
3.11 對客戶端進(jìn)行限制相關(guān)配置
limit_rate rate;
限制響應(yīng)給客戶端的傳輸速率,單位是bytes/second 默認(rèn)值0表示無限制
limit_except method … { … },僅用于location
限制客戶端使用除了指定的請求方法之外的其它方法
method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND,
PROPPATCH, LOCK, UNLOCK, PATCH
例:
limit_except GET {
allow 192.168.1.0/24;
deny all;
}
除了GET和HEAD 之外其它方法僅允許192.168.1.0/24網(wǎng)段主機(jī)使用
4. 訪問控制模塊ngx_http_access_module
實(shí)現(xiàn)基于ip的訪問控制功能
allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;
http, server, location, limit_except
自上而下檢查,一旦匹配,將生效,條件嚴(yán)格的置前
示例:
location / {
deny 192.168.1.1;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all;
}
5. 用戶認(rèn)證模塊ngx_http_auth_basic_module
實(shí)現(xiàn)基于用戶的訪問控制,使用basic機(jī)制進(jìn)行用戶認(rèn)證
auth_basic string | off;
auth_basic_user_file file;
location /admin/ {
auth_basic “Admin Area”;
auth_basic_user_file /etc/nginx/.ngxpasswd;
}
用戶口令:
1、明文文本:格式name:password:comment
2、加密文本:由htpasswd命令實(shí)現(xiàn) httpd-tools所提供
htpasswd [-c第一次創(chuàng)建時(shí)使用] [-D刪除用戶] passwdfile username
6. 狀態(tài)查看模塊ngx_http_stub_status_module
用于輸出nginx的基本狀態(tài)信息
Active connections:當(dāng)前狀態(tài),活動(dòng)狀態(tài)的連接數(shù)
accepts:統(tǒng)計(jì)總值,已經(jīng)接受的客戶端請求的總數(shù)
handled:統(tǒng)計(jì)總值,已經(jīng)處理完成的客戶端請求的總數(shù)
requests:統(tǒng)計(jì)總值,客戶端發(fā)來的總的請求數(shù)
Reading:當(dāng)前狀態(tài),正在讀取客戶端請求報(bào)文首部的連接的連接數(shù)
Writing:當(dāng)前狀態(tài),正在向客戶端發(fā)送響應(yīng)報(bào)文過程中的連接數(shù)
Waiting:當(dāng)前狀態(tài),正在等待客戶端發(fā)出請求的空閑連接數(shù)
示例:
location /status {
stub_status;
allow 172.16.0.0/16;
deny all;
}
7. 日志記錄模塊ngx_http_log_module
1、 log_format name string …;
string可以使用nginx核心模塊及其它模塊內(nèi)嵌的變量
2、 access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
access_log off;
訪問日志文件路徑,格式及相關(guān)的緩沖的配置
buffer=size
flush=time
示例
log_format compression ‘$remote_addr-$remote_user [$time_local] ‘
‘”$request” $status $bytes_sent ‘
‘”$http_referer” “$http_user_agent” “$gzip_ratio”‘;
access_log /spool/logs/nginx-access.log compression buffer=32k;
json格式日志示例;log_format json ‘{“@timestamp”:”$time_iso8601″,’
‘”client_ip”:”$remote_addr”,’
‘”size”:$body_bytes_sent,’
‘”responsetime”:$request_time,’
‘”upstreamtime”:”$upstream_response_time”,’
‘”upstreamhost”:”$upstream_addr”,’
‘”http_host”:”$host”,’
‘”method”:”$request_method”,’
‘”request_uri”:”$request_uri”,’
‘”xff”:”$http_x_forwarded_for”,’
‘”referrer”:”$http_referer”,’
‘”agent”:”$http_user_agent”,’
‘”status”:”$status”}’;
3、 open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
緩存各日志文件相關(guān)的元數(shù)據(jù)信息
max:緩存的最大文件描述符數(shù)量
min_uses:在inactive指定的時(shí)長內(nèi)訪問大于等于此值方可被當(dāng)作活動(dòng)項(xiàng)
inactive:非活動(dòng)時(shí)長
valid:驗(yàn)正緩存中各緩存項(xiàng)是否為活動(dòng)項(xiàng)的時(shí)間間隔
例: open_log_file_cache max=1000 inactive=20s valid=1m;
8. 壓縮相關(guān)選項(xiàng)ngx_http_gzip_module
1、gzip on off;#啟用或禁用gzip壓縮
2、gzip_comp_level level;#壓縮比由低到高: 1 到 9 默認(rèn): 1
3、gzip_disable regex …; #匹配到客戶端瀏覽器不執(zhí)行壓縮
4、gzip_min_length length; #啟用壓縮功能的響應(yīng)報(bào)文大小閾值
5、gzip_http_version 1.0 | 1.1; #設(shè)定啟用壓縮功能時(shí),協(xié)議的最小版本 默認(rèn): 1.1
6、gzip_buffers number size;
支持實(shí)現(xiàn)壓縮功能時(shí)緩沖區(qū)數(shù)量及每個(gè)緩存區(qū)的大小
默認(rèn): 32 4k 或 16 8k
7、gzip_types mime-type …;
指明僅對哪些類型的資源執(zhí)行壓縮操作;即壓縮過濾器
默認(rèn)包含有text/html,不用顯示指定,否則出錯(cuò)
8、gzip_vary on| off;
如果啟用壓縮,是否在響應(yīng)報(bào)文首部插入“Vary: AcceptEncoding
9、 gzip_proxied off | expired| no-cache| no-|
private | no_last_modified| no_etag | auth | any …;
nginx對于代理服務(wù)器請求的響應(yīng)報(bào)文,在何種條件下啟
用壓縮功能
off:對被代理的請求不啟用壓縮
expired,no-cache, no-store, private:對代理服務(wù)器
請求的響應(yīng)報(bào)文首部Cache-Control值任何一個(gè),啟用壓縮功能
示例:
gzip on;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_vary on;
gzip_min_length 1024;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_disable “MSIE[1-6].(?!.*SV1)”;
gzip_types text/xml text/plain text/css application/javascript application/xml application/json;
9. https模塊ngx_http_ssl_module模塊:
為指定虛擬機(jī)啟用HTTPS protocol, 建議用listen指令代替
當(dāng)前虛擬主機(jī)使用PEM格式的證書文件
當(dāng)前虛擬主機(jī)上與其證書匹配的私鑰文件
支持ssl協(xié)議版本,默認(rèn)為后三個(gè)
[shared:name:size];
builtin[:size]:使用OpenSSL內(nèi)建緩存,為每worker進(jìn)程私有
[shared:name:size]:在各worker之間使用一個(gè)共享的緩存
客戶端連接可以復(fù)用ssl session cache中緩存的ssl參數(shù)的有
效時(shí)長,默認(rèn)5m
示例:
server {
listen 443 ssl;
server_name www.magedu.com;
root /vhosts/ssl/htdocs;
ssl on;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
}
10. 重定向模塊ngx_http_rewrite_module:
1、rewrite regex replacement [flag]
將用戶請求的URI基于regex所描述的模式進(jìn)行檢查,匹配到時(shí)將其替換為replacement指定的新的URI
注意:如果在同一級配置塊中存在多個(gè)rewrite規(guī)則,那么會(huì)自下而下逐個(gè)檢查;被某條件規(guī)則替換
完成后,會(huì)重新一輪的替換檢查
隱含有循環(huán)機(jī)制,但不超過10次;如果超過,提示500響應(yīng)碼, [flag]所表示的標(biāo)志位用于控制此循環(huán)
機(jī)制
如果replacement是以http://或https://開頭,則替換結(jié)果會(huì)直接以重向返回給客戶端
[flag]:
last:重寫完成后停止對當(dāng)前URI在當(dāng)前l(fā)ocation中后續(xù)
的其它重寫操作,而后對新的URI啟動(dòng)新一輪重寫檢查;提前重
啟新一輪循環(huán)
break:重寫完成后停止對當(dāng)前URI在當(dāng)前l(fā)ocation中后
續(xù)的其它重寫操作,而后直接跳轉(zhuǎn)至重寫規(guī)則配置塊之后的其它
配置;結(jié)束循環(huán),建議在location中使用
redirect:臨時(shí)重定向,重寫完成后以臨時(shí)重定向方式直
接返回重寫后生成的新URI給客戶端,由客戶端重新發(fā)起請求;
不能以http://或https://開頭,使用相對路徑,狀態(tài)碼: 302
permanent:重寫完成后以永久重定向方式直接返回重寫
后生成的新URI給客戶端,由客戶端重新發(fā)起請求,狀態(tài)碼:301
例:
rewrite ^/zz/(.*.html)$ /zhengzhou/$1 break;
rewrite ^/zz/(.*.html)$ https://www.dianping/zhengzhou/$1 permanent;
return code [text];
return code URL;
return URL;
停止處理,并返回給客戶端指定的響應(yīng)碼
3、 rewrite_log on | off;
是否開啟重寫日志, 發(fā)送至error_log(notice level)
用戶自定義變量
注意:變量定義和調(diào)用都要以$開頭
引入新的上下文,條件滿足時(shí),執(zhí)行配置塊中的配置指令; server, location
condition:
比較操作符:
== 相同
!= 不同
~:模式匹配,區(qū)分字符大小寫
~*:模式匹配,不區(qū)分字符大小寫
!~:模式不匹配,區(qū)分字符大小寫
!~*:模式不匹配,不區(qū)分字符大小寫
文件及目錄存在性判斷:
-e, !-e 存在(包括文件,目錄,軟鏈接)
-f, !-f 文件
-d, !-d 目錄
-x, !-x 執(zhí)行
瀏覽器分流示例:
if ($http_user_agent ~ Chrom) {
rewrite ^(.*)$ /chrome/$1 break;
}
if ($http_user_agent ~ MSIE) {
rewrite ^(.*)$ /IE/$1 break;
}
11. 引用模塊ngx_http_referer_module
valid_referers none|blocked|server_names|string …;
定義referer首部的合法可用值,不能匹配的將是非法值
none:請求報(bào)文首部沒有referer首部
blocked:請求報(bào)文有referer首部,但無有效值
server_names:參數(shù),其可以有值作為主機(jī)名或主機(jī)名模式
arbitrary_string:任意字符串,但可使用*作通配符
regular expression:被指定的正則表達(dá)式模式匹配到的字符
串,要使用~開頭,例如: ~.*.magedu.com
示例:
valid_referers none block server_names *.magedu.com
*.mageedu.com magedu.* mageedu.* ~.magedu.;
if ($invalid_referer) {
return 403;
}
12. 反向代理模塊ngx_http_proxy_module
12.1 proxy_pass URL;
Context:location, if in location, limit_except
注意: proxy_pass后面的路徑不帶uri時(shí),其會(huì)將location的uri傳遞給后端主機(jī)
server {
…
server_name HOSTNAME;
location /uri/ {
proxy_pass http://host[:port];
}
…
}
上面示例: http://HOSTNAME/uri –> http://host/uri
http://host[:port]/ 意味著: http://HOSTNAME/uri –> http://host/
注意:如果location定義其uri時(shí)使用了正則表達(dá)式的模式,則proxy_pass之后必須不能使用uri;
用戶請求時(shí)傳遞的uri將直接附加代理到的服務(wù)的之后
server {
…
server_name HOSTNAME;
location ~|~* /uri/ {
proxy_pass http://host; 不能加/
}
…
}
http://HOSTNAME/uri/ –> http://host/uri/
12.2 proxy_set_header field value;
設(shè)定發(fā)往后端主機(jī)的請求報(bào)文的請求首部的值
Context: http, server, location
后端記錄日志記錄真實(shí)請求服務(wù)器IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
標(biāo)準(zhǔn)格式如下:
X-Forwarded-For: client1, proxy1, proxy2
如后端是Apache服務(wù)器應(yīng)更改日志格式:
%h —–> %{X-Real-IP}i
12.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];
例:proxy_cache_path /data/nginx/cache(屬主要為nginx) levels=1:2 keys_zone=nginxcache:20m inactive=2m
12.4 調(diào)用緩存
proxy_cache zone | off; 默認(rèn)off
指明調(diào)用的緩存,或關(guān)閉緩存機(jī)制; Context: http,server, location
12.5
proxy_cache_key string;
緩存中用于“鍵”的內(nèi)容
默認(rèn)值: proxy_cache_key $scheme$proxy_host$request_uri;
12.6
proxy_cache_valid [code …] time;
定義對特定響應(yīng)碼的響應(yīng)內(nèi)容的緩存時(shí)長
定義在http{…}中
示例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
示例:
在http配置定義緩存信
proxy_cache_path /var/cache/nginx/proxy_cache
levels=1:1:1 keys_zone=proxycache:20m
inactive=120s max_size=1g;
調(diào)用緩存功能,需要定義在相應(yīng)的配置段,如server{…};
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
12.7
proxy_cache_use_stale;
proxy_cache_use_stale error | timeout |
invalid_header | updating | http_500 | http_502 |
http_503 | http_504 | http_403 | http_404 | off …
在被代理的后端服務(wù)器出現(xiàn)哪種情況下,可以直接使用過
期的緩存響應(yīng)客戶端
12.8
proxy_cache_methods GET | HEAD | POST …;
對哪些客戶端請求方法對應(yīng)的響應(yīng)進(jìn)行緩存, GET和HEAD方法總是被緩存
12.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. 用于隱藏后端服
務(wù)器特定的響應(yīng)首部
12.10
proxy_connect_timeout time;
定義與后端服務(wù)器建立連接的超時(shí)時(shí)長,如超時(shí)會(huì)出現(xiàn)502錯(cuò)誤,默認(rèn)為60s,一般不建議超出75s
12.11
proxy_send_timeout time;
把請求發(fā)送給后端服務(wù)器的超時(shí)時(shí)長;默認(rèn)為60s
12.12
proxy_read_timeout time;
等待后端服務(wù)器發(fā)送響應(yīng)報(bào)文的超時(shí)時(shí)長, 默認(rèn)為60s
13. 首部信息
add_header name value [always];
添加自定義首部
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;
add_trailer name value [always];
添加自定義響應(yīng)信息的尾部
14. hph 相關(guān)模塊ngx_http_fastcgi_module
14.1
fastcgi_pass address;
address為后端的fastcgi server的地址
可用位置: location, if in location
14.2
fastcgi_index name;
fastcgi默認(rèn)的主頁資源
示例: fastcgi_index index.php;
14.3
fastcgi_param parameter value [if_not_empty];
設(shè)置傳遞給 FastCGI服務(wù)器的參數(shù)值,可以是文本,變
量或組合
示例1:
1)在后端服務(wù)器先配置fpm server和mariadb-server
2)在前端nginx服務(wù)上做以下配置:
location ~* .php$ {
fastcgi_pass 后端fpm服務(wù)器IP:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/usr/share/nginx/html$fastcgi_script_name;
include fastcgi.conf;
…
}
示例2:
通過/pm_status和/ping來獲取fpm server狀態(tài)信息(真實(shí)服務(wù)器端php-fpm配置文件中將這兩項(xiàng)
注釋掉)
location ~* ^/(status|ping)$ {
include fastcgi_params;
fastcgi_pass 后端fpm服務(wù)器IP:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi.conf;
}
14.4 fastcgi 緩存相關(guān)
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 緩存位置為磁盤上的文件系統(tǒng)
max_size=size
磁盤path路徑中用于緩存數(shù)據(jù)的緩存空間上限
levels=levels:緩存目錄的層級數(shù)量,以及每一級的目錄數(shù)量
levels=ONE:TWO:THREE
示例: leves=1:2:2
keys_zone=name:size
k/v映射的內(nèi)存空間的名稱及大小
inactive=time
非活動(dòng)時(shí)長
14.5
fastcgi_cache zone | off;
調(diào)用指定的緩存空間來緩存數(shù)據(jù)
可用位置: http, server, location
14.6
fastcgi_cache_key string;
定義用作緩存項(xiàng)的key的字符串
示例: fastcgi_cache_key $request_rui;
14.7
fastcgi_cache_methods GET | HEAD | POST …;
為哪些請求方法使用緩存
14.8
fastcgi_cache_min_uses number;
緩存空間中的緩存項(xiàng)在inactive定義的非活動(dòng)時(shí)間內(nèi)至少要被訪問到
此處所指定的次數(shù)方可被認(rèn)作活動(dòng)項(xiàng)
14.9
fastcgi_keep_conn on | off;
收到后端服務(wù)器響應(yīng)后, fastcgi服務(wù)器是否關(guān)閉連接,建議啟用長連接
14.10
fastcgi_cache_valid [code …] time;
不同的響應(yīng)碼各自的緩存時(shí)長
示例:http {
fastcgi_cache_path /var/cache/nginx/fcgi_cache
levels=1:2:1 keys_zone=fcgicache:20m inactive=120s;
…
server {
location ~* .php$ {
…
fastcgi_cache fcgicache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
…
}
}
15. 代理模塊ngx_http_upstream_module模塊
用于將多個(gè)服務(wù)器定義成服務(wù)器組,而由proxy_pass,fastcgi_pass等指令進(jìn)行引用
15.1upstream name { … }
定義后端服務(wù)器組,會(huì)引入一個(gè)新的上下文
默認(rèn)調(diào)度算法是wrr
Context: http
upstream httpdsrvs {
server …
server…
…
}
15.2
server address [parameters];
在upstream上下文中server成員,以及相關(guān)的參數(shù); Context:upstream
address的表示格式:
unix:/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT]
parameters:
weight=number 權(quán)重,默認(rèn)為1
max_conns 連接后端報(bào)務(wù)器最大并發(fā)活動(dòng)連接數(shù), 1.11.5后支持
max_fails=number 失敗嘗試最大次數(shù);超出此處指定的次數(shù)時(shí)
server將被標(biāo)記為不可用,默認(rèn)為1
fail_timeout=time 后端服務(wù)器標(biāo)記為不可用狀態(tài)的連接超時(shí)時(shí)
長,默認(rèn)10s
backup 將服務(wù)器標(biāo)記為“備用”,即所有服務(wù)器均不可用時(shí)才啟用
down 標(biāo)記為“不可用”,配合ip_hash使用,實(shí)現(xiàn)灰度發(fā)布
15.3
ip_hash 源地址hash調(diào)度方法
15.4
least_conn 最少連接調(diào)度算法,當(dāng)server擁有不同的權(quán)重時(shí)其為wlc,當(dāng)所有后端主機(jī)連接數(shù)相同時(shí),
則使用wrr,適用于長連接
15.5
hash key [consistent] 基于指定的key的hash表來實(shí)
現(xiàn)對請求的調(diào)度,此處的key可以直接文本、變量或二者組合
作用:將請求分類,同一類請求將發(fā)往同一個(gè)upstream
server,使用consistent參數(shù), 將使用ketama一致性hash算法,
適用于后端是Cache服務(wù)器(如varnish)時(shí)使用
hash $request_uri consistent;
hash $remote_addr;
15.6
keepalive 連接數(shù)N;
為每個(gè)worker進(jìn)程保留的空閑的長連接數(shù)量,可節(jié)約nginx
端口,并減少連接管理的消耗
15.7
health_check [parameters];
健康狀態(tài)檢測機(jī)制;只能用于location上下文
常用參數(shù):
interval=time檢測的頻率,默認(rèn)為5秒
fails=number:判定服務(wù)器不可用的失敗檢測次數(shù);默認(rèn)為1次
passes=number:判定服務(wù)器可用的失敗檢測次數(shù);默認(rèn)為1次
uri=uri:做健康狀態(tài)檢測測試的目標(biāo)uri;默認(rèn)為/
match=NAME:健康狀態(tài)檢測的結(jié)果評估調(diào)用此處指定的match配置塊
注意:僅對nginx plus有效
15.8
match name { … }
對backend server做健康狀態(tài)檢測時(shí),定義其結(jié)果判斷機(jī)制;
只能用于http上下文
常用的參數(shù):
status code[ code …]: 期望的響應(yīng)狀態(tài)碼
header HEADER[operator value]:期望存在響應(yīng)首
部,也可對期望的響應(yīng)首部的值基于比較操作符和值進(jìn)行比較
body:期望響應(yīng)報(bào)文的主體部分應(yīng)該有的內(nèi)容
注意:僅對nginx plus有效
16. ngx_stream_core_module模塊
模擬反代基于tcp或udp的服務(wù)連接,即工作于傳輸層的反代或調(diào)度器
stream { … }
定義stream相關(guān)的服務(wù); Context:main
stream {
upstream telnetsrvs {
server 192.168.22.2:23;
server 192.168.22.3:23;
least_conn;
}
server {
listen 10.1.0.6:23;
proxy_pass telnetsrvs;
}
}
listen address:port [ssl] [udp] [proxy_protocol]
[backlog=number] [bind] [ipv6only=on|off] [reuseport]
[so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
17. ngx_stream_proxy_module模塊
sockets的數(shù)據(jù)流
指定后端服務(wù)器地址
無數(shù)據(jù)傳輸時(shí),保持連接狀態(tài)的超時(shí)時(shí)長
默認(rèn)為10m
設(shè)置nginx與被代理的服務(wù)器嘗試建立連接的超時(shí)時(shí)長
默認(rèn)為60s
示例:
stream {
upstream telnetsrvs {
server 192.168.10.130:23;
server 192.168.10.131:23;
hash $remote_addr consistent;
根據(jù)客戶端地址進(jìn)行hash值調(diào)度,只要客戶端來自一個(gè)地址就一直調(diào)度過去,tcp,udp協(xié)議也可以往一個(gè)上面調(diào)度
}
server {
listen 172.16.100.10:2323;
proxy_pass telnetsrvs;
proxy_timeout 60s;
proxy_connect_timeout 10s;
}
}
集合: