溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置

發(fā)布時(shí)間:2021-12-07 14:54:04 來源:億速云 閱讀:146 作者:小新 欄目:云計(jì)算

這篇文章主要介紹LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!


1、配置環(huán)境介紹

系統(tǒng)環(huán)境:

[root@centos6 conf]# cat /etc/redhat-release 

CentOS release 6.5 (Final)

[root@centos6 conf]# uname -r

2.6.32-431.el6.x86_64

nginx版本:

[root@centos6 conf]# /application/nginx/sbin/nginx -v

nginx version: nginx/1.10.1

LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置

LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置

配置二臺(tái)虛擬主機(jī),用來做后續(xù)測(cè)試



2、整體邏輯圖

LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置


說明:當(dāng)用戶訪問時(shí),其實(shí)訪問的是負(fù)載均衡器對(duì)外提供的地地址,然后由它來根據(jù)相應(yīng)的規(guī)則進(jìn)行轉(zhuǎn)發(fā)給后端后服務(wù)器


3、配置過程

[root@centos6 conf]# vi nginx.conf

worker_processes  1;    

events {                          

    worker_connections  1024;    

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65; 

include /application/nginx/conf/extra/upstream01.conf;

  }

增加上述配置即可


接下來配置upstream01.conf


[root@centos6 extra]# vi upstream01.conf

#####config to nginx 

upstream test_servers {      #定義主機(jī)池

server 172.16.1.235:8081 weight=5;  #按權(quán)重的方式進(jìn)行輪詢

server 172.16.1.235:8080 weight=5; 

server 172.16.1.235:80 weight=15;

      }

server { 

listen 80; 

server_name www.mingonge.com;

location / { 

 proxy_pass http://test_servers;     #將監(jiān)聽到請(qǐng)求轉(zhuǎn)發(fā)到這個(gè)虛擬主機(jī)池

}  

 }


更多關(guān)于upstream模塊的介紹,請(qǐng)參考官方文檔

http://nginx.org/en/docs/http/ngx_http_upstream_module.html


4、重啟服務(wù)并測(cè)試

重啟nginx服務(wù)

[root@centos6 extra]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.10.1/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.10.1/conf/nginx.conf test is successful

[root@centos6 extra]# /application/nginx/sbin/nginx -s reload

linux本地客戶端測(cè)試

[root@centos6 extra]# curl http://172.16.1.235

welcont to mingongge's blog stie

[root@centos6 extra]# curl http://172.16.1.235

welcont to mingongge's bbs stie

[root@centos6 extra]# curl http://172.16.1.235

welcont to mingongge's blog stie

用戶客戶端用域名測(cè)試

本地客戶端需要將域名正確解析,www.mingongge.com------>172.16.1.235

LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置

LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置


從上面的測(cè)試結(jié)果來看,的確兩次訪問分配的服務(wù)器是不同的,為了測(cè)試效果,所以將顯示的內(nèi)容配置成不同,實(shí)際生產(chǎn)環(huán)境中,所有的訪問顯示內(nèi)容都是一樣的,實(shí)現(xiàn)服務(wù)器宕機(jī)但不會(huì)影響用戶的體驗(yàn)度



5、模擬測(cè)試真實(shí)環(huán)境

我們這里將兩臺(tái)虛擬機(jī)首頁內(nèi)容配置成相同顯示內(nèi)容來模擬真實(shí)生產(chǎn)環(huán)境

[root@centos6 ~]# echo "welcome to mingongge's web site" >/www/bbs/index.html

[root@centos6 ~]#  echo "welcome to mingongge's web site" >/www/blog/index.html

[root@centos6 ~]# cat /www/bbs/index.html 

welcome to mingongge's web site

[root@centos6 ~]# cat /www/blog/index.html 

welcome to mingongge's web site

linux客戶端測(cè)試

[root@centos6 ~]# curl http://172.16.1.235

welcome to mingongge's web site

[root@centos6 ~]# curl http://172.16.1.235

welcome to mingongge's web site


停止其中一臺(tái)虛擬的WEB服務(wù)功能來模擬故障,由于是用的nginx本身的虛擬主機(jī),這里我們就修改配置文件,將包含配置文件注釋掉

#include  /application/nginx/conf/extra/vhosts/bbs.conf;

如果測(cè)試用三臺(tái)服務(wù)器,可以配置不同的http服務(wù),模擬服務(wù)器宕機(jī)(停止WEB服務(wù)),來測(cè)試負(fù)載均衡的效果更加貼近現(xiàn)實(shí)環(huán)境


[root@centos6 ~]# curl http://www.mingongge.com        

welcome to mingongge's web site

[root@centos6 ~]# curl http://www.mingongge.com

welcome to mingongge's web site

LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置
客戶端仍然可以訪問 ,表明負(fù)載均衡的功能是生效的,當(dāng)其中的服務(wù)器出現(xiàn)宕機(jī)情況,也不會(huì)影響用戶的最終訪問

以上是“LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI