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

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

創(chuàng)新互聯(lián)建站于2013年開始,先為太和等服務(wù)建站,太和等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為太和企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

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



2、整體邏輯圖

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


說明:當(dāng)用戶訪問時(shí),其實(shí)訪問的是負(fù)載均衡器對外提供的地地址,然后由它來根據(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)聽到請求轉(zhuǎn)發(fā)到這個(gè)虛擬主機(jī)池

}  

 }


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

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


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

重啟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本地客戶端測試

[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

用戶客戶端用域名測試

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

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

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


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



5、模擬測試真實(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客戶端測試

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

如果測試用三臺(tái)服務(wù)器,可以配置不同的http服務(wù),模擬服務(wù)器宕機(jī)(停止WEB服務(wù)),來測試負(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)容對大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)頁標(biāo)題:LNMP架構(gòu)中Nginx反向代理負(fù)載均衡如何配置
本文地址:http://muchs.cn/article30/gcecpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣網(wǎng)站營銷、網(wǎng)站維護(hù)、網(wǎng)站制作App設(shè)計(jì)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化