欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

首頁綜合 正文
目錄

柚子快報邀請碼778899分享:運維 Nginx 反向代理

柚子快報邀請碼778899分享:運維 Nginx 反向代理

http://yzkb.51969.com/

Nginx 反向代理

代理基礎(chǔ)知識

代理分為兩種,分別是正向代理和反向代理

正向代理(Forward Proxy) 和 反向代理(Reverse Proxy) 是兩種常見的代理服務器,它們用于處理 網(wǎng)絡通信中的不同方向和用途

正向代理(Forward Proxy)

特點

代理服務器位于客戶端和目標服務器之間 客戶端向代理服務器發(fā)送請求,代理服務器將請求發(fā)送到目標服務器,并將目標服務器的響應返回給客戶端 目標服務器不知道客戶端的存在,它只知道有一個代理服務器向其發(fā)送請求 客戶端通過正向代理訪問互聯(lián)網(wǎng)資源時,通常需要配置客戶端來使用代理

用途

突破訪問限制:用于繞過網(wǎng)絡訪問限制,訪問受限制的資源 隱藏客戶端身份:客戶端可以通過正向代理隱藏其真實 IP 地址

反向代理(Reverse Proxy)

特點

代理服務器位于目標服務器和客戶端之間 客戶端向代理服務器發(fā)送請求,代理服務器將請求轉(zhuǎn)發(fā)給一個或多個目標服務器,并將其中一個目標服務器的響應返回給客戶端 目標服務器不知道最終客戶端的身份,只知道有一個代理服務器向其發(fā)送請求 用于將客戶端的請求分發(fā)給多個服務器,實現(xiàn)負載均衡

用途

負載均衡:通過將流量分發(fā)到多個服務器,確保服務器的負載均勻分布 緩存和加速:反向代理可以緩存靜態(tài)內(nèi)容,減輕目標服務器的負載,并提高訪問速度 安全性:隱藏真實服務器的信息,提高安全性,同時可以進行 SSL 終止(SSL Termination)

相同和不同

相同點

中間層:正向代理和反向代理都是位于客戶端和目標服務器之間的中間層。 代理功能:它們都充當了代理的角色,處理請求和響應,使得通信更加靈活和安全

不同點

方向:正向代理代理客戶端,反向代理代理服務器 目的:正向代理主要用于訪問控制和隱藏客戶端身份,反向代理主要用于負載均衡、緩存和提高安全性 配置:客戶端需要配置使用正向代理,而反向代理是對服務器透明的,客戶端無需感知

Nginx 和 LVS

Nginx 和 LVS(Linux Virtual Server) 都是流行的代理和負載均衡解決方案,但它們有一些不同的特點 和應用場景

選擇使用 Nginx 還是 LVS 取決于具體的應用需求和復雜度。Nginx 更適合作為 Web 服務器和應用層負 載均衡器,而 LVS 更適用于傳輸層負載均衡

相同點

負載均衡:Nginx 和 LVS 都可以作為負載均衡器,將流量分發(fā)到多個后端服務器,提高系統(tǒng)的可用 性和性能。 性能:Nginx 和 LVS 都具有高性能的特點,能夠處理大量并發(fā)連接和請求

不同點

層次:Nginx 在應用層進行負載均衡和反向代理,而 LVS 在傳輸層進行負載均衡 功能:Nginx 除了負載均衡外,還可以作為反向代理和靜態(tài)文件服務器;而 LVS 主要專注于負載均 衡,實現(xiàn)簡單而高效的四層分發(fā) 配置和管理:Nginx 配置相對簡單,易于管理,適用于各種規(guī)模的應用;LVS 需要深入了解 Linux 內(nèi)核和相關(guān)配置,適用于大規(guī)模和對性能有更高要求的場景

LVS 不監(jiān)聽端口,不處理請求數(shù)據(jù),不參與握手流程,只會在內(nèi)核層轉(zhuǎn)發(fā)數(shù)據(jù)報文

Nginx 需要在應用層接收請求,根據(jù)客戶端的請求參數(shù)和Nginx中配置的規(guī)則,再重新作為客戶端向后 端服務器發(fā)起請求

LVS 通常做四層代理,Nginx 做七層代理

實現(xiàn) http 協(xié)議反向代理

相關(guān)指令和參數(shù)

Nginx 可以基于ngx_http_proxy_module 模塊提供 http 協(xié)議的反向代理服務,該模塊是 Nginx 的默認模塊

