LB Cluster:
傳輸層:lvs、nginx、haproxy
應用層:nginx(http, https, smtp, pop, imap), haproxy(http), httpd(http/https), ats, perlbal, pound, …
nginx load balancer:
tcp/udp
nginx proxy:
reverse proxy:
應用程序發布:
灰度模型:
(1) 如果存在用戶會話;
從服務器上拆除會話;
(2) 新版本應用程序存在bug;
回滾;
ngx_http_proxy_module
(1) proxy_pass URL;
location, if in location, limit_except
注意:proxy_pass后面的路徑不帶uri時,其會將location的uri傳遞給后端主機;
location /uri/ {
proxy_pass http://HOST;
}
proxy_pass后面的路徑是一個uri時,其會將location的uri替換為proxy_pass的uri;
location /uri/ {
proxy_pass http://HOST/new_uri/;
}
如果location定義其uri時使用正則表達式的模式,則proxy_pass之后必須不能使用uri;
location ~|~* PATTERN {
proxy_pass http://HOST;
}
(2) proxy_set_header field value;
設定發往后端主機的請求報文的請求首部的值;
示例:
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for
練習步驟:
第一步:vim /etc/httpd/conf/httpd.conf
第二步:
第三步:刷新頁面并,檢查
(3) proxy_cache_path
proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
練習:
第一步:vim /etc/nginx/nginx.conf
第二步:vim /etc/nginx/conf.d/default.conf
(4) proxy_cache zone | off;
調用的緩存的名稱,或禁用緩存;
(5) proxy_cache_key string;
緩存條目的鍵;
(6) proxy_cache_valid [code …] time;
對各類響應碼的緩存時長;
使用示例:
定義在http{}中:
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:1 keys_zone=pcache:10m max_size=1g;
定義在server{}及其內部的組件中:
proxy_cache pcache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;
(7) proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off …;
(8) proxy_connect_timeout
proxy_read_timeout
proxy_send_timeout
(9) proxy_buffer_size
proxy_buffering
proxy_buffers
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];
向響應報文中添加自定義首部;
可用上下文:http, server, location, if in location
add_header X-Via $server_addr;
add_header X-Accel $server_name;
沒有添加自定義首部之前的一些狀態信息
添加了狀態信息后
(2) expires [modified] time;
expires epoch | max | off;
用于定義Expire或Cache-Control首部的值,或添加其它自定義首部;
回顧:
nginx:
web server
http/https reverse proxy
tcp/udp upstream server
ngx_http_proxy_module
proxy_pass
proxy_set_header
proxy_cache_path
proxy_cache
proxy_cache_key
proxy_cache_valid
proxy_connect_timeout, proxy_read_timeout, proxy_send_timeout
ngx_http_headers_module
add_header
ngx_http_upstream_module
The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives.
(1) upstream name { … }
定義后端服務器組;引入一個新的上下文;只能用于http{}上下文中;
首先,在/etc/nginx/conf.d/default.conf中定義
然后在/etc/nginx/nginx.conf中定義
測試
(2) server address [parameters];
定義服務器地址和相關的參數;
地址格式:
IP[:PORT]
HOSTNAME[:PORT]
unix:/PATH/TO/SOME_SOCK_FILE
參數:
weight=number
權重,默認為1;
max_fails=number
失敗嘗試的最大次數;
fail_timeout=time
設置服務器為不可用狀態的超時時長;
backup
把服務器標記為“備用”狀態;
down
手動標記其為不可用;
(3) least_conn;
最少連接調度算法; 當server擁有不同的權重時為wlc;
(4) least_time header | last_byte;
最短平均響應時長和最少連接;
header:response_header;
last_byte: full_response;
僅Nginx Plus有效;
(5) ip_hash;
源地址hash算法;能夠將來自同一個源IP地址的請求始終發往同一個upstream server;
(6) hash key [consistent];
基于指定的key的hash表實現請求調度,此處的key可以文本、變量或二者的組合;
consistent:參數,指定使用一致性hash算法;
示例:
hash $request_uri consistent
hash $remote_addr
hash $cookie_name
(7) keepalive connections;
可使用長連接的連接數量;
(8) health_check [parameters];
定義對后端主機的健康狀態檢測機制;只能用于location上下文;
可用參數:
interval=time:檢測頻率,默認為每隔5秒鐘;
fails=number:判斷服務器狀態轉為失敗需要檢測的次數;
passes=number:判斷服務器狀態轉為成功需要檢測的次數;
uri=uri:判斷其健康與否時使用的uri;
match=name:基于指定的match來衡量檢測結果的成??;
port=number:使用獨立的端口進行檢測;
僅Nginx Plus有效;
(9) match name { … }
Defines the named test set used to verify responses to health check requests.
定義衡量某檢測結果是否為成功的衡量機制;
專用指令:
status:期望的響應碼;
status CODE
status ! CODE
…
header:基于響應報文的首部進行判斷
header HEADER=VALUE
header HEADER ~ VALUE
…
body:基于響應報文的內容進行判斷
body ~ "PATTERN"
body !~ "PATTERN"
僅Nginx Plus有效;
原創文章,作者:178babyhanggege,如若轉載,請注明出處:http://www.www58058.com/56352