1. 性能相關配置
1
2
|
worker_processes number | auto;
worker進程的數量;通常應該為當前主機的cpu的物理核心數
|
1
2
3
4
|
worker_cpu_affinity auto [cpumask]
#將work進程綁定在固定cpu上提高緩存命中率
例:
worker_cpu_affinity 0001 0010 0100 1000;
worker_cpu_affinity 0101 1010;
|
1
2
|
worker_priority number
指定worker進程的
nice
值,設定worker進程優先級: [-20,20]
|
1
2
|
worker_rlimit_nofile number
worker進程所能夠打開的文件數量上限,默認較小,生產中需要調大如65535
|
2. 時間驅動events相關的配置
1
2
3
|
worker_connections number
每個worker進程所能夠打開的最大並發連接數數量,如10240
總最大並發數: worker_processes * worker_connections
|
1
2
3
|
use method
指明並發連接請求的處理方法,默認自動選擇最優方法不用調整
如:use epoll;
|
1
2
3
4
|
accept_mutex on | off 互斥;
處理新的連接請求的方法; on指由各個worker輪流處理新請求
, Off指每個新請求的到達都會通知(喚醒)所有的worker進程,但
只有一個進程可獲得連接,造成“驚群”,影響性能,默認on
|
3. http核心模塊相關配置ngx_http_core_module
3.1web服務模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
server { ... }
配置一個虛擬主機
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root
/PATH/TO/DOCUMENT_ROOT
;
}
注意:
(1) 基於port;
listen PORT; 指令監聽在不同的端口
(2) 基於ip的虛擬主機
listen IP:PORT; IP 地址不同
(3) 基於
hostname
server_name fqdn; 指令指向不同的主機名
|
3.2套接字相關配置
1
2
3
4
5
6
7
|
listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
default_server 設定為默認虛擬主機
ssl 限制僅能夠通過ssl連接提供服務
backlog=number 超過並發連接數后,新請求進入后援隊列的長度
rcvbuf=size 接收緩沖區大小
sndbuf=size 發送緩沖區大小
|
3.3 server_name
1
2
3
4
5
6
7
8
9
10
11
|
server_name name ...;
支持*通配任意長度的任意字符
server_name *.magedu.com www.magedu.*
支持~起始的字符做正則表達式模式匹配,性能原因慎用
server_name ~^www\d+\.magedu\.com$
#\d 表示 [0-9]
匹配優先級機制從高到低:
(1) 首先是字符串精確匹配 如: www.magedu.com
(2) 左側*通配符 如: *.magedu.com
(3) 右側*通配符 如: www.magedu.*
(4) 正則表達式 如: ~^.*\.magedu\.com$
(5) default_server
|
3.4 延遲發送選項
1
2
3
4
5
6
|
tcp_nodelay on | off;
tcp_nopush on | off;
在keepalived模式下的連接是否啟用TCP_NODELAY選項
當為off時,延遲發送,合並多個請求后再發送
默認On時,不延遲發送
可用於: http, server, location
|
3.5 sendfile
1
2
3
|
sendfile on | off;
是否啟用sendfile功能,在內核中封裝報文直接發送
默認Off
|
3.6 隱藏版本信息
1
2
|
server_tokens on | off | build | string
是否在響應報文的Server首部顯示nginx版本
|
3.7 location匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
在一個server中location配置段可存在多個,用於實現從uri到文件系統的路徑映射; ngnix會根據
用戶請求的URI來檢查定義的所有location,並找出一個最佳匹配,而后應用其配置
示例:
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的最左邊部分做匹配檢查,不區分字符大小寫
~:對URI做正則表達式模式匹配,區分字符大小寫
~*:對URI做正則表達式模式匹配,不區分字符大小寫
不帶符號:匹配起始於此uri的所有的uri
匹配優先級從高到低:
=, ^~, ~/~*, 不帶符號
|
3.7 路徑別名alias path
1
2
3
4
5
6
7
8
9
10
11
|
示例:
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,相當於追加在root目錄后面
(b)
alias
,相當於對location中的url進行替換
|
3.8 錯誤頁面顯示
1
2
3
4
5
6
|
error_page code ... [=[response]] uri;
模塊: ngx_http_core_module
定義錯誤頁, 以指定的響應狀態碼進行響應
可用位置: http, server, location,
if
in
location
error_page 404
/404
.html
error_page 404 =200
/404
.html
#防止404頁面被劫持
|
3.9 長連接相關配置
1
2
3
4
5
6
7
8
9
|
keepalive_timeout timeout [header_timeout];
設定保持連接超時時長, 0表示禁止長連接, 默認為75s
keepalive_requests number;
在一次長連接上所允許請求的資源的最大數量,默認為100
keepalive_disable none | browser ...
對哪種瀏覽器禁用長連接
send_timeout
time
;
向客戶端發送響應報文的超時時長,此處是指兩次寫操作之間的間隔時長,而非
整個響應過程的傳輸時長
|
3.10 請求報文緩存
1
2
3
4
5
6
7
8
9
10
|
client_body_buffer_size size;
用於接收每個客戶端請求報文的body部分的緩沖區大小;默認為16k;超出此大小時,
其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置
client_body_temp_path path [level1 [level2 [level3]]];
設定用於存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量
目錄名為16進制的數字;
client_body_temp_path
/var/tmp/client_body
1 2 2
1 1級目錄占1位16進制,即2^4=16個目錄 0-f
2 2級目錄占2位16進制,即2^8=256個目錄 00-ff
2 3級目錄占2位16進制, 即2^8=256個目錄 00-ff
|
3.11 對客戶端進行限制相關配置
1
2
3
4
5
6
7
8
9
10
11
12
|
limit_rate rate;
限制響應給客戶端的傳輸速率,單位是bytes
/second
默認值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
網段主機使用
|
4. 訪問控制模塊ngx_http_access_module
實現基於ip的訪問控制功能
1
2
3
4
5
6
7
8
9
10
11
12
|
allow address | CIDR | unix: | all;
deny 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;
}
|
5. 用戶認證模塊ngx_http_auth_basic_module
實現基於用戶的訪問控制,使用basic機制進行用戶認證
1
2
3
4
5
6
7
8
9
10
|
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命令實現 httpd-tools所提供
htpasswd [-c第一次創建時使用] [-D刪除用戶] passwdfile username
|
6. 狀態查看模塊ngx_http_stub_status_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
用於輸出nginx的基本狀態信息
Active connections:當前狀態,活動狀態的連接數
accepts:統計總值,已經接受的客戶端請求的總數
handled:統計總值,已經處理完成的客戶端請求的總數
requests:統計總值,客戶端發來的總的請求數
Reading:當前狀態,正在讀取客戶端請求報文首部的連接的連接數
Writing:當前狀態,正在向客戶端發送響應報文過程中的連接數
Waiting:當前狀態,正在等待客戶端發出請求的空閑連接數
示例:
location
/status
{
stub_status;
allow 172.16.0.0
/16
;
deny all;
}
|
7. 日志記錄模塊ngx_http_log_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
1、 log_format name string ...;
string可以使用nginx核心模塊及其它模塊內嵌的變量
2、 access_log path [
format
[buffer=size] [
gzip
[=level]] [flush=
time
] [
if
=condition]];
access_log off;
訪問日志文件路徑,格式及相關的緩沖的配置
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;
緩存各日志文件相關的元數據信息
max:緩存的最大文件描述符數量
min_uses:在inactive指定的時長內訪問大於等於此值方可被當作活動項
inactive:非活動時長
valid:驗正緩存中各緩存項是否為活動項的時間間隔
例: open_log_file_cache max=1000 inactive=20s valid=1m;
|
8. 壓縮相關選項ngx_http_gzip_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
1、
gzip
on | off;
#啟用或禁用gzip壓縮
2、gzip_comp_level level;
#壓縮比由低到高: 1 到 9 默認: 1
3、gzip_disable regex ...;
#匹配到客戶端瀏覽器不執行壓縮
4、gzip_min_length length;
#啟用壓縮功能的響應報文大小閾值
5、gzip_http_version 1.0 | 1.1;
#設定啟用壓縮功能時,協議的最小版本 默認: 1.1
6、gzip_buffers number size;
支持實現壓縮功能時緩沖區數量及每個緩存區的大小
默認: 32 4k 或 16 8k
7、gzip_types mime-
type
...;
指明僅對哪些類型的資源執行壓縮操作;即壓縮過濾器
默認包含有text
/html
,不用顯示指定,否則出錯
8、gzip_vary on | off;
如果啟用壓縮,是否在響應報文首部插入“Vary: AcceptEncoding
9、 gzip_proxied off | expired | no-cache | no-store |
private | no_last_modified | no_etag | auth | any ...;
nginx對於代理服務器請求的響應報文,在何種條件下啟
用壓縮功能
off:對被代理的請求不啟用壓縮
expired,no-cache, no-store, private:對代理服務器
請求的響應報文首部Cache-Control值任何一個,啟用壓縮功能
示例:
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模塊:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
1、 ssl on | off;
為指定虛擬機啟用HTTPS protocol, 建議用listen指令代替
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參數的有
效時長,默認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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
1、rewrite regex replacement [flag]
將用戶請求的URI基於regex所描述的模式進行檢查,匹配到時將其替換為replacement指定的新的URI
注意:如果在同一級配置塊中存在多個rewrite規則,那么會自下而下逐個檢查;被某條件規則替換
完成后,會重新一輪的替換檢查
隱含有循環機制,但不超過10次;如果超過,提示500響應碼, [flag]所表示的標志位用於控制此循環
機制
如果replacement是以http:
//
或https:
//
開頭,則替換結果會直接以重向返回給客戶端
[flag]:
last:重寫完成后停止對當前URI在當前location中后續
的其它重寫操作,而后對新的URI啟動新一輪重寫檢查;提前重
啟新一輪循環
break
:重寫完成后停止對當前URI在當前location中后
續的其它重寫操作,而后直接跳轉至重寫規則配置塊之后的其它
配置;結束循環,建議在location中使用
redirect:臨時重定向,重寫完成后以臨時重定向方式直
接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;
不能以http:
//
或https:
//
開頭,使用相對路徑,狀態碼: 302
permanent:重寫完成后以永久重定向方式直接返回重寫
后生成的新URI給客戶端,由客戶端重新發起請求,狀態碼:301
例:
rewrite ^
/zz/
(.*\.html)$
/zhengzhou/
$1
break
;
rewrite ^
/zz/
(.*\.html)$ https:
//www
.dianping
/zhengzhou/
$1 permanent;
|
1
2
3
4
5
|
2、
return
return
code [text];
return
code URL;
return
URL;
停止處理,並返回給客戶端指定的響應碼
|
1
2
|
3、 rewrite_log on | off;
是否開啟重寫日志, 發送至error_log(notice level)
|
1
2
3
|
4、
set
$variable value;
用戶自定義變量
注意:變量定義和調用都要以$開頭
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
5、
if
(condition) { ... }
引入新的上下文,條件滿足時,執行配置塊中的配置指令; server, location
condition:
比較操作符:
== 相同
!= 不同
~:模式匹配,區分字符大小寫
~*:模式匹配,不區分字符大小寫
!~:模式不匹配,區分字符大小寫
!~*:模式不匹配,不區分字符大小寫
文件及目錄存在性判斷:
-e, !-e 存在(包括文件,目錄,軟鏈接)
-f, !-f 文件
-d, !-d 目錄
-x, !-x 執行
瀏覽器分流示例:
if
($http_user_agent ~ Chrom) {
rewrite ^(.*)$
/chrome/
$1
break
;
}
if
($http_user_agent ~ MSIE) {
rewrite ^(.*)$
/IE/
$1
break
;
}
|
11. 引用模塊ngx_http_referer_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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) {
return
403;
}
|
12. 反向代理模塊ngx_http_proxy_module
12.1 proxy_pass URL;
Context:location, if in location, limit_except
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
注意: proxy_pass后面的路徑不帶uri時,其會將location的uri傳遞給后端主機
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時使用了正則表達式的模式,則proxy_pass之后必須不能使用uri;
用戶請求時傳遞的uri將直接附加代理到的服務的之后
server {
...
server_name HOSTNAME;
location ~|~*
/uri/
{
proxy_pass http:
//host
; 不能加/
}
...
}
http:
//HOSTNAME/uri/
--> http:
//host/uri/
|
12.2 proxy_set_header field value;
設定發往后端主機的請求報文的請求首部的值
Context: http, server, location
1
2
3
4
5
6
7
8
|
后端記錄日志記錄真實請求服務器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;
標准格式如下:
X-Forwarded-For: client1, proxy1, proxy2
如后端是Apache服務器應更改日志格式:
%h -----> %{X-Real-IP}i
|
12.3 proxy_cache_path;
1
2
3
4
5
6
7
8
|
定義可用於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 調用緩存
1
2
|
proxy_cache zone | off; 默認off
指明調用的緩存,或關閉緩存機制; Context: http,server, location
|
12.5
1
2
3
|
proxy_cache_key string;
緩存中用於“鍵”的內容
默認值: proxy_cache_key $scheme$proxy_host$request_uri;
|
12.6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
proxy_cache_valid [code ...]
time
;
定義對特定響應碼的響應內容的緩存時長
定義在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;
調用緩存功能,需要定義在相應的配置段,如server{...};
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
|
12.7
1
2
3
4
5
6
|
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 ...
在被代理的后端服務器出現哪種情況下,可以直接使用過
期的緩存響應客戶端
|
12.8
1
2
|
proxy_cache_methods GET | HEAD | POST ...;
對哪些客戶端請求方法對應的響應進行緩存, GET和HEAD方法總是被緩存
|
12.9
1
2
3
4
5
|
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. 用於隱藏后端服
務器特定的響應首部
|
12.10
1
2
|
proxy_connect_timeout
time
;
定義與后端服務器建立連接的超時時長,如超時會出現502錯誤,默認為60s,一般不建議超出75s
|
12.11
1
2
|
proxy_send_timeout
time
;
把請求發送給后端服務器的超時時長;默認為60s
|
12.12
1
2
|
proxy_read_timeout
time
;
等待后端服務器發送響應報文的超時時長, 默認為60s
|
13. 首部信息
1
2
3
4
5
6
7
|
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];
添加自定義響應信息的尾部
|
14. hph 相關模塊ngx_http_fastcgi_module
14.1
1
2
3
|
fastcgi_pass address;
address為后端的fastcgi server的地址
可用位置: location,
if
in
location
|
14.2
1
2
3
|
fastcgi_index name;
fastcgi默認的主頁資源
示例: fastcgi_index index.php;
|
14.3
1
2
3
|
fastcgi_param parameter value [if_not_empty];
設置傳遞給 FastCGI服務器的參數值,可以是文本,變
量或組合
|
示例1:
1
2
3
4
5
6
7
8
9
10
|
1)在后端服務器先配置fpm server和mariadb-server
2)在前端nginx服務上做以下配置:
location ~* \.php$ {
fastcgi_pass 后端fpm服務器IP:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/usr/share/nginx/html
$fastcgi_script_name;
include fastcgi.conf;
…
}
|
示例2:
1
2
3
4
5
6
7
8
|
通過
/pm_status
和
/ping
來獲取fpm server狀態信息(真實服務器端php-fpm配置文件中將這兩項
注釋掉)
location ~* ^/(status|
ping
)$ {
include fastcgi_params;
fastcgi_pass 后端fpm服務器IP:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi.conf;
}
|
14.4 fastcgi 緩存相關
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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 緩存位置為磁盤上的文件系統
max_size=size
磁盤path路徑中用於緩存數據的緩存空間上限
levels=levels:緩存目錄的層級數量,以及每一級的目錄數量
levels=ONE:TWO:THREE
示例: leves=1:2:2
keys_zone=name:size
k
/v
映射的內存空間的名稱及大小
inactive=
time
非活動時長
|
14.5
1
2
3
|
fastcgi_cache zone | off;
調用指定的緩存空間來緩存數據
可用位置: http, server, location
|
14.6
1
2
3
|
fastcgi_cache_key string;
定義用作緩存項的key的字符串
示例: fastcgi_cache_key $request_rui;
|
14.7
1
2
|
fastcgi_cache_methods GET | HEAD | POST ...;
為哪些請求方法使用緩存
|
14.8
1
2
3
|
fastcgi_cache_min_uses number;
緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到
此處所指定的次數方可被認作活動項
|
14.9
1
2
|
fastcgi_keep_conn on | off;
收到后端服務器響應后, fastcgi服務器是否關閉連接,建議啟用長連接
|
14.10
1
2
|
fastcgi_cache_valid [code ...]
time
;
不同的響應碼各自的緩存時長
|
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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模塊
用於將多個服務器定義成服務器組,而由proxy_pass,fastcgi_pass等指令進行引用
15.1
1
2
3
4
5
6
7
8
9
|
upstream name { ... }
定義后端服務器組,會引入一個新的上下文
默認調度算法是wrr
Context: http
upstream httpdsrvs {
server ...
server...
...
}
|
15.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
server address [parameters];
在upstream上下文中server成員,以及相關的參數; Context:upstream
address的表示格式:
unix:
/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT]
parameters:
weight=number 權重,默認為1
max_conns 連接后端報務器最大並發活動連接數, 1.11.5后支持
max_fails=number 失敗嘗試最大次數;超出此處指定的次數時
server將被標記為不可用,默認為1
fail_timeout=
time
后端服務器標記為不可用狀態的連接超時時
長,默認10s
backup 將服務器標記為“備用”,即所有服務器均不可用時才啟用
down 標記為“不可用”,配合ip_hash使用,實現灰度發布
|
15.3
1
|
ip_hash 源地址
hash
調度方法
|
15.4
1
2
|
least_conn 最少連接調度算法,當server擁有不同的權重時其為wlc,當所有后端主機連接數相同時,
則使用wrr,適用於長連接
|
15.5
1
2
3
4
5
6
7
|
hash
key [consistent] 基於指定的key的
hash
表來實
現對請求的調度,此處的key可以直接文本、變量或二者組合
作用:將請求分類,同一類請求將發往同一個upstream
server,使用consistent參數, 將使用ketama一致性
hash
算法,
適用於后端是Cache服務器(如varnish)時使用
hash
$request_uri consistent;
hash
$remote_addr;
|
15.6
1
2
3
|
keepalive 連接數N;
為每個worker進程保留的空閑的長連接數量,可節約nginx
端口,並減少連接管理的消耗
|
15.7
1
2
3
4
5
6
7
8
9
|
health_check [parameters];
健康狀態檢測機制;只能用於location上下文
常用參數:
interval=
time
檢測的頻率,默認為5秒
fails=number:判定服務器不可用的失敗檢測次數;默認為1次
passes=number:判定服務器可用的失敗檢測次數;默認為1次
uri=uri:做健康狀態檢測測試的目標uri;默認為/
match=NAME:健康狀態檢測的結果評估調用此處指定的match配置塊
注意:僅對nginx plus有效
|
15.8
1
2
3
4
5
6
7
8
9
|
match name { ... }
對backend server做健康狀態檢測時,定義其結果判斷機制;
只能用於http上下文
常用的參數:
status code[ code ...]: 期望的響應狀態碼
header HEADER[operator value]:期望存在響應首
部,也可對期望的響應首部的值基於比較操作符和值進行比較
body:期望響應報文的主體部分應該有的內容
注意:僅對nginx plus有效
|
16. ngx_stream_core_module模塊
模擬反代基於tcp或udp的服務連接,即工作於傳輸層的反代或調度器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
stream { ... }
定義stream相關的服務; 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模塊
1
2
3
4
5
6
7
8
9
10
|
可實現代理基於TCP, UDP (1.9.13), UNIX-domain
sockets的數據流
1 proxy_pass address;
指定后端服務器地址
2 proxy_timeout timeout;
無數據傳輸時,保持連接狀態的超時時長
默認為10m
3 proxy_connect_timeout
time
;
設置nginx與被代理的服務器嘗試建立連接的超時時長
默認為60s
|
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
stream {
upstream telnetsrvs {
server 192.168.10.130:23;
server 192.168.10.131:23;
hash
$remote_addr consistent;
}
server {
listen 172.16.100.10:2323;
proxy_pass telnetsrvs;
proxy_timeout 60s;
proxy_connect_timeout 10s;
}
}
|
1. 性能相關配置
1
2
|
worker_processes number | auto;
worker進程的數量;通常應該為當前主機的cpu的物理核心數
|
1
2
3
4
|
worker_cpu_affinity auto [cpumask]
#將work進程綁定在固定cpu上提高緩存命中率
例:
worker_cpu_affinity 0001 0010 0100 1000;
worker_cpu_affinity 0101 1010;
|
1
2
|
worker_priority number
指定worker進程的
nice
值,設定worker進程優先級: [-20,20]
|
1
2
|
worker_rlimit_nofile number
worker進程所能夠打開的文件數量上限,默認較小,生產中需要調大如65535
|
2. 時間驅動events相關的配置
1
2
3
|
worker_connections number
每個worker進程所能夠打開的最大並發連接數數量,如10240
總最大並發數: worker_processes * worker_connections
|
1
2
3
|
use method
指明並發連接請求的處理方法,默認自動選擇最優方法不用調整
如:use epoll;
|
1
2
3
4
|
accept_mutex on | off 互斥;
處理新的連接請求的方法; on指由各個worker輪流處理新請求
, Off指每個新請求的到達都會通知(喚醒)所有的worker進程,但
只有一個進程可獲得連接,造成“驚群”,影響性能,默認on
|
3. http核心模塊相關配置ngx_http_core_module
3.1web服務模板
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
server { ... }
配置一個虛擬主機
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root
/PATH/TO/DOCUMENT_ROOT
;
}
注意:
(1) 基於port;
listen PORT; 指令監聽在不同的端口
(2) 基於ip的虛擬主機
listen IP:PORT; IP 地址不同
(3) 基於
hostname
server_name fqdn; 指令指向不同的主機名
|
3.2套接字相關配置
1
2
3
4
5
6
7
|
listen address[:port] [default_server] [ssl] [http2 | spdy] [backlog=number] [rcvbuf=size] [sndbuf=size]
default_server 設定為默認虛擬主機
ssl 限制僅能夠通過ssl連接提供服務
backlog=number 超過並發連接數后,新請求進入后援隊列的長度
rcvbuf=size 接收緩沖區大小
sndbuf=size 發送緩沖區大小
|
3.3 server_name
1
2
3
4
5
6
7
8
9
10
11
|
server_name name ...;
支持*通配任意長度的任意字符
server_name *.magedu.com www.magedu.*
支持~起始的字符做正則表達式模式匹配,性能原因慎用
server_name ~^www\d+\.magedu\.com$
#\d 表示 [0-9]
匹配優先級機制從高到低:
(1) 首先是字符串精確匹配 如: www.magedu.com
(2) 左側*通配符 如: *.magedu.com
(3) 右側*通配符 如: www.magedu.*
(4) 正則表達式 如: ~^.*\.magedu\.com$
(5) default_server
|
3.4 延遲發送選項
1
2
3
4
5
6
|
tcp_nodelay on | off;
tcp_nopush on | off;
在keepalived模式下的連接是否啟用TCP_NODELAY選項
當為off時,延遲發送,合並多個請求后再發送
默認On時,不延遲發送
可用於: http, server, location
|
3.5 sendfile
1
2
3
|
sendfile on | off;
是否啟用sendfile功能,在內核中封裝報文直接發送
默認Off
|
3.6 隱藏版本信息
1
2
|
server_tokens on | off | build | string
是否在響應報文的Server首部顯示nginx版本
|
3.7 location匹配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
在一個server中location配置段可存在多個,用於實現從uri到文件系統的路徑映射; ngnix會根據
用戶請求的URI來檢查定義的所有location,並找出一個最佳匹配,而后應用其配置
示例:
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的最左邊部分做匹配檢查,不區分字符大小寫
~:對URI做正則表達式模式匹配,區分字符大小寫
~*:對URI做正則表達式模式匹配,不區分字符大小寫
不帶符號:匹配起始於此uri的所有的uri
匹配優先級從高到低:
=, ^~, ~/~*, 不帶符號
|
3.7 路徑別名alias path
1
2
3
4
5
6
7
8
9
10
11
|
示例:
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,相當於追加在root目錄后面
(b)
alias
,相當於對location中的url進行替換
|
3.8 錯誤頁面顯示
1
2
3
4
5
6
|
error_page code ... [=[response]] uri;
模塊: ngx_http_core_module
定義錯誤頁, 以指定的響應狀態碼進行響應
可用位置: http, server, location,
if
in
location
error_page 404
/404
.html
error_page 404 =200
/404
.html
#防止404頁面被劫持
|
3.9 長連接相關配置
1
2
3
4
5
6
7
8
9
|
keepalive_timeout timeout [header_timeout];
設定保持連接超時時長, 0表示禁止長連接, 默認為75s
keepalive_requests number;
在一次長連接上所允許請求的資源的最大數量,默認為100
keepalive_disable none | browser ...
對哪種瀏覽器禁用長連接
send_timeout
time
;
向客戶端發送響應報文的超時時長,此處是指兩次寫操作之間的間隔時長,而非
整個響應過程的傳輸時長
|
3.10 請求報文緩存
1
2
3
4
5
6
7
8
9
10
|
client_body_buffer_size size;
用於接收每個客戶端請求報文的body部分的緩沖區大小;默認為16k;超出此大小時,
其將被暫存到磁盤上的由client_body_temp_path指令所定義的位置
client_body_temp_path path [level1 [level2 [level3]]];
設定用於存儲客戶端請求報文的body部分的臨時存儲路徑及子目錄結構和數量
目錄名為16進制的數字;
client_body_temp_path
/var/tmp/client_body
1 2 2
1 1級目錄占1位16進制,即2^4=16個目錄 0-f
2 2級目錄占2位16進制,即2^8=256個目錄 00-ff
2 3級目錄占2位16進制, 即2^8=256個目錄 00-ff
|
3.11 對客戶端進行限制相關配置
1
2
3
4
5
6
7
8
9
10
11
12
|
limit_rate rate;
限制響應給客戶端的傳輸速率,單位是bytes
/second
默認值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
網段主機使用
|
4. 訪問控制模塊ngx_http_access_module
實現基於ip的訪問控制功能
1
2
3
4
5
6
7
8
9
10
11
12
|
allow address | CIDR | unix: | all;
deny 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;
}
|
5. 用戶認證模塊ngx_http_auth_basic_module
實現基於用戶的訪問控制,使用basic機制進行用戶認證
1
2
3
4
5
6
7
8
9
10
|
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命令實現 httpd-tools所提供
htpasswd [-c第一次創建時使用] [-D刪除用戶] passwdfile username
|
6. 狀態查看模塊ngx_http_stub_status_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
用於輸出nginx的基本狀態信息
Active connections:當前狀態,活動狀態的連接數
accepts:統計總值,已經接受的客戶端請求的總數
handled:統計總值,已經處理完成的客戶端請求的總數
requests:統計總值,客戶端發來的總的請求數
Reading:當前狀態,正在讀取客戶端請求報文首部的連接的連接數
Writing:當前狀態,正在向客戶端發送響應報文過程中的連接數
Waiting:當前狀態,正在等待客戶端發出請求的空閑連接數
示例:
location
/status
{
stub_status;
allow 172.16.0.0
/16
;
deny all;
}
|
7. 日志記錄模塊ngx_http_log_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
1、 log_format name string ...;
string可以使用nginx核心模塊及其它模塊內嵌的變量
2、 access_log path [
format
[buffer=size] [
gzip
[=level]] [flush=
time
] [
if
=condition]];
access_log off;
訪問日志文件路徑,格式及相關的緩沖的配置
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;
緩存各日志文件相關的元數據信息
max:緩存的最大文件描述符數量
min_uses:在inactive指定的時長內訪問大於等於此值方可被當作活動項
inactive:非活動時長
valid:驗正緩存中各緩存項是否為活動項的時間間隔
例: open_log_file_cache max=1000 inactive=20s valid=1m;
|
8. 壓縮相關選項ngx_http_gzip_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
1、
gzip
on | off;
#啟用或禁用gzip壓縮
2、gzip_comp_level level;
#壓縮比由低到高: 1 到 9 默認: 1
3、gzip_disable regex ...;
#匹配到客戶端瀏覽器不執行壓縮
4、gzip_min_length length;
#啟用壓縮功能的響應報文大小閾值
5、gzip_http_version 1.0 | 1.1;
#設定啟用壓縮功能時,協議的最小版本 默認: 1.1
6、gzip_buffers number size;
支持實現壓縮功能時緩沖區數量及每個緩存區的大小
默認: 32 4k 或 16 8k
7、gzip_types mime-
type
...;
指明僅對哪些類型的資源執行壓縮操作;即壓縮過濾器
默認包含有text
/html
,不用顯示指定,否則出錯
8、gzip_vary on | off;
如果啟用壓縮,是否在響應報文首部插入“Vary: AcceptEncoding
9、 gzip_proxied off | expired | no-cache | no-store |
private | no_last_modified | no_etag | auth | any ...;
nginx對於代理服務器請求的響應報文,在何種條件下啟
用壓縮功能
off:對被代理的請求不啟用壓縮
expired,no-cache, no-store, private:對代理服務器
請求的響應報文首部Cache-Control值任何一個,啟用壓縮功能
示例:
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模塊:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
1、 ssl on | off;
為指定虛擬機啟用HTTPS protocol, 建議用listen指令代替
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參數的有
效時長,默認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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
1、rewrite regex replacement [flag]
將用戶請求的URI基於regex所描述的模式進行檢查,匹配到時將其替換為replacement指定的新的URI
注意:如果在同一級配置塊中存在多個rewrite規則,那么會自下而下逐個檢查;被某條件規則替換
完成后,會重新一輪的替換檢查
隱含有循環機制,但不超過10次;如果超過,提示500響應碼, [flag]所表示的標志位用於控制此循環
機制
如果replacement是以http:
//
或https:
//
開頭,則替換結果會直接以重向返回給客戶端
[flag]:
last:重寫完成后停止對當前URI在當前location中后續
的其它重寫操作,而后對新的URI啟動新一輪重寫檢查;提前重
啟新一輪循環
break
:重寫完成后停止對當前URI在當前location中后
續的其它重寫操作,而后直接跳轉至重寫規則配置塊之后的其它
配置;結束循環,建議在location中使用
redirect:臨時重定向,重寫完成后以臨時重定向方式直
接返回重寫后生成的新URI給客戶端,由客戶端重新發起請求;
不能以http:
//
或https:
//
開頭,使用相對路徑,狀態碼: 302
permanent:重寫完成后以永久重定向方式直接返回重寫
后生成的新URI給客戶端,由客戶端重新發起請求,狀態碼:301
例:
rewrite ^
/zz/
(.*\.html)$
/zhengzhou/
$1
break
;
rewrite ^
/zz/
(.*\.html)$ https:
//www
.dianping
/zhengzhou/
$1 permanent;
|
1
2
3
4
5
|
2、
return
return
code [text];
return
code URL;
return
URL;
停止處理,並返回給客戶端指定的響應碼
|
1
2
|
3、 rewrite_log on | off;
是否開啟重寫日志, 發送至error_log(notice level)
|
1
2
3
|
4、
set
$variable value;
用戶自定義變量
注意:變量定義和調用都要以$開頭
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
5、
if
(condition) { ... }
引入新的上下文,條件滿足時,執行配置塊中的配置指令; server, location
condition:
比較操作符:
== 相同
!= 不同
~:模式匹配,區分字符大小寫
~*:模式匹配,不區分字符大小寫
!~:模式不匹配,區分字符大小寫
!~*:模式不匹配,不區分字符大小寫
文件及目錄存在性判斷:
-e, !-e 存在(包括文件,目錄,軟鏈接)
-f, !-f 文件
-d, !-d 目錄
-x, !-x 執行
瀏覽器分流示例:
if
($http_user_agent ~ Chrom) {
rewrite ^(.*)$
/chrome/
$1
break
;
}
if
($http_user_agent ~ MSIE) {
rewrite ^(.*)$
/IE/
$1
break
;
}
|
11. 引用模塊ngx_http_referer_module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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) {
return
403;
}
|
12. 反向代理模塊ngx_http_proxy_module
12.1 proxy_pass URL;
Context:location, if in location, limit_except
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
注意: proxy_pass后面的路徑不帶uri時,其會將location的uri傳遞給后端主機
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時使用了正則表達式的模式,則proxy_pass之后必須不能使用uri;
用戶請求時傳遞的uri將直接附加代理到的服務的之后
server {
...
server_name HOSTNAME;
location ~|~*
/uri/
{
proxy_pass http:
//host
; 不能加/
}
...
}
http:
//HOSTNAME/uri/
--> http:
//host/uri/
|
12.2 proxy_set_header field value;
設定發往后端主機的請求報文的請求首部的值
Context: http, server, location
1
2
3
4
5
6
7
8
|
后端記錄日志記錄真實請求服務器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;
標准格式如下:
X-Forwarded-For: client1, proxy1, proxy2
如后端是Apache服務器應更改日志格式:
%h -----> %{X-Real-IP}i
|
12.3 proxy_cache_path;
1
2
3
4
5
6
7
8
|
定義可用於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 調用緩存
1
2
|
proxy_cache zone | off; 默認off
指明調用的緩存,或關閉緩存機制; Context: http,server, location
|
12.5
1
2
3
|
proxy_cache_key string;
緩存中用於“鍵”的內容
默認值: proxy_cache_key $scheme$proxy_host$request_uri;
|
12.6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
proxy_cache_valid [code ...]
time
;
定義對特定響應碼的響應內容的緩存時長
定義在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;
調用緩存功能,需要定義在相應的配置段,如server{...};
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
|
12.7
1
2
3
4
5
6
|
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 ...
在被代理的后端服務器出現哪種情況下,可以直接使用過
期的緩存響應客戶端
|
12.8
1
2
|
proxy_cache_methods GET | HEAD | POST ...;
對哪些客戶端請求方法對應的響應進行緩存, GET和HEAD方法總是被緩存
|
12.9
1
2
3
4
5
|
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. 用於隱藏后端服
務器特定的響應首部
|
12.10
1
2
|
proxy_connect_timeout
time
;
定義與后端服務器建立連接的超時時長,如超時會出現502錯誤,默認為60s,一般不建議超出75s
|
12.11
1
2
|
proxy_send_timeout
time
;
把請求發送給后端服務器的超時時長;默認為60s
|
12.12
1
2
|
proxy_read_timeout
time
;
等待后端服務器發送響應報文的超時時長, 默認為60s
|
13. 首部信息
1
2
3
4
5
6
7
|
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];
添加自定義響應信息的尾部
|
14. hph 相關模塊ngx_http_fastcgi_module
14.1
1
2
3
|
fastcgi_pass address;
address為后端的fastcgi server的地址
可用位置: location,
if
in
location
|
14.2
1
2
3
|
fastcgi_index name;
fastcgi默認的主頁資源
示例: fastcgi_index index.php;
|
14.3
1
2
3
|
fastcgi_param parameter value [if_not_empty];
設置傳遞給 FastCGI服務器的參數值,可以是文本,變
量或組合
|
示例1:
1
2
3
4
5
6
7
8
9
10
|
1)在后端服務器先配置fpm server和mariadb-server
2)在前端nginx服務上做以下配置:
location ~* \.php$ {
fastcgi_pass 后端fpm服務器IP:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/usr/share/nginx/html
$fastcgi_script_name;
include fastcgi.conf;
…
}
|
示例2:
1
2
3
4
5
6
7
8
|
通過
/pm_status
和
/ping
來獲取fpm server狀態信息(真實服務器端php-fpm配置文件中將這兩項
注釋掉)
location ~* ^/(status|
ping
)$ {
include fastcgi_params;
fastcgi_pass 后端fpm服務器IP:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
include fastcgi.conf;
}
|
14.4 fastcgi 緩存相關
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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 緩存位置為磁盤上的文件系統
max_size=size
磁盤path路徑中用於緩存數據的緩存空間上限
levels=levels:緩存目錄的層級數量,以及每一級的目錄數量
levels=ONE:TWO:THREE
示例: leves=1:2:2
keys_zone=name:size
k
/v
映射的內存空間的名稱及大小
inactive=
time
非活動時長
|
14.5
1
2
3
|
fastcgi_cache zone | off;
調用指定的緩存空間來緩存數據
可用位置: http, server, location
|
14.6
1
2
3
|
fastcgi_cache_key string;
定義用作緩存項的key的字符串
示例: fastcgi_cache_key $request_rui;
|
14.7
1
2
|
fastcgi_cache_methods GET | HEAD | POST ...;
為哪些請求方法使用緩存
|
14.8
1
2
3
|
fastcgi_cache_min_uses number;
緩存空間中的緩存項在inactive定義的非活動時間內至少要被訪問到
此處所指定的次數方可被認作活動項
|
14.9
1
2
|
fastcgi_keep_conn on | off;
收到后端服務器響應后, fastcgi服務器是否關閉連接,建議啟用長連接
|
14.10
1
2
|
fastcgi_cache_valid [code ...]
time
;
不同的響應碼各自的緩存時長
|
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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模塊
用於將多個服務器定義成服務器組,而由proxy_pass,fastcgi_pass等指令進行引用
15.1
1
2
3
4
5
6
7
8
9
|
upstream name { ... }
定義后端服務器組,會引入一個新的上下文
默認調度算法是wrr
Context: http
upstream httpdsrvs {
server ...
server...
...
}
|
15.2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
server address [parameters];
在upstream上下文中server成員,以及相關的參數; Context:upstream
address的表示格式:
unix:
/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT]
parameters:
weight=number 權重,默認為1
max_conns 連接后端報務器最大並發活動連接數, 1.11.5后支持
max_fails=number 失敗嘗試最大次數;超出此處指定的次數時
server將被標記為不可用,默認為1
fail_timeout=
time
后端服務器標記為不可用狀態的連接超時時
長,默認10s
backup 將服務器標記為“備用”,即所有服務器均不可用時才啟用
down 標記為“不可用”,配合ip_hash使用,實現灰度發布
|
15.3
1
|
ip_hash 源地址
hash
調度方法
|
15.4
1
2
|
least_conn 最少連接調度算法,當server擁有不同的權重時其為wlc,當所有后端主機連接數相同時,
則使用wrr,適用於長連接
|
15.5
1
2
3
4
5
6
7
|
hash
key [consistent] 基於指定的key的
hash
表來實
現對請求的調度,此處的key可以直接文本、變量或二者組合
作用:將請求分類,同一類請求將發往同一個upstream
server,使用consistent參數, 將使用ketama一致性
hash
算法,
適用於后端是Cache服務器(如varnish)時使用
hash
$request_uri consistent;
hash
$remote_addr;
|
15.6
1
2
3
|
keepalive 連接數N;
為每個worker進程保留的空閑的長連接數量,可節約nginx
端口,並減少連接管理的消耗
|
15.7
1
2
3
4
5
6
7
8
9
|
health_check [parameters];
健康狀態檢測機制;只能用於location上下文
常用參數:
interval=
time
檢測的頻率,默認為5秒
fails=number:判定服務器不可用的失敗檢測次數;默認為1次
passes=number:判定服務器可用的失敗檢測次數;默認為1次
uri=uri:做健康狀態檢測測試的目標uri;默認為/
match=NAME:健康狀態檢測的結果評估調用此處指定的match配置塊
注意:僅對nginx plus有效
|
15.8
1
2
3
4
5
6
7
8
9
|
match name { ... }
對backend server做健康狀態檢測時,定義其結果判斷機制;
只能用於http上下文
常用的參數:
status code[ code ...]: 期望的響應狀態碼
header HEADER[operator value]:期望存在響應首
部,也可對期望的響應首部的值基於比較操作符和值進行比較
body:期望響應報文的主體部分應該有的內容
注意:僅對nginx plus有效
|
16. ngx_stream_core_module模塊
模擬反代基於tcp或udp的服務連接,即工作於傳輸層的反代或調度器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
stream { ... }
定義stream相關的服務; 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模塊
1
2
3
4
5
6
7
8
9
10
|
可實現代理基於TCP, UDP (1.9.13), UNIX-domain
sockets的數據流
1 proxy_pass address;
指定后端服務器地址
2 proxy_timeout timeout;
無數據傳輸時,保持連接狀態的超時時長
默認為10m
3 proxy_connect_timeout
time
;
設置nginx與被代理的服務器嘗試建立連接的超時時長
默認為60s
|
示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
stream {
upstream telnetsrvs {
server 192.168.10.130:23;
server 192.168.10.131:23;
hash
$remote_addr consistent;
}
server {
listen 172.16.100.10:2323;
proxy_pass telnetsrvs;
proxy_timeout 60s;
proxy_connect_timeout 10s;
}
}
|