proxy_pass URL; # 轉(zhuǎn)發(fā)的后端服務器地址,可以寫主機名,域名,IP地址,也可以額外指定端口,

# 作用域 location, if in location, limit_except

proxy_hide_header field; # Nginx 默認不會將后端服務器的 Date,Server,X-Pad,X-Accel-... 這些響應頭信息傳給

# 客戶端,除了這些之外的響應頭字段會回傳,

# 可以使用 proxy_hide_header 顯式指定不回傳的響應頭字段

# 作用域 http, server, location

proxy_pass_header field; # 顯式指定要回傳給客戶端的后端服務器響應頭中的字段,

#作用域 http, server, location

proxy_pass_request_body on|off; # 是否向后端服務器發(fā)送客戶端 http 請求的 body 部份,默認 on,

# 作用域 http, server, location

proxy_pass_request_headers on|off; # 是否向后端服務器發(fā)送客戶端 http 請求的頭部信息,默認 on

# 作用域 http, server, location

proxy_connect_timeout time; # Nginx與后端服務器建立連接超時時長,默認60S,超時會向客戶端返回504

# 作用域 http, server, location

proxy_read_timeout time; # Nginx 等待后端服務器返回數(shù)據(jù)的超時時長,默認60S,超時會向客戶端返回504

# 作用域 http, server, location

proxy_send_timeout time; # Nginx 向后端服務器發(fā)送請求的超時時長,默認60S,超時會向客戶端返回408

# 作用域 http, server, location

proxy_set_body value; # 重新定義傳給后端服務器的請求的正文,可以包含文本,變量等,

# 作用域 http, server, location

proxy_set_header field value; # 更改或添加請求頭字段并發(fā)送到后端服務器,

# 作用域http, server, location

proxy_http_version 1.0|1.1; # 設(shè)置向后端服務器發(fā)送請求時的 http 協(xié)議版本,默認值1.0,

# 作用域http, server, location

proxy_ignore_client_abort on|off; # 客戶端中斷連接,Nginx 是否繼續(xù)執(zhí)行與后端的連接,默認值 off

#客戶端中斷,Nginx 也會中斷后端連接, on 表示 客戶端中斷,nginx 還會繼續(xù)處理與后端在連接

# 作用域 http, server, location

proxy_headers_hash_bucket_size size; # 當配置了 proxy_hide_header和proxy_set_header的時候,

# 用于設(shè)置nginx保存HTTP報文頭的hash表的大小,默認值 64

# 作用域 http, server, location

proxy_headers_hash_max_size size; # 上一個參數(shù)的上限,默認值 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 ...;

# 當前配置的后端服務器無法提供服務時,因為何種錯誤而去請求下一個后端服務器

# 默認值 error timeout, 表示當前后端服務器因為error 和 timeout 錯誤時,去請求 另一個后端服務器

# 作用域 http, server, location

proxy_cache zone|off; # 是否啟用代理緩存,默認 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,默認沒有設(shè)置

# path 表示緩存數(shù)據(jù)存放路徑,要保證nginx 有寫權(quán)限

# levels 表示緩存數(shù)據(jù)目錄層級,16進制表示,levels=1:2 表示第一級有16個目錄,0-f,第一級中每個目錄下有16*16個子目錄,00-ff

# keys_zone=name:size zone 表示緩存名稱,先定義后使用,size 表示該zone 空間大小

# inactive 表示緩存數(shù)據(jù)生命周期,默認值10分鐘

# max_size 表示緩存占用的磁盤空間最大能有多大

proxy_cache_key string; # 指定緩存數(shù)據(jù)的key,不同的key 對應不同的緩存文件,

#作用域 http, server, location

# 默認值 $scheme$proxy_host$request_uri

proxy_cache_valid [code ...] time; # 為不同響應狀態(tài)碼的數(shù)據(jù)設(shè)置不同的緩存時長,可設(shè)置多條,默認不設(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 ...;

# 在后端服務器報哪些錯誤的情況下,直接使用過期緩存數(shù)據(jù)響應客戶端請求默認off,

#作用域 http, server, location

proxy_cache_methods GET|HEAD|POST ...; # 緩存哪些請求類型的數(shù)據(jù),默認值 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服務,請求的是默認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;

}

}

#后端主機配置

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;

}

}

#后端主機配置

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

#測試

[root@ubuntu ~]# curl www.a30.com

node-1

