Linux nginx服務之反向代理
Nginx服務之反向代理:
GSLB:Global Service LB 全局服務負載均衡:
SLB:Service LB
應用程序發布:
灰度模型:
ngx_http_proxy_module模塊:
1、proxy_pass URL;
Context: location, if in location, limit_except
注意:proxy_pass后面的路徑不帶uri時,其會將location的uri傳遞給后端主機;
server {
…
server_name HOSTNAME;
location /uri/ {
proxy_pass http://hos[:port];
}
…
}
http://HOSTNAME/uri –> http://host/uri
proxy_pass后面的路徑是一個uri時,其會將location的uri替換為proxy_pass的uri;
server {
…
server_name HOSTNAME;
location /uri/ {
proxy_pass http://host/new_uri/;
}
…
}
http://HOSTNAME/uri/ –> http://host/new_uri/
如果location定義其uri時使用了正則表達式的模式,則proxy_pass之后必須不能使用uri; 用戶請求時傳遞的uri將直接附加代理到的服務的之后;
server {
…
server_name HOSTNAME;
location ~|~* /uri/ {
proxy_pass http://host;
}
…
}
http://HOSTNAME/uri/ –> http://host/uri/;
2、proxy_set_header field value;
設定發往后端主機的請求報文的請求首部的值;Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
示例:
讓后臺realserver主機記錄客戶端訪問真正的地址,而不是nginx反向代理服務器的IP:
在nginx反向代理服務器上設置:
在后臺realserver httpd服務器上設置LogFormat
3、proxy_cache_path :只能用于http配置段中;
定義可用于proxy功能的緩存;Context: http
proxy_cache_path path[levels=levels] keys_zone=name:size [inactive=time] [max_size=size]
4、proxy_cache zone | off;
指明要調用的緩存,或關閉緩存機制;Context: http, server, location
5、 proxy_cache_key string;
緩存中用于“鍵”的內容;
默認值:proxy_cache_key $scheme$proxy_host$request_uri;
6、proxy_cache_valid [code …] time;
定義對特定響應碼的響應內容的緩存時長;
示例:
在nginx主配置文件中定義可用于proxy功能的緩存:
在nginx默認配置文件中指明要調用的緩存或關閉緩存:
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 …;
當后端服務器出現故障時,后端服務器是何種故障才提供緩存中的內容響應給客戶端。
8、proxy_cache_methods GET | HEAD | POST …;
為那些請求方法去檢查緩存:默認為GET、HEAD、POST沒有什么意義
If the client request method is listed in this directive then the response will be cached. “GET” and “HEAD” methods are always added to the list, though it is recommended to specify them explicitly.
9、proxy_hide_header field;
指明要隱藏的head
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. The proxy_hide_header directive sets additional fields that will not be passed.
10、proxy_connect_timeout time;
設置代理連接時長,默認為60s,不能超過75s。
Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.
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];
向響應報文中添加首部信息;
添加自定義首部;
add_header X-Via $server_addr; 顯示代理服務器的IP地址;
add_header X-Accel $server_name; 顯示代理服務器的主機名;
指明此次訪問的網頁是由哪個ip地址的服務器代理的。
2、expires [modified] time;
expires epoch | max | off;
用于定義Expire或Cache-Control首部的值;
ngx_http_upstream_module模塊
定義realserver服務組
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 { … }
定義后端服務器組,會引入一個新的上下文;Context: http
upstream httpdsrvs {
server …
server…
…
}
2、server address [parameters];
在upstream上下文中server成員,以及相關的參數;Context: upstream
address的表示格式:
unix:/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT]
在nginx的默認配置文件中定義反向代理的后端服務器組的組名websrvs:
parameters(參數):
weight=number
權重,默認為1;
max_fails=number
失敗嘗試最大次數;超出此處指定的次數時,server將被標記為不可用;
可以對后臺服務器的健康狀態做檢測
fail_timeout=time
設置將服務器標記為不可用狀態的超時時長;
max_conns
當前的服務器的最大并發連接數;
backup
將服務器標記為“備用”,即所有服務器均不可用時此服務器才啟用;
down
標記為“不可用”;
3、least_conn;
最少連接調度算法,當server擁有不同的權重時其為wlc;
4、 ip_hash;
源地址hash調度方法;
5、consistent_hash;
一致性hash調度;
我們舉個例子來說明一致性哈希的好處。
假設后端集群包含三臺緩存服務器,A、B、C。
請求r1、r2落在A上。
請求r3、r4落在B上。
請求r5、r6落在C上。
使用一致性哈希時,當緩存服務器B宕機時,r1/r2會仍然落在A上,r5/r6會仍然落在C 上,
也就是說這兩臺服務器上的緩存都不會失效。r3/r4會被重新分配給A或者C,并產生回源。
使用其它算法,當緩存服務器B宕機時,r1/r2不再落在A上,r5/r6不再落在C上了。
也就是說A、B、C上的緩存都失效了,所有的請求都要回源。
5、hash key [consistent];
基于指定的key的hash表來實現對請求的調度,此處的key可以直接文本、變量或二者的組合;
作用:將請求分類,同一類請求將發往同一個real server;
If the consistent parameter is specified the ketama consistent hashing method will be used instead.
示例:
hash $request_uri consistent; 將來自同一uri的請求始終發往同一個real server
hash $remote_addr;將來自同一個客戶端的IP地址請求始終發往同一個real server
6、keepalive connections;
定義nginx代理服務器和后端服務器的保持連接。
為每個worker進程保留的空閑的長連接數量;
nginx的其它的二次發行版:
tengine
OpenResty
ngx_stream_core_module模塊 放置在main配置段中 core:核心 stream:流動
模擬反代基于tcp或udp的服務連接,即工作于傳輸層的反代或調度器;
和http同等級的配置段,stream要放置在main配置段中。
1、stream { … }
定義stream相關的服務;Context:main
stream {
upstream sshsrvs {
server 192.168.22.2:22;
server 192.168.22.3:22;
least_conn;
}
server {
listen 10.1.0.6:22022;
proxy_pass sshsrvs;
}
}
注意:自定義stream基于tcp或udp反向代理時,需要把nginx的主配置文件中的http配置段注釋掉。
2、listen
listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
memcached緩存服務器:
緩存服務器:
緩存:cache,無持久存儲功能;
bypass緩存
k/v cache,僅支持存儲流式化數據;
LiveJournal旗下的Danga Interactive研發,
特性:
k/v cache:僅可序列化數據;存儲項:k/v;
k:存放于內存的hash表中;(k是哈希的文件名)
v:存放于內存中的數據;(v是數據)
智能性一半依賴于客戶端(調用memcached的API開發程序),一半依賴于服務端;
分布式緩存:互不通信的分布式集群;
分布式系統請求路由方法:取模法,一致性哈希算法;
算法復雜度:O(1)
O(1)就是說n很大的時候,復雜度基本就不增長了,基本就是個常量c;
清理過期緩存項:
緩存耗盡:LRU
緩存項過期:惰性清理機制
安裝配置:
由CentOS 7 base倉庫直接提供:
監聽的端口:11211/tcp, 11211/udp ,以tcp使用的最多。
主程序:/usr/bin/memcached
配置文件:/etc/sysconfig/memcached memcached的環境配置文件。
Unit File:memcached.service
啟動memcached:
協議格式:memcached協議
文本格式
二進制格式:編碼效率更高;
命令:
統計類:stats, stats items, stats slabs, stats sizes
存儲類:set, add, replace, append, prepend
set:表示key有值就修改,沒有值就新創建。
add:表示新創建key值。
replace:
append:在已有key值之后附加新值。
prepend:在已有key值的前面追加新值。
命令格式:<command name> <key> <flags> <exptime> <bytes>
<cas unique>
檢索類:get, delete, incr/decr
get:獲取key值數據。
delete:刪除數據。
incr:把已有的值加1。
decr:把已有的值減1。
清空:flush_all:清空整個緩存。
示例:
telnet> add KEY <flags> <expiretime> <bytes> \r
telnet> VALUE
memcached程序的常用選項:
-m <num>:Use <num> MB memory max to use for object storage; the default is 64 megabytes.
-c <num>:Use <num> max simultaneous connections; the default is 1024.
-u <username>:以指定的用戶身份來運行進程;
-l <ip_addr>:監聽的IP地址,默認為本機所有地址;
-p <num>:監聽的TCP端口, the default is port 11211.
-U <num>:Listen on UDP port <num>, the default is port 11211, 0 is off.
-M:內存耗盡時,不執行LRU清理緩存,而是拒絕存入新的緩存項,直到有多余的空間可用時為止;
-f <factor>:增長因子;默認是1.25;
-t <threads>:啟動的用于響應用戶請求的線程數;
查看memcached的增長因子過程:
memcached默認沒有認證機制,可借用于SASL進行認證;
SASL:Simple Authentication Secure Layer
API:
php-pecl-memcache 可能需要依賴epel yum源進行安裝。
php-pecl-memcached 可能需要依賴epel yum源進行安裝。
python-memcached
libmemcached
libmemcached-devel
命令行工具:
memcached-tool SERVER:PORT COMMAND
實驗測試:
使用nginx反向代理,后端httpd realserver組:
實驗環境:
測試客戶機:
client客戶機:IP :192.168.3.7;
nginx反向代理服務器:
配置兩塊網卡,網卡1:ip:192.168.3.5 網卡2:192.168.22.1
后臺realserver配置:
realserver (1)httpd服務器:serverIP:192.168.22.2 gateway:192.168.22.1:
在realserver(1)中配置http服務器 upstream server 1
realserver (2)httpd服務器:serverIP:192.168.22.3 gateway:192.168.22.1:
在realserver(2)中配置http服務器 upstream server 2
1)在realserver(1)上搭建http服務:
2)在realserver(2)上搭建http服務和nginx服務器,nginx服務器監聽8080端口:
在realserver(2)上 配置nginx服務器監聽8080端口:
配置默認主頁為:
3)配置nginx反向代理服務器:
在nginx的主配置文件中的http配置段中配置upstream 后端服務器組:
在nginx的默認配置文件中定義反向代理的后端服務器組的組名websrvs:
nginx配合完成之后使用nginx -t 檢查一下語法是否正確;
如果語法沒有錯誤就可以使用 nginx -s reload 或 systemctl restart nginx.service 重載或重啟服務。
4)在client 192.168.3.7 客戶端 主機上測試請求后端realserver httpd服務器組:
5)關閉 realserver(1)和realserver(2)上的httpd服務進行測試backup主機服務器:
或是在/etc/nginx/nginx.conf的主配置文件中在upstream配置段中將realserver1、2標記為down。
原創文章,作者:zhengyibo,如若轉載,請注明出處:http://www.www58058.com/59610
Sehr geehrte Damen und Herren,bitte tragen Sie mich in den Hydrogeit-Newsletter ein. Leider f??hrt die Eigeninitiative unter Verwendung des vorgesehenen Formulars zu einem Dat.rbankfehleneBesten Dank!Mit freundlichen Gr????enHolger Krings