柚子快報(bào)邀請(qǐng)碼778899分享:運(yùn)維 Nginx 反向代理
柚子快報(bào)邀請(qǐng)碼778899分享:運(yùn)維 Nginx 反向代理
Nginx 反向代理
代理基礎(chǔ)知識(shí)
代理分為兩種,分別是正向代理和反向代理
正向代理(Forward Proxy) 和 反向代理(Reverse Proxy) 是兩種常見的代理服務(wù)器,它們用于處理 網(wǎng)絡(luò)通信中的不同方向和用途
正向代理(Forward Proxy)
特點(diǎn)
代理服務(wù)器位于客戶端和目標(biāo)服務(wù)器之間 客戶端向代理服務(wù)器發(fā)送請(qǐng)求,代理服務(wù)器將請(qǐng)求發(fā)送到目標(biāo)服務(wù)器,并將目標(biāo)服務(wù)器的響應(yīng)返回給客戶端 目標(biāo)服務(wù)器不知道客戶端的存在,它只知道有一個(gè)代理服務(wù)器向其發(fā)送請(qǐng)求 客戶端通過正向代理訪問互聯(lián)網(wǎng)資源時(shí),通常需要配置客戶端來使用代理
用途
突破訪問限制:用于繞過網(wǎng)絡(luò)訪問限制,訪問受限制的資源 隱藏客戶端身份:客戶端可以通過正向代理隱藏其真實(shí) IP 地址
反向代理(Reverse Proxy)
特點(diǎn)
代理服務(wù)器位于目標(biāo)服務(wù)器和客戶端之間 客戶端向代理服務(wù)器發(fā)送請(qǐng)求,代理服務(wù)器將請(qǐng)求轉(zhuǎn)發(fā)給一個(gè)或多個(gè)目標(biāo)服務(wù)器,并將其中一個(gè)目標(biāo)服務(wù)器的響應(yīng)返回給客戶端 目標(biāo)服務(wù)器不知道最終客戶端的身份,只知道有一個(gè)代理服務(wù)器向其發(fā)送請(qǐng)求 用于將客戶端的請(qǐng)求分發(fā)給多個(gè)服務(wù)器,實(shí)現(xiàn)負(fù)載均衡
用途
負(fù)載均衡:通過將流量分發(fā)到多個(gè)服務(wù)器,確保服務(wù)器的負(fù)載均勻分布 緩存和加速:反向代理可以緩存靜態(tài)內(nèi)容,減輕目標(biāo)服務(wù)器的負(fù)載,并提高訪問速度 安全性:隱藏真實(shí)服務(wù)器的信息,提高安全性,同時(shí)可以進(jìn)行 SSL 終止(SSL Termination)
相同和不同
相同點(diǎn)
中間層:正向代理和反向代理都是位于客戶端和目標(biāo)服務(wù)器之間的中間層。 代理功能:它們都充當(dāng)了代理的角色,處理請(qǐng)求和響應(yīng),使得通信更加靈活和安全
不同點(diǎn)
方向:正向代理代理客戶端,反向代理代理服務(wù)器 目的:正向代理主要用于訪問控制和隱藏客戶端身份,反向代理主要用于負(fù)載均衡、緩存和提高安全性 配置:客戶端需要配置使用正向代理,而反向代理是對(duì)服務(wù)器透明的,客戶端無需感知
Nginx 和 LVS
Nginx 和 LVS(Linux Virtual Server) 都是流行的代理和負(fù)載均衡解決方案,但它們有一些不同的特點(diǎn) 和應(yīng)用場(chǎng)景
選擇使用 Nginx 還是 LVS 取決于具體的應(yīng)用需求和復(fù)雜度。Nginx 更適合作為 Web 服務(wù)器和應(yīng)用層負(fù) 載均衡器,而 LVS 更適用于傳輸層負(fù)載均衡
相同點(diǎn)
負(fù)載均衡:Nginx 和 LVS 都可以作為負(fù)載均衡器,將流量分發(fā)到多個(gè)后端服務(wù)器,提高系統(tǒng)的可用 性和性能。 性能:Nginx 和 LVS 都具有高性能的特點(diǎn),能夠處理大量并發(fā)連接和請(qǐng)求
不同點(diǎn)
層次:Nginx 在應(yīng)用層進(jìn)行負(fù)載均衡和反向代理,而 LVS 在傳輸層進(jìn)行負(fù)載均衡 功能:Nginx 除了負(fù)載均衡外,還可以作為反向代理和靜態(tài)文件服務(wù)器;而 LVS 主要專注于負(fù)載均 衡,實(shí)現(xiàn)簡(jiǎn)單而高效的四層分發(fā) 配置和管理:Nginx 配置相對(duì)簡(jiǎn)單,易于管理,適用于各種規(guī)模的應(yīng)用;LVS 需要深入了解 Linux 內(nèi)核和相關(guān)配置,適用于大規(guī)模和對(duì)性能有更高要求的場(chǎng)景
LVS 不監(jiān)聽端口,不處理請(qǐng)求數(shù)據(jù),不參與握手流程,只會(huì)在內(nèi)核層轉(zhuǎn)發(fā)數(shù)據(jù)報(bào)文
Nginx 需要在應(yīng)用層接收請(qǐng)求,根據(jù)客戶端的請(qǐng)求參數(shù)和Nginx中配置的規(guī)則,再重新作為客戶端向后 端服務(wù)器發(fā)起請(qǐng)求
LVS 通常做四層代理,Nginx 做七層代理
實(shí)現(xiàn) http 協(xié)議反向代理
相關(guān)指令和參數(shù)
Nginx 可以基于ngx_http_proxy_module 模塊提供 http 協(xié)議的反向代理服務(wù),該模塊是 Nginx 的默認(rèn)模塊
proxy_pass URL; # 轉(zhuǎn)發(fā)的后端服務(wù)器地址,可以寫主機(jī)名,域名,IP地址,也可以額外指定端口,
# 作用域 location, if in location, limit_except
proxy_hide_header field; # Nginx 默認(rèn)不會(huì)將后端服務(wù)器的 Date,Server,X-Pad,X-Accel-... 這些響應(yīng)頭信息傳給
# 客戶端,除了這些之外的響應(yīng)頭字段會(huì)回傳,
# 可以使用 proxy_hide_header 顯式指定不回傳的響應(yīng)頭字段
# 作用域 http, server, location
proxy_pass_header field; # 顯式指定要回傳給客戶端的后端服務(wù)器響應(yīng)頭中的字段,
#作用域 http, server, location
proxy_pass_request_body on|off; # 是否向后端服務(wù)器發(fā)送客戶端 http 請(qǐng)求的 body 部份,默認(rèn) on,
# 作用域 http, server, location
proxy_pass_request_headers on|off; # 是否向后端服務(wù)器發(fā)送客戶端 http 請(qǐng)求的頭部信息,默認(rèn) on
# 作用域 http, server, location
proxy_connect_timeout time; # Nginx與后端服務(wù)器建立連接超時(shí)時(shí)長(zhǎng),默認(rèn)60S,超時(shí)會(huì)向客戶端返回504
# 作用域 http, server, location
proxy_read_timeout time; # Nginx 等待后端服務(wù)器返回?cái)?shù)據(jù)的超時(shí)時(shí)長(zhǎng),默認(rèn)60S,超時(shí)會(huì)向客戶端返回504
# 作用域 http, server, location
proxy_send_timeout time; # Nginx 向后端服務(wù)器發(fā)送請(qǐng)求的超時(shí)時(shí)長(zhǎng),默認(rèn)60S,超時(shí)會(huì)向客戶端返回408
# 作用域 http, server, location
proxy_set_body value; # 重新定義傳給后端服務(wù)器的請(qǐng)求的正文,可以包含文本,變量等,
# 作用域 http, server, location
proxy_set_header field value; # 更改或添加請(qǐng)求頭字段并發(fā)送到后端服務(wù)器,
# 作用域http, server, location
proxy_http_version 1.0|1.1; # 設(shè)置向后端服務(wù)器發(fā)送請(qǐng)求時(shí)的 http 協(xié)議版本,默認(rèn)值1.0,
# 作用域http, server, location
proxy_ignore_client_abort on|off; # 客戶端中斷連接,Nginx 是否繼續(xù)執(zhí)行與后端的連接,默認(rèn)值 off
#客戶端中斷,Nginx 也會(huì)中斷后端連接, on 表示 客戶端中斷,nginx 還會(huì)繼續(xù)處理與后端在連接
# 作用域 http, server, location
proxy_headers_hash_bucket_size size; # 當(dāng)配置了 proxy_hide_header和proxy_set_header的時(shí)候,
# 用于設(shè)置nginx保存HTTP報(bào)文頭的hash表的大小,默認(rèn)值 64
# 作用域 http, server, location
proxy_headers_hash_max_size size; # 上一個(gè)參數(shù)的上限,默認(rèn)值 512
# 作用域 http, server, location
proxy_next_upstream error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_403|http_404|http_429|non_idempotent| off ...;
# 當(dāng)前配置的后端服務(wù)器無法提供服務(wù)時(shí),因?yàn)楹畏N錯(cuò)誤而去請(qǐng)求下一個(gè)后端服務(wù)器
# 默認(rèn)值 error timeout, 表示當(dāng)前后端服務(wù)器因?yàn)閑rror 和 timeout 錯(cuò)誤時(shí),去請(qǐng)求 另一個(gè)后端服務(wù)器
# 作用域 http, server, location
proxy_cache zone|off; # 是否啟用代理緩存,默認(rèn) off,不啟用,zone 指緩存名稱,
# 作用域 http, server, location
proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size
[inactive=time] [max_size=size] [min_free=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];
# 開啟代理緩存后指定緩存數(shù)據(jù)的存放路徑,作用域http,默認(rèn)沒有設(shè)置
# path 表示緩存數(shù)據(jù)存放路徑,要保證nginx 有寫權(quán)限
# levels 表示緩存數(shù)據(jù)目錄層級(jí),16進(jìn)制表示,levels=1:2 表示第一級(jí)有16個(gè)目錄,0-f,第一級(jí)中每個(gè)目錄下有16*16個(gè)子目錄,00-ff
# keys_zone=name:size zone 表示緩存名稱,先定義后使用,size 表示該zone 空間大小
# inactive 表示緩存數(shù)據(jù)生命周期,默認(rèn)值10分鐘
# max_size 表示緩存占用的磁盤空間最大能有多大
proxy_cache_key string; # 指定緩存數(shù)據(jù)的key,不同的key 對(duì)應(yīng)不同的緩存文件,
#作用域 http, server, location
# 默認(rèn)值 $scheme$proxy_host$request_uri
proxy_cache_valid [code ...] time; # 為不同響應(yīng)狀態(tài)碼的數(shù)據(jù)設(shè)置不同的緩存時(shí)長(zhǎng),可設(shè)置多條,默認(rèn)不設(shè)置,
# 作用域 http, server, location
proxy_cache_use_stale error|timeout|invalid_header|updating|http_500|http_502|http_503|http_504|http_403|http_404|http_429|off ...;
# 在后端服務(wù)器報(bào)哪些錯(cuò)誤的情況下,直接使用過期緩存數(shù)據(jù)響應(yīng)客戶端請(qǐng)求默認(rèn)off,
#作用域 http, server, location
proxy_cache_methods GET|HEAD|POST ...; # 緩存哪些請(qǐng)求類型的數(shù)據(jù),默認(rèn)值 GET HEAD,
#作用域 http, server, location
基本配置
#轉(zhuǎn)發(fā)到指定IP
server{
listen 80;
server_name www.a30.com;
#root /var/www/html/www.a30.com;
location /{
proxy_pass http://10.0.0.161; #161要開啟WEB服務(wù),請(qǐng)求的是默認(rèn)default_Serve 配置
}
}
[root@ubuntu ~]# curl http://10.0.0.161
hello world
#被轉(zhuǎn)發(fā)到后端161
[root@ubuntu ~]# curl http://www.a30.com
hello world
#轉(zhuǎn)發(fā)到指定IP指定端口
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://10.0.0.161:8080;
}
}
#后端主機(jī)配置
server {
listen 8080;
root /var/www/html/8080;
}
[root@ubuntu ~]# curl www.a30.com
hello 8080
#轉(zhuǎn)發(fā)到指定域名
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://www.node-1.com;
}
}
#后端主機(jī)配置
server {
listen 80;
root /var/www/html/www.node-1.com;
server_name www.node-1.com;
}
[root@ubuntu ~]# echo "node-1" > /var/www/html/www.node-1.com/index.html
#測(cè)試
[root@ubuntu ~]# curl www.a30.com
node-1
#透?jìng)髦付▍?shù)
#在上述請(qǐng)求中,客戶端訪問 http://www.a30.com,該主機(jī)收到請(qǐng)求后作為客戶端去請(qǐng)求http://www.node-1.com
#客戶端主機(jī)配置
[root@ubuntu ~]# cat /etc/hosts
10.0.0.206 www.a30.com
#中間主機(jī)配置
[root@ubuntu ~]# cat /etc/hosts
10.0.0.161 www.a30.com
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://10.0.0.161;
proxy_set_header Host $http_host; #將客戶端從請(qǐng)求頭中傳來的 Host 值傳給后端服務(wù)器
}
}
#后端主機(jī)配置
server {
listen 80;
root /var/www/html/www.a30.com;
server_name www.a30.com;
}
[root@ubuntu ~]# echo "a30" > /var/www/html/www.a30.com/index.html
#客戶端測(cè)試
[root@ubuntu ~]# curl http://www.a30.com
a30
#如果后端服務(wù)不可用,從客戶端訪問會(huì)返回502
#停止后端nginx
[root@ubuntu ~]# systemctl stop nginx.service
#客戶端訪問中間Nginx
[root@ubuntu ~]# curl http://www.a30.com
502 Bad Gateway
[root@ubuntu ~]# curl http://www.a30.com -I
HTTP/1.1 502 Bad Gateway
Server: nginx
Date: Wed, 18 Sep 2024 11:32:16 GMT
Content-Type: text/html; charset=utf8
Content-Length: 150
Connection: keep-alive
實(shí)現(xiàn)動(dòng)靜分離
根據(jù)條件進(jìn)行調(diào)度,實(shí)現(xiàn)動(dòng)靜分離
角色
IP
Client
10.0.0.158
Proxy Server
10.0.0.157
API Server
10.0.0.161
Static Server
10.0.0.151
Client配置
[root@Rocky-9 ~]# cat /etc/hosts
10.0.0.157 www.a30.com
Proxy Server 配置
server{
listen 80;
server_name www.a30.com;
#root /var/www/html/www.a30.com;
location /static{
proxy_pass http://10.0.0.151;
proxy_set_header Host "static.a30.com";
}
location /api{
proxy_pass http://10.0.0.161;
proxy_set_header Host "api.a30.com";
}
API Server配置
server{
listen 80;
server_name api.a30.com;
root /apps/nginx/html/api.a30.com/;
}
[root@ubuntu24 sites-enabled]# cat /apps/nginx/html/api.a30.com/api/index.html
api.a30.com
Static Server配置
server{
listen 80;
server_name static.a30.com;
root /var/www/html/static.a30.com/;
}
[root@ubuntu22:sites-enabled]# cat /var/www/html/static.a30.com/static/index.html
static.a30.com
Client測(cè)試
[root@Rocky-9 ~]# curl http://www.a30.com/api/index.html
api.a30.com
[root@Rocky-9 ~]# curl http://www.a30.com/static/index.html
static.a30.com
proxy_pass 后面加斜線和不加斜線的區(qū)別
#沒有斜線是追加
# http://www.a30.com/api/index.html ----> http://api.a30.com/api/index.html
location /api{
proxy_pass http://10.0.0.161;
proxy_set_header Host "api.a30.com";
}
#有斜線是替換
# http://www.a30.com/api/index.html -----> http://api.a30.com/index.html
location /api{
proxy_pass http://10.0.0.161;
proxy_set_header Host "api.a30.com";
}
實(shí)現(xiàn)對(duì)特定資源的代理
location ~ \.(jpe?g|png|bmp|gif)$ {
proxy_pass http://10.0.0.161;
proxy_set_header Host "api.a30.com";
}
代理服務(wù)器實(shí)現(xiàn)數(shù)據(jù)緩存
前置條件:各服務(wù)器時(shí)間和時(shí)區(qū)先統(tǒng)一,方便測(cè)試
#Proxy Server 配置
#定義緩存
proxy_cache_path /tmp/proxycache levels=1:2 keys_zone=proxycache:20m inactive=60s max_size=1g;
server{
listen 80;
server_name www.a30.com;
location /static{
proxy_pass http://10.0.0.157/;
proxy_set_header Host "static.a30.com";
proxy_cache proxycache; #使用緩存
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 90s;
proxy_cache_valid any 2m; #此處一定要寫,否則緩存不生效
}
}
#重載,生成緩存目錄
[root@ubuntu ~]# nginx -s reload
[root@ubuntu ~]# ll /tmp/proxycache/
total 8
drwx------ 2 www-data root 4096 Feb 12 23:09 ./
drwxrwxrwt 14 root root 4096 Feb 12 23:09 ../
#Static Server 配置
server {
listen 80;
root /var/www/html/static.a30.com;
server_name static.a30.com;
}
[root@ubuntu24 sites-enabled]# ls -lh /var/www/html/static.a30.com/
total 8.0K
-rw-r--r-- 1 root root 657 Sep 19 09:55 fstab
-rw-r--r-- 1 root root 18 Sep 19 09:55 index.html
#客戶端測(cè)試
[root@Rocky-9 ~]# curl http://www.a30.com/static/fstab
#查看 Proxy Server 上的緩存數(shù)據(jù),文件名就是key 的 hash 值
[root@ubuntu22:sites-enabled]# tree /tmp/proxycache/
/tmp/proxycache/
└── 3
└── ab
└── 2d291e4d45687e428f0215bec190aab3
2 directories, 1 file
#并不是一個(gè)文本文件
[root@ubuntu22:sites-enabled]# file /tmp/proxycache/3/ab/2d291e4d45687e428f0215bec190aab3
/tmp/proxycache/3/ab/2d291e4d45687e428f0215bec190aab3: data
#查看當(dāng)前時(shí)間和緩存文件時(shí)間
[root@ubuntu22:sites-enabled]# date
Thu Sep 19 10:03:38 AM CST 2024
[root@ubuntu22:sites-enabled]# stat /tmp/proxycache/3/ab/2d291e4d45687e428f0215bec190aab3
File: /tmp/proxycache/3/ab/2d291e4d45687e428f0215bec190aab3
Size: 1254 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 4194322 Links: 1
Access: (0600/-rw-------) Uid: ( 33/www-data) Gid: ( 33/www-data)
Access: 2024-09-19 10:03:16.497567601 +0800
Modify: 2024-09-19 10:02:55.416164600 +0800
Change: 2024-09-19 10:02:55.416164600 +0800
Birth: 2024-09-19 10:02:55.416164600 +0800
#除了文件內(nèi)容外,還有頭部信息
[root@ubuntu22:sites-enabled]# cat /tmp/proxycache/3/ab/2d291e4d45687e428f0215bec190aab3
????eudU"66eb8498-291"
KEY: /static/fstab
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 19 Sep 2024 02:05:25 GMT
Content-Type: application/octet-stream
Content-Length: 657
Last-Modified: Thu, 19 Sep 2024 01:55:36 GMT
Connection: close
ETag: "66eb8498-291"
Accept-Ranges: bytes
# /etc/fstab: static file system information.
#
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
#
#
# / was on /dev/ubuntu-vg/ubuntu-lv during curtin installation
/dev/disk/by-id/dm-uuid-LVM-H5gHRIOORHBs4Od7xVmGBfQE2ZJSd1kxqhie9niuE1re5q1XVMJmVvoQ7stlUKe2 / ext4 defaults 0 1
# /boot was on /dev/sda2 during curtin installation
/dev/disk/by-uuid/8a0cc0fa-cc21-4fe4-ab25-4a43540d9f02 /boot ext4 defaults 0 1
/swap.img none swap sw 0 0
#生命周期結(jié)束后文件被刪除
#但是在緩存有效期內(nèi),后端服務(wù)器內(nèi)容發(fā)生了更新,客戶端獲取的還是緩存數(shù)據(jù)
#后端真實(shí)數(shù)據(jù)刪除,客戶端還能拿到緩存數(shù)據(jù)
實(shí)現(xiàn)客戶端IP地址透?jìng)?/p>
在使用Nginx 做代理的情況下,默認(rèn)后端服務(wù)器無法獲取客戶端真實(shí)IP地址
角色
IP
Client
10.0.0.158
Proxy Server
10.0.0.157
Real Server
10.0.0.151
默認(rèn)情況下,后端服務(wù)器無法獲取真實(shí)客戶端IP
Proxy Server 配置
server{
listen 80;
server_name www.a30.com;
location / {
proxy_pass http://10.0.0.151;
}
Real Server 配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
return 200 ${remote_addr}---${http_x_real_ip}---${http_x_forwarded_for};
}
}
Client測(cè)試
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.157------
#后端服務(wù)器只能獲取代理服務(wù)器IP
修改代理服務(wù)器配置,透?jìng)髡鎸?shí)客戶端IP
server{
listen 80;
server_name www.a30.com;
location / {
proxy_pass http://10.0.0.151;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
#表示將客戶端IP追加請(qǐng)求報(bào)文中X-Forwarded-For首部字段,多個(gè)IP之間用逗號(hào)分隔,如果請(qǐng)求中沒有X-Forwarded-For,就使用$remote_addr
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#客戶端測(cè)試 $remote_addr 獲取代理IP,$http_x_real_ip 獲取真實(shí)客戶端IP,
$http_x_forwarded_for 獲取真實(shí)客戶端IP
#客戶端測(cè)試
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.157---10.0.0.158---10.0.0.158
實(shí)現(xiàn)多級(jí)代理客戶端IP透?jìng)?/p>
角色
IP
Client
10.0.0.158
Proxy Server - First
10.0.0.161
Proxy Server - Second
10.0.0.157
Real Server
10.0.0.151
158 -----> 161 -----> 157 -----> 151
#Proxy Server - First 配置
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://10.0.0.157;
}
}
#Proxy Server - Second 配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://10.0.0.151;
}
}
#Real Server 配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
return 200 ${remote_addr}---${http_x_real_ip}---${http_x_forwarded_for};
}
}
#客戶端測(cè)試,Real Server 只能通過 $remote_addr 獲取上一級(jí)代理的IP
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.157------
修改第一級(jí)代理服務(wù)器配置
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://10.0.0.157;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#客戶端測(cè)試 $remote_addr 獲取上一級(jí)代理IP,$http_x_real_ip 獲取真實(shí)客戶端IP,$http_x_forwarded_for 獲取真實(shí)客戶端IP
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.157---10.0.0.158---10.0.0.158
繼續(xù)修改第二級(jí)代理服務(wù)器配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_pass http://10.0.0.151;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
#客戶端測(cè)試
# $remote_addr 獲取上一級(jí)代理IP
# $http_x_real_ip 獲取上上一級(jí)代理IP
# $http_x_forwarded_for 累加了第一級(jí)代理的 X-Forwarded-For 和第二級(jí)代理的 X-Forwarded-For
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.157---10.0.0.161---10.0.0.158, 10.0.0.161
第一級(jí)代理不透?jìng)?,不添加?qǐng)求頭字
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://10.0.0.157;
#proxy_set_header X-Real-IP $remote_addr;
#proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.157---10.0.0.161---10.0.0.161
實(shí)現(xiàn) http 協(xié)議反向代理的負(fù)載均衡
相關(guān)指令和參數(shù)
在實(shí)現(xiàn) Nginx 反向代理的基礎(chǔ)上,可以基于 ngx_http_upstream_module 模塊實(shí)現(xiàn)后端服務(wù)器的分 組,權(quán)重分配,狀態(tài)監(jiān)測(cè),調(diào)度算法等高級(jí)功能
upstream name { server address [parameters]; } # 定義一個(gè)后端服務(wù)器組,可以包含一臺(tái)或多臺(tái)服務(wù)器,
# 定義好后在 proxy_pass 指令中引用,作用域 http
server address [parameters]; # 在 upstream 中定義一個(gè)具體的后端服務(wù)器,作用域upstream
# address 指定后端服務(wù)器 可以是IP地址,主機(jī)名,或UNIX Socket,可以加端口號(hào)
# parameters 是可選摻數(shù),具體有以下幾個(gè)屬性
# weight=number 指定該 server 的權(quán)重,默認(rèn)值都是1
# max_conns=number 該 Server 的最大活動(dòng)連接數(shù),達(dá)到后將不再給該 Server 發(fā)送請(qǐng)求,默認(rèn)值0,表示不限制
# max_fails=number 后端服務(wù)器的下線條件,當(dāng)客戶端訪問時(shí),對(duì)本次調(diào)度選中的后端服務(wù)器連續(xù)進(jìn)行檢測(cè)多少次,如果都失敗就標(biāo)記為不可用,默認(rèn)為1次,當(dāng)客戶端訪問時(shí),才會(huì)利用TCP觸發(fā)對(duì)探測(cè)后端服務(wù)器健康性檢查,而非周期性的探測(cè)
# fail_timeout=time 后端服務(wù)器的上線條件,對(duì)已經(jīng)檢測(cè)到處于不可用的后端服務(wù)器,每隔此時(shí)間間隔再次進(jìn)行檢測(cè)是否恢復(fù)可用,如果發(fā)現(xiàn)可用,則將后端服務(wù)器參與調(diào)度,默認(rèn)為10秒
# backup 標(biāo)記該 Server 為備用,當(dāng)所有后端服務(wù)器不可用時(shí),才使用此服務(wù)器
# down 標(biāo)記該 Server 臨時(shí)不可用,可用于平滑下線后端服務(wù)器,新請(qǐng)求不再調(diào)度到此服務(wù)器,原有連接不受影響
hash key [consistent]; # 使用自行指定的 Key 做 hash 運(yùn)算后進(jìn)行調(diào)度,Key 可以是變量,比如請(qǐng)求頭中的字段,URI等,如果對(duì)應(yīng)的 server 條目配置發(fā)生了變化,會(huì)導(dǎo)致相同的 key 被重新hash
# consistent 表示使用一致性 hash,此參數(shù)確保該upstream 中的 server 條目發(fā)生變化時(shí),盡可能少的重新 hash,適用于做緩存服務(wù)的場(chǎng)景,提高緩存命中率
# 作用域 upstream
ip_hash; # 源地址hash調(diào)度方法,基于的客戶端的remote_addr(源地址IPv4的前24位或整個(gè)IPv6地址)做hash計(jì)算,以實(shí)現(xiàn)會(huì)話保持,作用域 upstream
least_conn; # 最少連接調(diào)度算法,優(yōu)先將客戶端請(qǐng)求調(diào)度到當(dāng)前連接最少的后端服務(wù)器,相當(dāng)于LVS中的 LC 算法
# 配合權(quán)重,能實(shí)現(xiàn)類似于 LVS 中的 WLC 算法
# 作用域 upstream
keepalive connections; # 為每個(gè) worker 進(jìn)程保留最多多少個(gè)空閑?;钸B接數(shù),超過此值,最近最少使用的連接將被關(guān)閉
# 默認(rèn)不設(shè)置,作用域 upstream
keepalive_time time; # 空閑連接保持的時(shí)長(zhǎng),超過該時(shí)間,一直沒使用的空閑連接將被銷毀
# 默認(rèn)值 1h,作用域 upstream
基本配置
角色
IP
Client
10.0.0.158
Proxy Server
10.0.0.157
Real Server - 1
10.0.0.161
Real Server - 2
10.0.0.151
# Proxy Server 配置
upstream group1{
server 10.0.0.161;
server 10.0.0.151;
}
server{
listen 80;
server_name www.a30.com;
#root /var/www/html/www.a30.com;
keepalive_timeout 15 30;
autoindex on;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
# Real Server-1 配置
server {
listen 80;
root /var/www/html/www.a30.com;
server_name www.a30.com;
}
[root@ubuntu24 www.a30.com]# cat /apps/nginx/html/www.a30.com/index.html
10.0.0.161
# Real Server-2 配置
server {
listen 80;
root /var/www/html/www.a30.com;
server_name www.a30.com;
}
[root@ubuntu22:sites-enabled]# cat /var/www/html/www.a30.com/index.html
www.a30.com
10.0.0.151
#客戶端測(cè)試-輪循調(diào)度到后端服務(wù)器
[root@Rocky-9 ~]# curl www.a30.com
www.a30.com
10.0.0.151
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.161
[root@Rocky-9 ~]# curl www.a30.com
www.a30.com
10.0.0.151
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.161
設(shè)置權(quán)重
#每個(gè) server 配置的默認(rèn)權(quán)重是1,這種寫法,兩個(gè) server 被調(diào)度的比例為 3:1
upstream group1{
server 10.0.0.161 weight=3;
server 10.0.0.151;
}
[root@Rocky-9 ~]# curl www.a30.com
www.a30.com
10.0.0.151
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.161
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.161
[root@Rocky-9 ~]# curl www.a30.com
10.0.0.161
[root@Rocky-9 ~]# curl www.a30.com
www.a30.com
10.0.0.151
限制最大活動(dòng)連接數(shù)
#10.0.0.161 同時(shí)只能維持兩個(gè)活動(dòng)連接
upstream group1{
server 10.0.0.161 max_conns=2;
server 10.0.0.151;
}
#后端服務(wù)器配置
server {
listen 80;
root /var/www/html/www.a30.com;
server_name www.a30.com;
limit_rate 10k;
}
#客戶端測(cè)試,開6個(gè)窗口下載文件
[root@ubuntu ~]# wget http://www.a30.com/test.img
#查看 10.0.0.161 上的連接,2個(gè)活動(dòng)連接
[root@ubuntu ~]# ss -tnpe | grep 80
#查看 10.0.0.151 上的連接,4個(gè)活動(dòng)連接
#客戶端繼續(xù)測(cè)試,新的請(qǐng)求都不會(huì)調(diào)度給 10.0.0.161
后端服務(wù)器健康性檢查
Nginx 的 upstream 指令對(duì)于后端服務(wù)器的健康性檢查是被動(dòng)檢查,當(dāng)有客戶端請(qǐng)求被調(diào)度到該服務(wù)器 上時(shí),會(huì)在TCP協(xié)議層的三次握手時(shí)檢查該服務(wù)器是否可用,如果不可用就調(diào)度到別的服務(wù)器,當(dāng)不可 用的次數(shù)達(dá)到指定次數(shù)時(shí)(默認(rèn)是1次,由 Server 配置中的 max_fails 選項(xiàng)決定),在規(guī)定時(shí)間內(nèi)(默 認(rèn)是10S,由 Server 配置中的 fail_timeout 選項(xiàng)決定),不會(huì)再向該服務(wù)器調(diào)度請(qǐng)求,直到超過規(guī)定時(shí) 間后再次向該服務(wù)器調(diào)度請(qǐng)求,如果再次調(diào)度該服務(wù)器還是不可用,則繼續(xù)等待一個(gè)時(shí)間段,如果再次 調(diào)度該服務(wù)器可用,則恢復(fù)該服務(wù)器到調(diào)度列表中
upstream group1{
server 10.0.0.161;
server 10.0.0.151;
}
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
#停止 10.0.0.161 上的Nginx 服務(wù)
[root@ubuntu ~]# systemctl stop nginx.service
#客戶端測(cè)試,檢測(cè)到 10.0.0.161不可用,將請(qǐng)求都調(diào)度到 10.0.0.151
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
#啟用 10.0.0.161 上的Nginx 服務(wù),客戶端需要在距離上次檢測(cè)該服務(wù)器不可用10S后才能調(diào)度到該服務(wù)器
[root@ubuntu ~]# systemctl stop nginx.service
#如果后端服務(wù)器上的資源不存在,則不會(huì)影響調(diào)度,會(huì)返回對(duì)應(yīng)的狀態(tài)碼和狀態(tài)頁
[root@ubuntu ~]# mv /var/www/html/www.a30.com/index.html{,bak}
#客戶端測(cè)試
[root@ubuntu ~]# curl www.a30.com/index.html
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com/index.html
設(shè)置備用服務(wù)器
#設(shè)置 backup,當(dāng) 10.0.0.161 和 10.0.0.151 都不可用時(shí),請(qǐng)求會(huì)被調(diào)度到 10.0.0.213
upstream group1{
server 10.0.0.161;
server 10.0.0.151;
server 10.0.0.213 backup;
}
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
#當(dāng)前151 和 161 可用,客戶端測(cè)試
[root@ubuntu ~]# curl www.a30.com
10.0.0.161 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.161 index
#停止151和161的 Nginx 服務(wù)后再次測(cè)試
[root@ubuntu ~]# curl www.a30.com
10.0.0.213
設(shè)置后端服務(wù)器平滑下線
#Proxy Server 配置
upstream group1{
server 10.0.0.161;
server 10.0.0.151;
}
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
#后端服務(wù)器配置
server {
listen 80;
root /var/www/html/www.a30.com;
server_name www.a30.com;
limit_rate 10k;
}
#客戶端測(cè)試 - 開啟兩個(gè)窗口下載文件
[root@ubuntu ~]# wget http://www.a30.com/test.img
#在10.0.0.161 上查看,有一個(gè)連接
[root@ubuntu ~]# ss -tnep | grep 80
#在10.0.0.151 上查看,也有一個(gè)連接
[root@ubuntu ~]# ss -tnep | grep 80
#修改Proxy Server 配置,將 10.0.0.161 下線
upstream group1{
server 10.0.0.161 down;
server 10.0.0.151;
}
#重載生效
[root@ubuntu ~]# nginx -s reload
#你會(huì)發(fā)現(xiàn)原來保持的下載連接沒有中斷,但新的請(qǐng)求,不會(huì)再被調(diào)度到 10.0.0.161
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
負(fù)載均衡調(diào)度算法
源IP地址hash
ip_hash 算法只使用 IPV4 的前 24 位做 hash 運(yùn)算,如果客戶端IP前24位一致,則會(huì)被調(diào)度到同一臺(tái)后 端服務(wù)器
#Proxy Server 配置
upstream group1{
ip_hash;
server 10.0.0.161;
server 10.0.0.151;
}
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
#10.0.0.208 - 客戶端測(cè)試,被調(diào)度到 10.0.0.151
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
#10.0.0.213 - 客戶端測(cè)試,被調(diào)度到 10.0.0.151
[root@rocky ~]# curl www.a30.com
10.0.0.151 index
[root@rocky ~]# curl www.a30.com
10.0.0.151 index
[root@rocky ~]# curl www.a30.com
10.0.0.151 index
使用自行指定的 key 做 hash 調(diào)度
#Proxy Server 配置
#三臺(tái) server 權(quán)重一樣,調(diào)度算法 hash($remoute_addr)%3,值為 0,1,2根據(jù)不同的值調(diào)度到不同server
upstream group1{
hash $remote_addr;
server 10.0.0.161;
server 10.0.0.151;
server 10.0.0.213;
}
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
#10.0.0.208 - 客戶端測(cè)試,被調(diào)度到 10.0.0.151
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl www.a30.com
10.0.0.151 index
#10.0.0.207 - 客戶端測(cè)試,被調(diào)度到 10.0.0.213
[root@rocky ~]# curl www.a30.com
10.0.0.213 index
[root@rocky ~]# curl www.a30.com
10.0.0.213 index
[root@rocky ~]# curl www.a30.com
10.0.0.213 index
#三臺(tái) server 權(quán)重不一樣,調(diào)度算法 hash($remoute_addr)%(1+2+3),值為 0,1,2,3,4,5
#0 調(diào)度到 161
#1,2調(diào)度到 151
#3,4,5調(diào)度到 213
upstream group1{
hash $remote_addr;
server 10.0.0.161 weight=1;
server 10.0.0.151 weight=2;
server 10.0.0.213 weight=3;
}
#Proxy Server 配置
#根據(jù) request_uri 進(jìn)行調(diào)度,不同客戶端訪問同一個(gè)資源會(huì)被調(diào)度到同一臺(tái)后端服務(wù)器上
upstream group1{
hash $request_uri;
server 10.0.0.161;
server 10.0.0.151;
server 10.0.0.213;
}
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
#10.0.0.208 - 客戶端測(cè)試
[root@ubuntu ~]# curl www.a30.com/index.html
10.0.0.213 index
[root@ubuntu ~]# curl www.a30.com/index.html
10.0.0.213 index
[root@ubuntu ~]# curl www.a30.com/index.html
10.0.0.213 index
[root@ubuntu ~]# curl www.a30.com/a.html
10.0.0.161 aaa
[root@ubuntu ~]# curl www.a30.com/a.html
10.0.0.161 aaa
[root@ubuntu ~]# curl www.a30.com/a.html
10.0.0.161 aaa
#10.0.0.207 - 客戶端測(cè)試
[root@rocky ~]# curl www.a30.com
10.0.0.213 index
[root@rocky ~]# curl www.a30.com
10.0.0.213 index
[root@rocky ~]# curl www.a30.com
10.0.0.213 index
[root@rocky ~]# curl www.a30.com/a.html
10.0.0.161 aaa
[root@rocky ~]# curl www.a30.com/a.html
10.0.0.161 aaa
最少連接調(diào)度算法
#Proxy Server 配置,最少連接調(diào)度算法
upstream group1{
least_conn;
server 10.0.0.161;
server 10.0.0.151;
}
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
#客戶端開啟一個(gè)下載連接,限速,讓該連接一直保持
[root@ubuntu ~]# wget www.a30.com/test.img
#下載請(qǐng)求被調(diào)度到 10.0.0.161上了
[root@ubuntu ~]# ss -tnep | grep 80
#新開客戶端測(cè)試,請(qǐng)求不會(huì)被調(diào)度到 10.0.0.161 上
[root@rocky ~]# curl www.a30.com
10.0.0.151 index
[root@rocky ~]# curl www.a30.com
10.0.0.151 index
[root@rocky ~]# curl www.a30.com
10.0.0.151 index
一致性 hash
#Proxy Server 配置
#三臺(tái) server 權(quán)重一樣,調(diào)度算法 hash($remoute_addr)%3,值為 0,1,2 根據(jù)不同的值調(diào)度到不
同 server
upstream group1{
hash $remote_addr;
server 10.0.0.161;
server 10.0.0.151;
server 10.0.0.213;
}
server{
listen 80;
server_name www.a30.com;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
在上述配置中,三臺(tái)后端服務(wù)器的權(quán)重都為 1,則總權(quán)重為 3,再使用客戶端IP的 hash 值對(duì)總權(quán)重求余
假設(shè)當(dāng)前調(diào)度情況如下
hash($remoute_addr)
hash($remoute_addr)%3
server
3,6,9
0,0,0
10.0.0.161
1,4,7
1,1,1
10.0.0.151
2,5,8
2,2,2
10.0.0.213
此時(shí)如果后端新增一臺(tái)服務(wù)器,則總權(quán)重會(huì)變?yōu)?4,那么同樣的 hash 值,最后的調(diào)度結(jié)果如下
hash($remoute_addr)
hash($remoute_addr)%4
server
4,8
0,0
10.0.0.161
1,5,9
1,1,1
10.0.0.151
2,6
2,2
10.0.0.213
3,7
3,3
10.0.0.223
我們會(huì)發(fā)現(xiàn),新增后端服務(wù)器后,總權(quán)重發(fā)生變化,則所有前端的請(qǐng)求都會(huì)被重新計(jì)算,調(diào)度到和原來 不同的后端服務(wù)器上了,這樣會(huì)導(dǎo)致在原來后端服務(wù)器上創(chuàng)建的數(shù)據(jù),在新的服務(wù)器上沒有了減少后端服務(wù)器或修改后端服務(wù)器權(quán)重,都會(huì)導(dǎo)致重新調(diào)度,會(huì)導(dǎo)致原有緩存數(shù)據(jù)失效(例如登錄狀 態(tài),購物車等)
一致性哈希
一致性哈希(Consistent Hashing)是一種用于分布式系統(tǒng)中數(shù)據(jù)分片和負(fù)載均衡的算法,其中 的"hash環(huán)"是該算法的核心概念之一
在一致性哈希中,所有可能的數(shù)據(jù)節(jié)點(diǎn)或服務(wù)器被映射到一個(gè)虛擬的環(huán)上。這個(gè)環(huán)的范圍通常是一個(gè)固 定的哈希空間,比如0到2^32-1,每個(gè)數(shù)據(jù)節(jié)點(diǎn)或服務(wù)器被映射到環(huán)上的一個(gè)點(diǎn),通過對(duì)其進(jìn)行哈希計(jì) 算得到。這個(gè)哈希值的范圍也是在0到2^32-1之間
在這個(gè)環(huán)上,數(shù)據(jù)會(huì)被分散到最接近它的節(jié)點(diǎn)。當(dāng)有新的數(shù)據(jù)要存儲(chǔ)時(shí),首先通過哈希計(jì)算得到該數(shù)據(jù) 的哈希值,然后在環(huán)上找到離這個(gè)哈希值最近的節(jié)點(diǎn),將數(shù)據(jù)存儲(chǔ)在這個(gè)節(jié)點(diǎn)上。同樣,當(dāng)要查詢數(shù)據(jù) 時(shí),也是通過哈希計(jì)算得到數(shù)據(jù)的哈希值,然后找到最近的節(jié)點(diǎn)進(jìn)行查詢
由于哈希環(huán)是一個(gè)環(huán)形結(jié)構(gòu),節(jié)點(diǎn)的添加和刪除對(duì)整體的影響相對(duì)較小。當(dāng)添加或刪除節(jié)點(diǎn)時(shí),只有相 鄰的節(jié)點(diǎn)受到影響,而其他節(jié)點(diǎn)保持不變。這使得一致性哈希算法在分布式系統(tǒng)中能夠提供較好的負(fù)載 均衡性能,同時(shí)減小了數(shù)據(jù)遷移的開銷
總的來說,一致性哈希中的哈希環(huán)是通過哈希計(jì)算將數(shù)據(jù)節(jié)點(diǎn)映射到環(huán)上,以實(shí)現(xiàn)數(shù)據(jù)分片和負(fù)載均衡 的分布式算法
環(huán)偏移
在一致性哈希中,哈希環(huán)可能會(huì)面臨的一個(gè)問題是環(huán)偏移(Ring Wrapping)。環(huán)偏移指的是哈希環(huán)上 的某個(gè)區(qū)域過于擁擠,而其他區(qū)域相對(duì)空閑,這可能導(dǎo)致負(fù)載不均衡。為了解決這個(gè)問題,一些改進(jìn)的 一致性哈希算法引入了虛擬節(jié)點(diǎn)(Virtual Nodes)的概念
虛擬節(jié)點(diǎn)是對(duì)物理節(jié)點(diǎn)的一種擴(kuò)展,通過為每個(gè)物理節(jié)點(diǎn)創(chuàng)建多個(gè)虛擬節(jié)點(diǎn),將它們均勻地分布在哈希 環(huán)上。這樣一來,每個(gè)物理節(jié)點(diǎn)在環(huán)上的位置會(huì)有多個(gè)副本,而不是只有一個(gè)位置。這樣一來,即使哈 希環(huán)上的某個(gè)區(qū)域過于擁擠,也可以通過調(diào)整虛擬節(jié)點(diǎn)的數(shù)量來使得負(fù)載更均衡
綜合案例
實(shí)現(xiàn) http 自動(dòng)重定向至 https,并將客戶端 https 請(qǐng)求通過負(fù)載均衡的方式反向代理到后端的多臺(tái) http 服務(wù)器上
# upstream 配置
upstream group1{
server 10.0.0.161;
server 10.0.0.151;
}
# http 重定向到 https
server{
listen 80;
server_name www.a30.com;
return 302 https://$server_name$request_uri;
}
# https 配置
server{
listen 443 ssl http2;
server_name www.a30.com;
ssl_certificate /usr/share/easy-rsa/pki/www.a30.com.pem;
ssl_certificate_key /usr/share/easy-rsa/pki/private/www.a30.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
location /{
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
#客戶端測(cè)試
[root@ubuntu ~]# curl -Lk www.a30.com
10.0.0.151 index
[root@ubuntu ~]# curl -LkI www.a30.com
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Sat, 17 Feb 2024 13:55:01 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
Location: https://www.a30.com/
HTTP/2 200
server: nginx
date: Sat, 17 Feb 2024 13:55:01 GMT
content-type: text/html; charset=utf8
content-length: 17
last-modified: Wed, 14 Feb 2024 02:45:15 GMT
etag: "65cc293b-11"
accept-ranges: bytes
Nginx 的四層代理和負(fù)載
相關(guān)指令和參數(shù)
Nginx在1.9.0版本開始支持tcp模式的負(fù)載均衡,在1.9.13版本開始支持udp協(xié)議的負(fù)載,udp主要用于 DNS的域名解析,其配置方式和指令和http 代理類似,其基于ngx_stream_proxy_module模塊實(shí)現(xiàn)tcp 負(fù)載,另外基于模塊ngx_stream_upstream_module實(shí)現(xiàn)后端服務(wù)器分組轉(zhuǎn)發(fā)、權(quán)重分配、狀態(tài)監(jiān)測(cè)、 調(diào)度算法等高級(jí)功能
如果編譯安裝,需要指定 –with-stream 選項(xiàng)才能支持 ngx_stream_proxy_module模塊
實(shí)現(xiàn)TCP協(xié)議的反向代理
#10.0.0.206 Proxy Server 配置,此配置要寫在最外層
stream{
server{
listen 3306;
proxy_pass 10.0.0.161:3306;
}
server{
listen 6379;
proxy_pass 10.0.0.151:6379;
}
}
#10.0.0.161 安裝mysql-server,并配置遠(yuǎn)程用戶
[root@ubuntu ~]# apt update;apt install mysql-server
mysql> create user proxyer@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#當(dāng)前mysql-server 只監(jiān)聽了本機(jī)的3306
[root@ubuntu ~]# ss -tnlp | grep 3306
#修改配置,注釋掉這兩行
[root@ubuntu ~]# vim /etc/mysql/mysql.conf.d/mysqld.cnf
#bind-address = 127.0.0.1
#mysqlx-bind-address = 127.0.0.1
skip-name-resolve #添加此行,跳過主機(jī)名反解
#重啟服務(wù)
[root@ubuntu ~]# systemctl restart mysql.service
[root@ubuntu ~]# ss -tnlp | grep 3306
#10.0.0.151 安裝redis-server
[root@ubuntu ~]# apt update;apt install redis-server
#當(dāng)前只監(jiān)聽了127.1
[root@ubuntu ~]# ss -tnlp | grep 6379
#修改配置并重啟
[root@ubuntu ~]# vim /etc/redis/redis.conf
#bind 127.0.0.1 ::1
protected-mode no #關(guān)閉保護(hù)模式
[root@ubuntu ~]# systemctl restart redis-server.service
[root@ubuntu ~]# ss -tnlp | grep 6379
#客戶端配置,并測(cè)試
[root@ubuntu ~]# apt update;
[root@ubuntu ~]# apt install mysql-client-8.0 redis
#直連測(cè)試
[root@ubuntu ~]# mysql -h 10.0.0.161 -uproxyer -p'123456'
#redis 測(cè)試,客戶端直連
[root@ubuntu ~]# redis-cli -h 10.0.0.151
#代理測(cè)試
[root@ubuntu ~]# mysql -h 10.0.0.206 -uproxyer -p'123456'
#redis 測(cè)試
[root@ubuntu ~]# redis-cli -h 10.0.0.206
實(shí)現(xiàn)TCP協(xié)議的負(fù)載均衡
Proxy Server 配置,此配置要寫在最外層
stream{
upstream mysql{
server 10.0.0.161:3306;
server 10.0.0.151:3306;
}
upstream redis{
server 10.0.0.161:6379;
server 10.0.0.151:6379;
}
server{
listen 3306;
proxy_pass mysql;
}
server{
listen 6379;
proxy_pass redis;
}
}
實(shí)現(xiàn) FastCGI 代理
相關(guān)指令和參數(shù)
fastcgi_index name; # 后端 FastCGI 服務(wù)器默認(rèn)資源,默認(rèn)值為空,
# 作用域 http, server, location
fastcgi_pass address; # 指定后端 FastCGI 服務(wù)器地址,可以寫 IP:port,也可以指定socket 文件
# 作用域 location, if in location
fastcgi_param parameter value [if_not_empty];# 設(shè)置傳遞給FastCGI服務(wù)器的參數(shù)值,可以是文本,變量或組合,可用于將Nginx的內(nèi)置變量賦值給自定義key
# 作用域 http, server, location
柚子快報(bào)邀請(qǐng)碼778899分享:運(yùn)維 Nginx 反向代理
好文鏈接
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。