#透傳指定參數(shù)

#在上述請求中,客戶端訪問 http://www.a30.com,該主機收到請求后作為客戶端去請求http://www.node-1.com

#客戶端主機配置

[root@ubuntu ~]# cat /etc/hosts

10.0.0.206 www.a30.com

#中間主機配置

[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; #將客戶端從請求頭中傳來的 Host 值傳給后端服務器

}

}

#后端主機配置

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

#客戶端測試

[root@ubuntu ~]# curl http://www.a30.com

a30

#如果后端服務不可用,從客戶端訪問會返回502

#停止后端nginx

[root@ubuntu ~]# systemctl stop nginx.service

#客戶端訪問中間Nginx

[root@ubuntu ~]# curl http://www.a30.com

502 Bad Gateway

502 Bad Gateway


nginx

[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

實現(xiàn)動靜分離

根據(jù)條件進行調(diào)度,實現(xiàn)動靜分離

角色

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測試

[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";

}

實現(xiàn)對特定資源的代理

location ~ \.(jpe?g|png|bmp|gif)$ {

proxy_pass http://10.0.0.161;

proxy_set_header Host "api.a30.com";

}

代理服務器實現(xiàn)數(shù)據(jù)緩存

前置條件:各服務器時間和時區(qū)先統(tǒng)一,方便測試

#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

#客戶端測試

[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

#并不是一個文本文件

[root@ubuntu22:sites-enabled]# file /tmp/proxycache/3/ab/2d291e4d45687e428f0215bec190aab3

/tmp/proxycache/3/ab/2d291e4d45687e428f0215bec190aab3: data

#查看當前時間和緩存文件時間

[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),后端服務器內(nèi)容發(fā)生了更新,客戶端獲取的還是緩存數(shù)據(jù)

#后端真實數(shù)據(jù)刪除,客戶端還能拿到緩存數(shù)據(jù)

實現(xiàn)客戶端IP地址透傳

在使用Nginx 做代理的情況下,默認后端服務器無法獲取客戶端真實IP地址

角色

IP

Client

10.0.0.158

Proxy Server

10.0.0.157

Real Server

10.0.0.151

默認情況下,后端服務器無法獲取真實客戶端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測試

[root@Rocky-9 ~]# curl www.a30.com

10.0.0.157------

#后端服務器只能獲取代理服務器IP

修改代理服務器配置,透傳真實客戶端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追加請求報文中X-Forwarded-For首部字段,多個IP之間用逗號分隔,如果請求中沒有X-Forwarded-For,就使用$remote_addr

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

#客戶端測試 $remote_addr 獲取代理IP,$http_x_real_ip 獲取真實客戶端IP,

$http_x_forwarded_for 獲取真實客戶端IP

#客戶端測試

[root@Rocky-9 ~]# curl www.a30.com

10.0.0.157---10.0.0.158---10.0.0.158

實現(xiàn)多級代理客戶端IP透傳

角色

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};

}

}

#客戶端測試,Real Server 只能通過 $remote_addr 獲取上一級代理的IP

[root@Rocky-9 ~]# curl www.a30.com

10.0.0.157------

修改第一級代理服務器配置

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;

}

}

#客戶端測試 $remote_addr 獲取上一級代理IP,$http_x_real_ip 獲取真實客戶端IP,$http_x_forwarded_for 獲取真實客戶端IP

[root@Rocky-9 ~]# curl www.a30.com

10.0.0.157---10.0.0.158---10.0.0.158

繼續(xù)修改第二級代理服務器配置

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;

}

}

#客戶端測試

# $remote_addr 獲取上一級代理IP

# $http_x_real_ip 獲取上上一級代理IP

# $http_x_forwarded_for 累加了第一級代理的 X-Forwarded-For 和第二級代理的 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

第一級代理不透傳,不添加請求頭字

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

實現(xiàn) http 協(xié)議反向代理的負載均衡

相關(guān)指令和參數(shù)

在實現(xiàn) Nginx 反向代理的基礎(chǔ)上,可以基于 ngx_http_upstream_module 模塊實現(xiàn)后端服務器的分 組,權(quán)重分配,狀態(tài)監(jiān)測,調(diào)度算法等高級功能

upstream name { server address [parameters]; } # 定義一個后端服務器組,可以包含一臺或多臺服務器,

# 定義好后在 proxy_pass 指令中引用,作用域 http

server address [parameters]; # 在 upstream 中定義一個具體的后端服務器,作用域upstream

