日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

nginx的反向代理以及负载均衡模块的使用

發(fā)布時(shí)間:2025/4/5 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx的反向代理以及负载均衡模块的使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

內(nèi)容:

1、何為代理

2、nginx的反向代理模塊以及使用

3、nginx的負(fù)載均衡模塊以及使用


一、何為代理

代理,由字面意思可以理解為代為服務(wù)的是意思。

代理服務(wù)技術(shù)是一門很古老的技術(shù),是在互聯(lián)網(wǎng)早期出現(xiàn)就使用的技術(shù)。一般實(shí)現(xiàn)代理技術(shù)的方式就是在服務(wù)器上安裝代理服務(wù)軟件,讓其成為一個(gè)代理服務(wù)器,從而實(shí)現(xiàn)代理技術(shù)。常用的代理技術(shù)分為正向代理、反向代理。

正向代理:

正常的用戶訪問站點(diǎn)是客戶打開瀏覽器,然后輸入相關(guān)的網(wǎng)站進(jìn)行瀏覽,其實(shí)這就是一個(gè)代理的行為,只不過代理的是瀏覽器,我們通過瀏覽器來(lái)代理請(qǐng)求站點(diǎn)服務(wù),而正向代理服務(wù)器可以理解為:

?

反向代理:反向代理正好與正向代理相反,對(duì)于客戶端而言代理服務(wù)器就像是原始服務(wù)器,并且客戶端不需要進(jìn)行任何特別的設(shè)置??蛻舳讼蚍聪虼淼拿臻g(name-space)中的內(nèi)容發(fā)送普通請(qǐng)求,接著反向代理將判斷向何處(原始服務(wù)器)轉(zhuǎn)交請(qǐng)求,并將獲得的內(nèi)容返回給客戶端。?

所以,nginx由于起輕量高效的特性,是的其成為了反向代理服務(wù)器的熱門選擇。

二、ngixn的反向代理模塊以及使用

ngx_http_proxy_module模塊:

1、proxy_pass URL;

Context: location, if in location, limit_except

(1)注意:proxy_pass后面的路徑不帶uri時(shí),其會(huì)將location的uri傳遞給后端主機(jī);

server {

...

server_name HOSTNAME;

location /uri/ {

proxy http://hos[:port];

}

...

}

效果:http://HOSTNAME/uri --> http://host/uri

?

(2)proxy_pass后面的路徑是一個(gè)uri時(shí),其會(huì)將location的uri替換為proxy_pass的uri;

server {

...

server_name HOSTNAME;

location /uri/ {

proxy http://host/new_uri/;

}

...

}

效果:http://HOSTNAME/uri/ --> http://host/new_uri/

(3)如果location定義其uri時(shí)使用了正則表達(dá)式的模式,則proxy_pass之后必須不能使用uri; 用戶請(qǐng)求時(shí)傳遞的uri將直接附加代理到的服務(wù)的之后;

server {

...

server_name HOSTNAME;

location ~|~* /uri/ {

proxy http://host;

}

...

}

效果:http://HOSTNAME/uri/ --> http://host/uri/;

2、proxy_set_header field value;設(shè)定發(fā)往后端主機(jī)的請(qǐng)求報(bào)文的請(qǐng)求首部的值;Context: http, server, location,常用于標(biāo)記客戶端的真是IP地址

例:

proxy_set_header X-Real-IP ?$remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

3、proxy_cache_path:定義可用于proxy功能的緩存;Context: http

proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_

4、proxy_cache zone | off;

指明要調(diào)用的緩存,或關(guān)閉緩存機(jī)制;Context: http, server, location

5、 proxy_cache_key string;

緩存中用于“鍵”的內(nèi)容;

默認(rèn)值:proxy_cache_key $scheme$proxy_host$request_uri;

6、proxy_cache_valid [code ...] time;

定義對(duì)特定響應(yīng)碼的響應(yīng)內(nèi)容的緩存時(shí)長(zhǎng);

定義在http{...}中;

proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;

定義在需要調(diào)用緩存功能的配置段,例如server{...};

proxy_cache pxycache;

proxy_cache_key $request_uri;

proxy_cache_valid 200 302 301 1h;

proxy_cache_valid any 1m;

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

Determines in which cases a stale cached response can be used when an error occurs during communication with the proxied server.

8、proxy_cache_methods GET | HEAD | POST ...;

只有標(biāo)記的相關(guān)請(qǐng)求方法才進(jìn)行緩存

9、proxy_hide_header field;

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;

Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75 seconds.

默認(rèn)為60s;

ngx_http_headers_module模塊 ?

? ? 向由代理服務(wù)器響應(yīng)給客戶端的響應(yīng)報(bào)文添加自定義首部,或修改指定首部的值; ?

? ? 1、add_header name value [always];

? ? 添加自定義首部;

? 例:

? ? add_header X-Via ?$server_addr;

? ? add_header X-Accel $server_name;

? ?

? ? 2、expires [modified] time;

? ? expires epoch | max | off;

? ? 用于定義Expire或Cache-Control首部的值;

下面來(lái)進(jìn)行操作效果的演示:

