麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 網站 > Nginx > 正文

負載均衡的基本知識以及使用nginx進行負載均衡的簡單例子

2024-08-30 12:30:08
字體:
來源:轉載
供稿:網友

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

四層負載均衡 vs 七層負載均衡

經常會說七層負載均衡還是四層負載均衡,其實根據ISO的OSI網絡模型的所在層的叫法而決定的,nginx因為在使用http協議在應用層進行負載均衡的操作,所以被稱為七層負載均衡。而諸如LVS在TCP層進行負載均衡操作的則被稱為四層負載均衡。一般來說,有如下層的負載均衡分類:

負載均衡,nginx

常見軟件的支持

負載均衡,nginx

常見的負載均衡算法

負載均衡常見有如下幾種算法:

負載均衡,nginx

負載均衡演示實例:普通輪詢

接下來使用nginx來演示一下如何進行普通輪詢:

負載均衡,nginx

事前準備

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

[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:7001Hello, Service :User Service 1: 7001[root@kong ~]# [root@kong ~]# curl http://192.168.163.117:7002Hello, 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-lb9d53c7e9a45e    nginx           "nginx -g 'daemon ..."  11 seconds ago   Up 10 seconds    0.0.0.0:9080->80/tcp                         nginx-lb[root@kong ~]#

nginx代碼段

準備如下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達到效果,也可以在本地修改然后通過docker cp傳入,或者直接sed修改都可。如果在容器中安裝vim,使用如下方式即可

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

修改前

# cat default.confserver {  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.confupstream 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-lbnginx-lb[root@kong ~]#

確認結果

可以清晰地看到按照順序,進行輪詢:

[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 ~]#

負載均衡演示實例:權重輪詢

而在此基礎上,進行權重輪詢只需要加上weight即可

負載均衡,nginx

修改default.conf

按照如下修改default.conf

# cp default.conf default.conf.org# vi default.conf# diff default.conf default.conf.org2,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-lbnginx-lb[root@kong ~]#

確認結果

可以看到輪詢結果按照1/3和2/3的比重在進行了:

[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 ~]#

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到服務器教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 日韩精品免费一区二区三区 | www.99热视频 | 国产精品高潮视频 | 狠狠干天天操 | 一级一级一级一级毛片 | 羞羞视频一区 | 黄色特级片黄色特级片 | 国产一区二区在线观看视频 | 久久久久久久久久综合 | 久久看免费视频 | 可以看逼的视频 | 成人短视频在线播放 | 久久久久久久久日本理论电影 | 56av国产精品久久久久久久 | 日产精品久久久一区二区开放时间 | 日韩字幕 | 免费国产在线视频 | xxx日本视频| 免费毛片免费看 | 久久国产亚洲视频 | 欧美一级性 | 久久另类视频 | 国产视频在线免费观看 | 黄色试看视频 | 在线观看中文字幕av | 毛片成人网 | 播色网| 69性欧美高清影院 | 一级外国毛片 | 钻石午夜影院 | 免费黄色在线观看网站 | 日产精品久久久一区二区开放时间 | 久久蜜桃香蕉精品一区二区三区 | 超碰97人 | 一级黄色欧美 | 国产剧情在线观看一区二区 | 九九热精品在线 | 一及毛片视频 | 黄色的视频在线观看 | 91精品国产成人 | 717影院理论午夜伦八戒秦先生 |