# address 指定后端服務器 可以是IP地址,主機名,或UNIX Socket,可以加端口號

# parameters 是可選摻數(shù),具體有以下幾個屬性

# weight=number 指定該 server 的權(quán)重,默認值都是1

# max_conns=number 該 Server 的最大活動連接數(shù),達到后將不再給該 Server 發(fā)送請求,默認值0,表示不限制

# max_fails=number 后端服務器的下線條件,當客戶端訪問時,對本次調(diào)度選中的后端服務器連續(xù)進行檢測多少次,如果都失敗就標記為不可用,默認為1次,當客戶端訪問時,才會利用TCP觸發(fā)對探測后端服務器健康性檢查,而非周期性的探測

# fail_timeout=time 后端服務器的上線條件,對已經(jīng)檢測到處于不可用的后端服務器,每隔此時間間隔再次進行檢測是否恢復可用,如果發(fā)現(xiàn)可用,則將后端服務器參與調(diào)度,默認為10秒

# backup 標記該 Server 為備用,當所有后端服務器不可用時,才使用此服務器

# down 標記該 Server 臨時不可用,可用于平滑下線后端服務器,新請求不再調(diào)度到此服務器,原有連接不受影響

hash key [consistent]; # 使用自行指定的 Key 做 hash 運算后進行調(diào)度,Key 可以是變量,比如請求頭中的字段,URI等,如果對應的 server 條目配置發(fā)生了變化,會導致相同的 key 被重新hash

# consistent 表示使用一致性 hash,此參數(shù)確保該upstream 中的 server 條目發(fā)生變化時,盡可能少的重新 hash,適用于做緩存服務的場景,提高緩存命中率

# 作用域 upstream

ip_hash; # 源地址hash調(diào)度方法,基于的客戶端的remote_addr(源地址IPv4的前24位或整個IPv6地址)做hash計算,以實現(xiàn)會話保持,作用域 upstream

least_conn; # 最少連接調(diào)度算法,優(yōu)先將客戶端請求調(diào)度到當前連接最少的后端服務器,相當于LVS中的 LC 算法

# 配合權(quán)重,能實現(xiàn)類似于 LVS 中的 WLC 算法

# 作用域 upstream

keepalive connections; # 為每個 worker 進程保留最多多少個空閑?;钸B接數(shù),超過此值,最近最少使用的連接將被關(guān)閉

# 默認不設(shè)置,作用域 upstream

keepalive_time time; # 空閑連接保持的時長,超過該時間,一直沒使用的空閑連接將被銷毀

# 默認值 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

#客戶端測試-輪循調(diào)度到后端服務器

[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)重

#每個 server 配置的默認權(quán)重是1,這種寫法,兩個 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

限制最大活動連接數(shù)

#10.0.0.161 同時只能維持兩個活動連接

upstream group1{

server 10.0.0.161 max_conns=2;

server 10.0.0.151;

}

#后端服務器配置

server {

listen 80;

root /var/www/html/www.a30.com;

server_name www.a30.com;

limit_rate 10k;

}

#客戶端測試,開6個窗口下載文件

[root@ubuntu ~]# wget http://www.a30.com/test.img

#查看 10.0.0.161 上的連接,2個活動連接

[root@ubuntu ~]# ss -tnpe | grep 80

#查看 10.0.0.151 上的連接,4個活動連接

#客戶端繼續(xù)測試,新的請求都不會調(diào)度給 10.0.0.161

后端服務器健康性檢查

Nginx 的 upstream 指令對于后端服務器的健康性檢查是被動檢查,當有客戶端請求被調(diào)度到該服務器 上時,會在TCP協(xié)議層的三次握手時檢查該服務器是否可用,如果不可用就調(diào)度到別的服務器,當不可 用的次數(shù)達到指定次數(shù)時(默認是1次,由 Server 配置中的 max_fails 選項決定),在規(guī)定時間內(nèi)(默 認是10S,由 Server 配置中的 fail_timeout 選項決定),不會再向該服務器調(diào)度請求,直到超過規(guī)定時 間后再次向該服務器調(diào)度請求,如果再次調(diào)度該服務器還是不可用,則繼續(xù)等待一個時間段,如果再次 調(diào)度該服務器可用,則恢復該服務器到調(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 服務

[root@ubuntu ~]# systemctl stop nginx.service

#客戶端測試,檢測到 10.0.0.161不可用,將請求都調(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 服務,客戶端需要在距離上次檢測該服務器不可用10S后才能調(diào)度到該服務器

[root@ubuntu ~]# systemctl stop nginx.service

#如果后端服務器上的資源不存在,則不會影響調(diào)度,會返回對應的狀態(tài)碼和狀態(tài)頁

[root@ubuntu ~]# mv /var/www/html/www.a30.com/index.html{,bak}

#客戶端測試

[root@ubuntu ~]# curl www.a30.com/index.html

10.0.0.151 index

[root@ubuntu ~]# curl www.a30.com/index.html

404 Not Found

設(shè)置備用服務器

#設(shè)置 backup,當 10.0.0.161 和 10.0.0.151 都不可用時,請求會被調(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;

}

}

