如何使用nginx進(jìn)行負(fù)載均衡

這篇文章主要介紹了如何使用nginx進(jìn)行負(fù)載均衡,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

麻城網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司于2013年創(chuàng)立到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。

四層負(fù)載均衡 vs 七層負(fù)載均衡

經(jīng)常會(huì)說七層負(fù)載均衡還是四層負(fù)載均衡,其實(shí)根據(jù)ISO的OSI網(wǎng)絡(luò)模型的所在層的叫法而決定的,nginx因?yàn)樵谑褂胔ttp協(xié)議在應(yīng)用層進(jìn)行負(fù)載均衡的操作,所以被稱為七層負(fù)載均衡。而諸如LVS在TCP層進(jìn)行負(fù)載均衡操作的則被稱為四層負(fù)載均衡。一般來說,有如下層的負(fù)載均衡分類:

如何使用nginx進(jìn)行負(fù)載均衡

常見軟件的支持

如何使用nginx進(jìn)行負(fù)載均衡

常見的負(fù)載均衡算法

負(fù)載均衡常見有如下幾種算法:

如何使用nginx進(jìn)行負(fù)載均衡

負(fù)載均衡演示實(shí)例:普通輪詢

接下來使用nginx來演示一下如何進(jìn)行普通輪詢:

如何使用nginx進(jìn)行負(fù)載均衡

事前準(zhǔn)備

事前在7001/7002兩個(gè)端口分別啟動(dòng)兩個(gè)服務(wù),用于顯示不同信息,為了演示方便,使用tornado做了一個(gè)鏡像,通過docker容器啟動(dòng)時(shí)傳遞的參數(shù)不同用于顯示服務(wù)的不同。

[root@kong ~]# docker run -d -p 7001:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7001"
ddba0abd24524d270a782c3fab907f6a35c0ce514eec3159357bded09022ee57
[root@kong ~]# docker run -d -p 7002:8080 liumiaocn/tornado:latest python /usr/local/bin/daemon.py "User Service 1: 7002"
95deadd795e19f675891bfcd44e5ea622c95615a95655d1fd346351eca707951
[root@kong ~]# 
[root@kong ~]# curl http://192.168.163.117:7001
Hello, Service :User Service 1: 7001
[root@kong ~]# 
[root@kong ~]# curl http://192.168.163.117:7002
Hello, Service :User Service 1: 7002
[root@kong ~]#

啟動(dòng)nginx

[root@kong ~]# docker run -p 9080:80 --name nginx-lb -d nginx 
9d53c7e9a45ef93e7848eb3f4e51c2652a49681e83bda6337c89a3cf2f379c74
[root@kong ~]# docker ps |grep nginx-lb
9d53c7e9a45e    nginx           "nginx -g 'daemon ..."  11 seconds ago   Up 10 seconds    0.0.0.0:9080->80/tcp                         nginx-lb
[root@kong ~]#

nginx代碼段

準(zhǔn)備如下nginx代碼段將其添加到nginx的/etc/nginx/conf.d/default.conf中

http {
upstream nginx_lb {
  server 192.168.163.117:7001;
  server 192.168.163.117:7002;
}
server {
  listen    80;
  server_name www.liumiao.cn 192.168.163.117;
  location / {
    proxy_pass http://nginx_lb;
  }
}

修改default.conf的方法

可以通過在容器中安裝vim達(dá)到效果,也可以在本地修改然后通過docker cp傳入,或者直接sed修改都可。如果在容器中安裝vim,使用如下方式即可

[root@kong ~]# docker exec -it nginx-lb sh
# apt-get update
...省略
# apt-get install vim
...省略

修改前

# cat default.conf
server {
  listen    80;
  server_name localhost;
  #charset koi8-r;
  #access_log /var/log/nginx/host.access.log main;
  location / {
    root  /usr/share/nginx/html;
    index index.html index.htm;
  }
  #error_page 404       /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root  /usr/share/nginx/html;
  }
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #  proxy_pass  http://127.0.0.1;
  #}
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #  root      html;
  #  fastcgi_pass  127.0.0.1:9000;
  #  fastcgi_index index.php;
  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  #  include    fastcgi_params;
  #}
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #  deny all;
  #}
}
#

修改后

# cat default.conf
upstream nginx_lb {
  server 192.168.163.117:7001;
  server 192.168.163.117:7002;
}
server {
  listen    80;
  server_name www.liumiao.cn 192.168.163.117;
  #charset koi8-r;
  #access_log /var/log/nginx/host.access.log main;
  location / {
    #root  /usr/share/nginx/html;
    #index index.html index.htm;
    proxy_pass http://nginx_lb;
  }
  #error_page 404       /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root  /usr/share/nginx/html;
  }
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #  proxy_pass  http://127.0.0.1;
  #}
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #  root      html;
  #  fastcgi_pass  127.0.0.1:9000;
  #  fastcgi_index index.php;
  #  fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  #  include    fastcgi_params;
  #}
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #  deny all;
  #}
}
#

重啟nginx容器

[root@kong ~]# docker restart nginx-lb
nginx-lb
[root@kong ~]#

確認(rèn)結(jié)果

可以清晰地看到按照順序,進(jìn)行輪詢:

[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]#

負(fù)載均衡演示實(shí)例:權(quán)重輪詢

而在此基礎(chǔ)上,進(jìn)行權(quán)重輪詢只需要加上weight即可

如何使用nginx進(jìn)行負(fù)載均衡

修改default.conf

按照如下修改default.conf

# cp default.conf default.conf.org
# vi default.conf
# diff default.conf default.conf.org
2,3c2,3
<   server 192.168.163.117:7001 weight=100;
<   server 192.168.163.117:7002 weight=200;
---
>   server 192.168.163.117:7001;
>   server 192.168.163.117:7002;
#

重啟nginx容器

[root@kong ~]# docker restart nginx-lb
nginx-lb
[root@kong ~]#

確認(rèn)結(jié)果

可以看到輪詢結(jié)果按照1/3和2/3的比重在進(jìn)行了:

[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7001
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]# curl http://localhost:9080
Hello, Service :User Service 1: 7002
[root@kong ~]#

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“如何使用nginx進(jìn)行負(fù)載均衡”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

文章題目:如何使用nginx進(jìn)行負(fù)載均衡
標(biāo)題路徑:http://muchs.cn/article24/pdpdje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)外貿(mào)建站、網(wǎng)站內(nèi)鏈、標(biāo)簽優(yōu)化網(wǎng)站維護(hù)、建站公司

廣告

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

成都定制網(wǎng)站建設(shè)