Nginx反向代理及負(fù)載均衡(1)
第一章:反向代理
正向代理與反向代理的區(qū)別
區(qū)別在于形式上服務(wù)的"對(duì)象"不一樣
正向代理代理的對(duì)象是客戶端,為客戶端服務(wù) PC電腦
反向代理代理的對(duì)象是服務(wù)端,為服務(wù)端服務(wù) 應(yīng)用服務(wù)
正向代理軟件:
squid
nginx
路由器
反向代理:
1.企業(yè)常用反向代理模式 與 Nginx反向代理模塊對(duì)應(yīng)關(guān)系總結(jié)如下表格
反向代理模式 Nginx反向代理模塊
http驹溃、websocket撩轰、https ngx_http_proxy_module
fastcgi ngx_http_fastcgi_module
uwsgi ngx_http_uwsgi_module
grpc ngx_http_v2_module
補(bǔ)充:
額外的:
系統(tǒng)默認(rèn)的端口 65535
系統(tǒng)服務(wù)需要占用部分的端口: 1~10000
正常配置一臺(tái)主機(jī)作為nginx服務(wù),最多能有5w多的tcp鏈接
正常配置一臺(tái)后端的應(yīng)用服務(wù),可能能使用的端口數(shù)時(shí)代理的一半 2w-3w左右
nginx --隨機(jī)端口--應(yīng)用服務(wù)-->隨機(jī)端口-->php動(dòng)態(tài)程序--->隨機(jī)端口-->mysql
反向代理配置
1.使用的主機(jī)
角色 外網(wǎng)IP(NAT) 內(nèi)網(wǎng)IP(LAN) 主機(jī)名
Proxy eth0:10.0.0.5 eth1:172.16.1.5 lb01
web01 eth1:172.16.1.7 web01
2.web01操作如下
[root@web01 conf.d]# cat web.oldboy.com.conf
server {
listen 80;
server_name web.oldboy.com;
location / {
root /web;
index index.html;
}
}
3.proxy的操作如下
[root@lb01 conf.d]# cat proxy_web.oldboy.com.conf
server {
listen 80;
server_name web.oldboy.com;
location / {
proxy_pass http://10.0.0.7:80;
proxy_http_version 1.1;
proxy_set_header Host $http_host; # 指向?qū)?yīng)域名
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 傳遞真實(shí)IP
}
}
4.windows的hosts解析
將所有的參數(shù)防止在一個(gè)proxy_params文件中,后期include即可
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30; #連接后端超時(shí)時(shí)間
proxy_send_timeout 60; #后端服務(wù)器數(shù)據(jù)回傳給nginx代理超時(shí)時(shí)間
proxy_read_timeout 60; #nginx代理等待后端服務(wù)器的響應(yīng)時(shí)間
proxy_buffering on; # 緩沖弃秆,邊收邊還
proxy_buffer_size 32k;
proxy_buffers 4 128k;
負(fù)載均衡
1.負(fù)載均衡介紹
lb
load balance
負(fù)載均衡
調(diào)度系統(tǒng)
SLB
ULB
CLB
--------------
硬件:
F5 radware
軟件:
七層: nginx haproxy
四層: nginx haproxy lvs
云產(chǎn)品:
SLB lvs做四層 tengine七層
ULB haproxy做四層 haproxy nginx
2.四層負(fù)載均衡與七層負(fù)載均衡區(qū)別
四層負(fù)載均衡數(shù)據(jù)包在底層就進(jìn)行了分發(fā)梁剔,而七層負(fù)載均衡數(shù)據(jù)包則是在最頂層進(jìn)行分發(fā)怯晕、由此可以看出悠汽,七層負(fù)載均衡效率沒有四負(fù)載均衡高箱吕。
但七層負(fù)載均衡更貼近于服務(wù),如:http協(xié)議就是七層協(xié)議柿冲,我們可以用Nginx可以作URL路徑規(guī)則匹配茬高、head頭改寫、會(huì)話保持等等假抄,這些是四層負(fù)載均衡無法實(shí)現(xiàn)的怎栽。
3.負(fù)載均衡配置文件 .conf
[root@lb01 conf.d]# cat proxy_all_oldboy.com.conf
upstream all {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name blog.oldboy.com;
location / {
proxy_pass http://all;
include proxy_params;
}
}
server {
listen 80;
server_name zh.oldboy.com;
location / {
proxy_pass http://all;
include proxy_params;
}
}
4.代理
場(chǎng)景: 反向代理
抓包分析
透?jìng)髡鎸?shí)客戶端IP地址
傳遞Host頭信息
調(diào)優(yōu)的參數(shù)
負(fù)載均衡:
容災(zāi) -->故障轉(zhuǎn)移 后端不是nginx故障,而是php故障了
性能 -->多臺(tái)機(jī)器邏輯上組成集群 upstrem定義
名詞:
四七層負(fù)載均衡
http
tcp
場(chǎng)景: 負(fù)載均衡
負(fù)載均衡常見典型故障
如果后臺(tái)服務(wù)連接超時(shí),Nginx是本身是有機(jī)制的宿饱,如果出現(xiàn)一個(gè)節(jié)點(diǎn)down掉的時(shí)候熏瞄,Nginx會(huì)更據(jù)你具體負(fù)載均衡的設(shè)置,將請(qǐng)求轉(zhuǎn)移到其他的節(jié)點(diǎn)上谬以,但是强饮,如果后臺(tái)服務(wù)連接沒有down掉,但是返回錯(cuò)誤異常碼了如:504为黎、502邮丰、500,這個(gè)時(shí)候你需要加一個(gè)負(fù)載均衡的設(shè)置行您,如下:proxy_next_upstream http_500 | http_502 | http_503 | http_504 |http_404;意思是,當(dāng)其中一臺(tái)返回錯(cuò)誤碼404,500...等錯(cuò)誤時(shí)柠座,可以分配到下一臺(tái)服務(wù)器程序繼續(xù)處理邑雅,提高平臺(tái)訪問成功率。
server {
listen 80;
server_name www.driverzeng.com;
location / {
proxy_pass http://node;
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
}
}
Nginx負(fù)載均衡調(diào)度算法
調(diào)度算法 概述
輪詢 按順序逐一分配到不同的后端服務(wù)器(默認(rèn))
weight 加權(quán)輪詢, weight值越大,分配到的訪問幾率越高
ip_hash 每個(gè)請(qǐng)求按訪問IP的hash結(jié)果分配,這樣來自同一IP的固定訪問一個(gè)后端服務(wù)器
url_hash 按照訪問URL的hash結(jié)果來分配請(qǐng)求,是每個(gè)URL定向到同一個(gè)后端服務(wù)器
least_conn 最少鏈接數(shù), 那個(gè)機(jī)器鏈接數(shù)少就分發(fā)
1.加權(quán)輪詢
[root@lb01 conf.d]# cat proxy_web.oldboy.com.conf
upstream web {
server 172.16.1.7:80 weight=8; 服務(wù)器
server 172.16.1.8:80 weight=2; pc
}
2.ip_hash, 按照ip定向到某一臺(tái)后端web服務(wù)器, 不能和weight
[root@lb01 conf.d]# cat proxy_web.oldboy.com.conf
upstream web {
ip_hash;
server 172.16.1.7:80;
server 172.16.1.8:80;
}
----------------------
解決會(huì)話登錄問題
新的問題: 會(huì)造成某臺(tái)主機(jī)壓力過大,而其他主機(jī)處于比較空閑狀態(tài)
最常用的就是輪詢
Nginx負(fù)載均衡后端狀態(tài)
1.后端Web服務(wù)器在前端Nginx負(fù)載均衡調(diào)度中的狀態(tài)
狀態(tài) 概述
down 當(dāng)前的server暫時(shí)不參與負(fù)載均衡
backup 預(yù)留的備份服務(wù)器
max_fails 允許請(qǐng)求失敗的次數(shù)
fail_timeout 經(jīng)過max_fails失敗后, 服務(wù)暫停時(shí)間
max_conns 限制最大的接收連接數(shù)
2.測(cè)試down狀態(tài)妈经,該Server不猜與負(fù)載均衡的調(diào)度
upstream load_pass {
#不參與任何調(diào)度, 一般用于停機(jī)維護(hù)
server 10.0.0.7:80 down;
}
3.測(cè)試backup以及down狀態(tài)
upstream load_pass {
server 10.0.0.7:80 down;
server 10.0.0.8:80 backup; #備份服務(wù)器
server 10.0.0.9:80 max_fails=1 fail_timeout=10s;
}
location / {
proxy_pass http://load_pass;
include proxy_params;
}
4.測(cè)試max_conns最大TCP連接數(shù)
upstream load_pass {
server 10.0.0.7:80;
server 10.0.0.8:80 max_conns=1;
}
Nginx負(fù)載均衡健康檢查
在Nginx官方模塊提供的模塊中淮野,沒有對(duì)負(fù)載均衡后端節(jié)點(diǎn)的健康檢查模塊,但可以使用第三方模塊吹泡。
nginx_upstream_check_module
來檢測(cè)后端服務(wù)的健康狀態(tài)骤星。
第三方模塊項(xiàng)目地址:TP
1.安裝依賴包
[root@lb02 ~]# yum install -y gcc glibc gcc-c++ pcre-devel openssl-devel patch
2.下載nginx源碼包以及nginx_upstream_check模塊第三方模塊
[root@lb02 ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@lb02 ~]# wget https://github.com/yaoweibin/nginx_upstream_check_module/archive/master.zip
3..解壓nginx源碼包以及第三方模塊
[root@lb02 ~]# tar xf nginx-1.14.2.tar.gz
[root@lb02 ~]# unzip master.zip
4.進(jìn)入nginx目錄,打補(bǔ)丁(nginx的版本是1.14補(bǔ)丁就選擇1.14的,p1代表在nginx目錄爆哑,p0是不在nginx目錄
[root@lb02 ~]# cd nginx-1.14.2/
[root@lb02 nginx-1.14.2]# patch -p1 <../nginx_upstream_check_module-master/check_1.14.0+.patch
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-module=/root/nginx_upstream_check_module-master --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
[root@lb02 nginx-1.14.2]# make && make install
5.在已有的負(fù)載均衡上增加健康檢查的功能
[root@lb01 conf.d]# cat proxy_web.conf
upstream web {
server 172.16.1.7:80 max_fails=2 fail_timeout=10s;
server 172.16.1.8:80 max_fails=2 fail_timeout=10s;
check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
#interval 檢測(cè)間隔時(shí)間洞难,單位為毫秒
#rise 表示請(qǐng)求2次正常,標(biāo)記此后端的狀態(tài)為up
#fall 表示請(qǐng)求3次失敗揭朝,標(biāo)記此后端的狀態(tài)為down
#type 類型為tcp
#timeout 超時(shí)時(shí)間队贱,單位為毫秒
}
server {
listen 80;
server_name web.drz.com;
location / {
proxy_pass http://web;
include proxy_params;
}
location /upstream_check {
check_status;
}
}