#當前151 和 161 可用,客戶端測試

[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 服務后再次測試

[root@ubuntu ~]# curl www.a30.com

10.0.0.213

設(shè)置后端服務器平滑下線

#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;

}

}

#后端服務器配置

server {

listen 80;

root /var/www/html/www.a30.com;

server_name www.a30.com;

limit_rate 10k;

}

#客戶端測試 - 開啟兩個窗口下載文件

[root@ubuntu ~]# wget http://www.a30.com/test.img

#在10.0.0.161 上查看,有一個連接

[root@ubuntu ~]# ss -tnep | grep 80

#在10.0.0.151 上查看,也有一個連接

[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

#你會發(fā)現(xiàn)原來保持的下載連接沒有中斷,但新的請求,不會再被調(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

負載均衡調(diào)度算法

源IP地址hash

ip_hash 算法只使用 IPV4 的前 24 位做 hash 運算,如果客戶端IP前24位一致,則會被調(diào)度到同一臺后 端服務器

#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 - 客戶端測試,被調(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 - 客戶端測試,被調(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 配置

#三臺 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 - 客戶端測試,被調(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 - 客戶端測試,被調(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

#三臺 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 進行調(diào)度,不同客戶端訪問同一個資源會被調(diào)度到同一臺后端服務器上

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 - 客戶端測試

[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 - 客戶端測試

[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;

}

}

#客戶端開啟一個下載連接,限速,讓該連接一直保持

[root@ubuntu ~]# wget www.a30.com/test.img

#下載請求被調(diào)度到 10.0.0.161上了

[root@ubuntu ~]# ss -tnep | grep 80

#新開客戶端測試,請求不會被調(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 配置

#三臺 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;

}

}

在上述配置中,三臺后端服務器的權(quán)重都為 1,則總權(quán)重為 3,再使用客戶端IP的 hash 值對總權(quán)重求余

假設(shè)當前調(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

此時如果后端新增一臺服務器,則總權(quán)重會變?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

我們會發(fā)現(xiàn),新增后端服務器后,總權(quán)重發(fā)生變化,則所有前端的請求都會被重新計算,調(diào)度到和原來 不同的后端服務器上了,這樣會導致在原來后端服務器上創(chuàng)建的數(shù)據(jù),在新的服務器上沒有了減少后端服務器或修改后端服務器權(quán)重,都會導致重新調(diào)度,會導致原有緩存數(shù)據(jù)失效(例如登錄狀 態(tài),購物車等)

一致性哈希

一致性哈希(Consistent Hashing)是一種用于分布式系統(tǒng)中數(shù)據(jù)分片和負載均衡的算法,其中 的"hash環(huán)"是該算法的核心概念之一

在一致性哈希中,所有可能的數(shù)據(jù)節(jié)點或服務器被映射到一個虛擬的環(huán)上。這個環(huán)的范圍通常是一個固 定的哈??臻g,比如0到2^32-1,每個數(shù)據(jù)節(jié)點或服務器被映射到環(huán)上的一個點,通過對其進行哈希計 算得到。這個哈希值的范圍也是在0到2^32-1之間

在這個環(huán)上,數(shù)據(jù)會被分散到最接近它的節(jié)點。當有新的數(shù)據(jù)要存儲時,首先通過哈希計算得到該數(shù)據(jù) 的哈希值,然后在環(huán)上找到離這個哈希值最近的節(jié)點,將數(shù)據(jù)存儲在這個節(jié)點上。同樣,當要查詢數(shù)據(jù) 時,也是通過哈希計算得到數(shù)據(jù)的哈希值,然后找到最近的節(jié)點進行查詢

由于哈希環(huán)是一個環(huán)形結(jié)構(gòu),節(jié)點的添加和刪除對整體的影響相對較小。當添加或刪除節(jié)點時,只有相 鄰的節(jié)點受到影響,而其他節(jié)點保持不變。這使得一致性哈希算法在分布式系統(tǒng)中能夠提供較好的負載 均衡性能,同時減小了數(shù)據(jù)遷移的開銷

總的來說,一致性哈希中的哈希環(huán)是通過哈希計算將數(shù)據(jù)節(jié)點映射到環(huán)上,以實現(xiàn)數(shù)據(jù)分片和負載均衡 的分布式算法

環(huán)偏移

在一致性哈希中,哈希環(huán)可能會面臨的一個問題是環(huán)偏移(Ring Wrapping)。環(huán)偏移指的是哈希環(huán)上 的某個區(qū)域過于擁擠,而其他區(qū)域相對空閑,這可能導致負載不均衡。為了解決這個問題,一些改進的 一致性哈希算法引入了虛擬節(jié)點(Virtual Nodes)的概念

虛擬節(jié)點是對物理節(jié)點的一種擴展,通過為每個物理節(jié)點創(chuàng)建多個虛擬節(jié)點,將它們均勻地分布在哈希 環(huán)上。這樣一來,每個物理節(jié)點在環(huán)上的位置會有多個副本,而不是只有一個位置。這樣一來,即使哈 希環(huán)上的某個區(qū)域過于擁擠,也可以通過調(diào)整虛擬節(jié)點的數(shù)量來使得負載更均衡

綜合案例

實現(xiàn) http 自動重定向至 https,并將客戶端 https 請求通過負載均衡的方式反向代理到后端的多臺 http 服務器上

# 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;

}

}