規(guī)劃:nginx反代的服務(wù)IP:10.1.249.75,后端真實(shí)服務(wù)器IP:10.1.252.235

在ngixn服務(wù)器中配置:

修改ngixn的反向代理服務(wù)中的配置文件,啟動(dòng)反向代理模式

???location?/?{#root???/usr/share/nginx/html;proxy_pass?http://10.1.252.235;index??index.html?index.htm;} [root@localhost?upload]#?ifconfig eno16777736:?flags=4163<UP,BROADCAST,RUNNING,MULTICAST>??mtu?1500inet?10.1.249.75??netmask?255.255.0.0??broadcast?10.1.255.255inet6?fe80::20c:29ff:fe65:55a6??prefixlen?64??scopeid?0x20<link>ether?00:0c:29:65:55:a6??txqueuelen?1000??(Ethernet)RX?packets?9647??bytes?1463932?(1.3?MiB)RX?errors?0??dropped?0??overruns?0??frame?0TX?packets?1736??bytes?231308?(225.8?KiB)TX?errors?0??dropped?0?overruns?0??carrier?0??collisions?0 lo:?flags=73<UP,LOOPBACK,RUNNING>??mtu?65536inet?127.0.0.1??netmask?255.0.0.0inet6?::1??prefixlen?128??scopeid?0x10<host>loop??txqueuelen?0??(Local?Loopback)RX?packets?0??bytes?0?(0.0?B)RX?errors?0??dropped?0??overruns?0??frame?0TX?packets?0??bytes?0?(0.0?B)TX?errors?0??dropped?0?overruns?0??carrier?0??collisions?0


在真實(shí)服務(wù)器中提供測(cè)試頁(yè)面:

[root@localhost?html]#?cat?index.html? RS1


在客戶端測(cè)試,已經(jīng)成功訪問只后端服務(wù)器

[root@localhost?~]#?curl?10.1.249.75 RS1 [root@localhost?~]#?curl?10.1.249.75 RS1


查看真實(shí)服務(wù)器的訪問日志,發(fā)現(xiàn)訪問的客戶段IP是代理服務(wù)器的IP:

[root@localhost?html]#?tail?/var/log/httpd/access_log? 10.1.249.75?-?-?[28/Oct/2016:20:10:46?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:46?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:46?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:47?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:47?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:47?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:49?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2"


有沒有辦法獲取真正的客戶端IP呢?答案是肯定的,nginx提供了自身的變量可以提取原始客戶的請(qǐng)求報(bào)文信息,只需要保留原客戶端的真實(shí)IP即可

修改nginx的配置文件,添加其中一條命令:proxy_set_header X-Real-IP $remote_addr;

? ?

location?/?{#root???/usr/share/nginx/html;proxy_pass?http://10.1.252.235;proxy_set_header?X-Real-IP?$remote_addr;index??index.html?index.htm;} [root@localhost?upload]#?nginx?-t nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful [root@localhost?upload]#?nginx?-s?reload

????????????再次測(cè)試,并訪問日志:

[root@localhost?html]#?tail?/var/log/httpd/access_log? 10.1.249.75?-?-?[28/Oct/2016:20:10:46?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:47?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:47?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:47?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:49?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:16:01?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:16:04?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2"


發(fā)現(xiàn)源IP還是沒有變,這是因?yàn)閔ttpd的配置文件定義了日志的格式,只需要簡(jiǎn)單修改日志格式即可,講%h改為之前nginx定義的報(bào)文首部{X-Real-IP}i

LogFormat?"%{X-Real-IP}i?%l?%u?%t?\"%r\"?%>s?%b?\"%{Referer}i\"?\"%{User-Agent}i\""?combined


再次測(cè)試,并訪問日志,已經(jīng)能正確記錄遠(yuǎn)端客戶的IP了!

[root@localhost?html]#?!tai tail?/var/log/httpd/access_log? 10.1.249.75?-?-?[28/Oct/2016:20:10:47?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:48?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:10:49?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:16:01?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.249.75?-?-?[28/Oct/2016:20:16:04?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.252.215?-?-?[28/Oct/2016:20:19:40?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.252.215?-?-?[28/Oct/2016:20:19:40?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2" 10.1.252.215?-?-?[28/Oct/2016:20:19:41?+0800]?"GET?/?HTTP/1.0"?200?4?"-"?"curl/7.19.7?(x86_64-redhat-linux-gnu)?libcurl/7.19.7?NSS/3.21?Basic?ECC?zlib/1.2.3?libidn/1.18?libssh2/1.4.2"


三、nginx的負(fù)載均衡模塊以及使用

與lvs不同的是,nginx在應(yīng)用層提供了強(qiáng)大的負(fù)載均衡的功能,并帶有后端服務(wù)的健康檢查功能。

nginx的負(fù)載均衡模塊怎么使用?

ngx_http_upstream_module模塊 (定義在http上下文)

1、upstream name { ... }

定義后端服務(wù)器組,會(huì)引入一個(gè)新的上下文;Context: http

upstream httpdsrvs {

server ...

server...

...

}

2、server address [parameters];

在upstream上下文中server成員,以及相關(guān)的參數(shù);Context: upstream

address的表示格式:

unix:/PATH/TO/SOME_SOCK_FILE

IP[:PORT]

HOSTNAME[:PORT]

parameters:

weight=number

權(quán)重,默認(rèn)為1;

max_fails=number

失敗嘗試最大次數(shù);超出此處指定的次數(shù)時(shí),server將被標(biāo)記為不可用;

fail_timeout=time

設(shè)置將服務(wù)器標(biāo)記為不可用狀態(tài)的超時(shí)時(shí)長(zhǎng);

max_conns

當(dāng)前的服務(wù)器的最大并發(fā)連接數(shù);

backup

將服務(wù)器標(biāo)記為“備用”,即所有服務(wù)器均不可用時(shí)此服務(wù)器才啟用;

down

標(biāo)記為“不可用”;

3、least_conn;

最少連接調(diào)度算法,當(dāng)server擁有不同的權(quán)重時(shí)其為wlc;

4、 ip_hash;

源地址hash調(diào)度方法;

5、hash key [consistent];

基于指定的key的hash表來(lái)實(shí)現(xiàn)對(duì)請(qǐng)求的調(diào)度,此處的key可以直接文本、變量或二者的組合;

作用:將請(qǐng)求分類,同一類請(qǐng)求將發(fā)往同一個(gè)upstream server;

示例:

hash $request_uri consistent;

hash $remote_addr;

6、keepalive connections;

為每個(gè)worker進(jìn)程保留的空閑的長(zhǎng)連接數(shù)量;

下面來(lái)實(shí)驗(yàn)驗(yàn)證:

規(guī)劃:nginx反代的服務(wù)IP:10.1.249.75,后端服務(wù)器RS1:10.1.252.235,后端服務(wù)器RS2:10.1.252.215

(1)RS1,RS2啟動(dòng)web服務(wù),并提供相關(guān)的測(cè)試頁(yè)面

(2)配置nginx的負(fù)載均衡:

在httpd段添加:

? ?

upstream?test?{server?10.1.252.235;server?10.1.252.215; }

(3)啟動(dòng)反向代理模式,并實(shí)現(xiàn)負(fù)載均衡:

? ?

location?/?{#root???/usr/share/nginx/html;proxy_pass?http://test;proxy_set_header?X-Real-IP?$remote_addr;index??index.html?index.htm;} [root@localhost?upload]#?nginx?-t nginx:?the?configuration?file?/etc/nginx/nginx.conf?syntax?is?ok nginx:?configuration?file?/etc/nginx/nginx.conf?test?is?successful [root@localhost?upload]#?nginx?-s?reload

(4)客戶端測(cè)試,已經(jīng)可以實(shí)現(xiàn)負(fù)載均衡功能(默認(rèn)是輪詢r(jià)r算法):

[root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS2 [root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS2 [root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS2 [root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS2


(5)添加權(quán)重測(cè)試:

?

??upstream?test?{server?10.1.252.235?weight=2;server?10.1.252.215; }[root@localhost?~]#?curl?http://10.1.249.75 RS2 [root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS2 [root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS2 [root@localhost?~]#?curl?http://10.1.249.75 RS1 [root@localhost?~]#?curl?http://10.1.249.75 RS1

(6)我們知道,lvs的負(fù)載均衡調(diào)度中,lvs自身并沒有對(duì)后端服務(wù)器進(jìn)行健康狀態(tài)檢測(cè)的機(jī)制,也就是說后端服務(wù)器掛了還是會(huì)照樣進(jìn)行調(diào)度,而ngixn則進(jìn)行健康狀態(tài)檢查。修改配置如下:

?

??upstream?test?{server?10.1.252.235?max_fails=3?fail_timeout=10;server?10.1.252.215?max_fails=3?fail_timeout=10; }

????????(7)停掉RS1前的測(cè)試:

[root@localhost?~]#?for?i?in?`seq?10`;do?curl?http://10.1.249.75;done? RS1 RS2 RS1 RS2 RS1 RS2 RS1 RS2 RS1 RS2

????????(8)停掉RS1測(cè)試:

[root@localhost?html]#?service?httpd?stop Stopping?httpd:????????????????????????????????????????????[??OK??] [root@localhost?html]#? [root@localhost?~]#?for?i?in?`seq?10`;do?curl?http://10.1.249.75;done? RS2 RS2 RS2 RS2 RS2 RS2 RS2 RS2 RS2 RS2

(9)已經(jīng)自動(dòng)把RS排除,現(xiàn)在再次恢復(fù)RS測(cè)試:

[root@localhost?~]#?for?i?in?`seq?10`;do?curl?http://10.1.249.75;done? RS2 RS1 RS2 RS1 RS2 RS1 RS2 RS1 RS2 RS1


????????好了nginx的反向代理和負(fù)載均衡功能就演示到這里,更多文章請(qǐng)關(guān)注?我的博客。


轉(zhuǎn)載于:https://blog.51cto.com/6638225/1866970

總結(jié)

以上是生活随笔為你收集整理的nginx的反向代理以及负载均衡模块的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。