最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

負(fù)載均衡的基本知識以及使用nginx進(jìn)行負(fù)載均衡的簡單例子

 更新時間:2018年12月29日 10:00:58   作者:liumiaocn  
今天小編就為大家分享一篇關(guān)于負(fù)載均衡的基本知識以及使用nginx進(jìn)行負(fù)載均衡的簡單例子,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

nginx一般可以用于七層的負(fù)載均衡,這篇文章將介紹一些負(fù)載均衡的基本知識以及使用nginx進(jìn)行負(fù)載均衡的簡單的例子。

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

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

常見軟件的支持

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

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

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

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

事前準(zhǔn)備

事前在7001/7002兩個端口分別啟動兩個服務(wù),用于顯示不同信息,為了演示方便,使用tornado做了一個鏡像,通過docker容器啟動時傳遞的參數(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 ~]# 

啟動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ù)載均衡演示實例:權(quán)重輪詢

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

修改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 ~]#

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • nginx 解決跨域問題嵌入第三方頁面

    nginx 解決跨域問題嵌入第三方頁面

    本文主要介紹了nginx 解決跨域問題嵌入第三方頁面,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Nginx中accept鎖的機(jī)制與實現(xiàn)詳解

    Nginx中accept鎖的機(jī)制與實現(xiàn)詳解

    這篇文章主要給大家介紹了關(guān)于Nginx中accept鎖的機(jī)制與實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • 升級nginx支持HTTP/2服務(wù)端推送的方法

    升級nginx支持HTTP/2服務(wù)端推送的方法

    這篇文章主要介紹了升級nginx支持HTTP/2服務(wù)端推送的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • nginx與apache限制ip并發(fā)訪問 限制ip連接的設(shè)置方法

    nginx與apache限制ip并發(fā)訪問 限制ip連接的設(shè)置方法

    nginx限制ip并發(fā)數(shù),也是說限制同一個ip同時連接服務(wù)器的數(shù)量,要使apache服務(wù)器做對同一IP地址的連接限制,需要mod_limitipconn來實現(xiàn)。一般需要手動編譯。不過模塊作者也提供了一些編譯好的模塊,根據(jù)自己的apache版本可以直接使用
    2012-11-11
  • Nginx實現(xiàn)自簽名SSL證書生成與配置實現(xiàn)

    Nginx實現(xiàn)自簽名SSL證書生成與配置實現(xiàn)

    本文主要介紹了Nginx實現(xiàn)自簽名SSL證書生成與配置實現(xiàn),文章將詳細(xì)介紹生成自簽名SSL證書的步驟,具有一定的參考價值,感興趣的可以了解一下
    2023-09-09
  • 超實用的Nginx常見配置合集分享

    超實用的Nginx常見配置合集分享

    這篇文章主要為大家詳細(xì)介紹了超實用的Nginx常見配置合集,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)或工作有一定的參考價值,感興趣的可以了解一下
    2022-07-07
  • Nginx和Apache幾種防盜鏈配置方法實例

    Nginx和Apache幾種防盜鏈配置方法實例

    這篇文章主要介紹了Nginx和Apache幾種防盜鏈配置方法實例,本文使用判斷來路的方法實現(xiàn)防盜鏈,分別給出Nginx和Apache配置實例,需要的朋友可以參考下
    2015-02-02
  • Nginx搭建高可用的實現(xiàn)

    Nginx搭建高可用的實現(xiàn)

    高可用HA是分布式系統(tǒng)架構(gòu)設(shè)計中必須考慮的因素之一,本文主要介紹了Nginx搭建高可用的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • Nginx 域名轉(zhuǎn)發(fā)的實現(xiàn)

    Nginx 域名轉(zhuǎn)發(fā)的實現(xiàn)

    這篇文章主要介紹了Nginx 域名轉(zhuǎn)發(fā)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • nginx搭建文件服務(wù)器的詳細(xì)過程

    nginx搭建文件服務(wù)器的詳細(xì)過程

    這篇文章主要介紹了nginx搭建文件服務(wù)器,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-06-06

最新評論

龙川县| 密云县| 敦煌市| 广宁县| 桂平市| 肇州县| 台中市| 永济市| 视频| 南木林县| 永平县| 永济市| 沂南县| 苏尼特左旗| 龙海市| 永济市| 汝城县| 尚义县| 泰州市| 仲巴县| 凌源市| 沛县| 井冈山市| 枣阳市| 彭泽县| 海南省| 南宁市| 和平区| 杂多县| 滨海县| 丰县| 南城县| 砚山县| 海原县| 天柱县| 交口县| 夏河县| 赞皇县| 清丰县| 特克斯县| 达孜县|