#客戶端測試

[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 的四層代理和負載

相關(guān)指令和參數(shù)

Nginx在1.9.0版本開始支持tcp模式的負載均衡,在1.9.13版本開始支持udp協(xié)議的負載,udp主要用于 DNS的域名解析,其配置方式和指令和http 代理類似,其基于ngx_stream_proxy_module模塊實現(xiàn)tcp 負載,另外基于模塊ngx_stream_upstream_module實現(xiàn)后端服務器分組轉(zhuǎn)發(fā)、權(quán)重分配、狀態(tài)監(jiān)測、 調(diào)度算法等高級功能

如果編譯安裝,需要指定 –with-stream 選項才能支持 ngx_stream_proxy_module模塊

實現(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,并配置遠程用戶

[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)

#當前mysql-server 只監(jiān)聽了本機的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 #添加此行,跳過主機名反解

#重啟服務

[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

#當前只監(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)閉保護模式

[root@ubuntu ~]# systemctl restart redis-server.service

[root@ubuntu ~]# ss -tnlp | grep 6379

#客戶端配置,并測試

[root@ubuntu ~]# apt update;

[root@ubuntu ~]# apt install mysql-client-8.0 redis

#直連測試

[root@ubuntu ~]# mysql -h 10.0.0.161 -uproxyer -p'123456'

#redis 測試,客戶端直連

[root@ubuntu ~]# redis-cli -h 10.0.0.151

#代理測試

[root@ubuntu ~]# mysql -h 10.0.0.206 -uproxyer -p'123456'

#redis 測試

[root@ubuntu ~]# redis-cli -h 10.0.0.206

實現(xiàn)TCP協(xié)議的負載均衡

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;

}

}

實現(xiàn) FastCGI 代理

相關(guān)指令和參數(shù)

fastcgi_index name; # 后端 FastCGI 服務器默認資源,默認值為空,

# 作用域 http, server, location

fastcgi_pass address; # 指定后端 FastCGI 服務器地址,可以寫 IP:port,也可以指定socket 文件

# 作用域 location, if in location

fastcgi_param parameter value [if_not_empty];# 設(shè)置傳遞給FastCGI服務器的參數(shù)值,可以是文本,變量或組合,可用于將Nginx的內(nèi)置變量賦值給自定義key

# 作用域 http, server, location

柚子快報邀請碼778899分享:運維 Nginx 反向代理

http://yzkb.51969.com/

好文鏈接

評論可見,查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。

轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://m.gantiao.com.cn/post/19613973.html

發(fā)布評論

您暫未設(shè)置收款碼

請在主題配置——文章設(shè)置里上傳

掃描二維碼手機訪問

文章目錄