首頁 運維 Nginx 怎麼使用nginx進行負載平衡

怎麼使用nginx進行負載平衡

Jun 03, 2023 am 08:19 AM
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: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程式碼片段

準備如下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.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進行負載平衡

重啟nginx容器

[root@kong ~]# docker restart nginx-lb
nginx-lb
[root@kong ~]#
登入後複製
登入後複製
確認結果

#可以清楚地看到依照順序,進行輪詢:

[root@kong ~]# curl

hello, service :user service 1: 7001
[root@kong ~]# curl

hello, service :user service 1: 7002
[root@kong ~]# curl
hello, service :user service 1: 7001
[root@kong ~]# curl
hello, service :user service 1: 7002
[root@kong ~]

####負載平衡示範實例:權重輪詢#########而在此基礎上,進行權重輪詢只需加上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 ~]#
登入後複製
登入後複製
######確認結果#########可以看到輪詢結果依照1/3和2/3的比重在進行了:#########[root @kong ~]# curl###hello, service :user service 1: 7001####[root@kong ~]# curl###hello, service :user service 1: 7002###[root@kong ~] # curl###hello, service :user service 1: 7002###[root@kong ~]########

以上是怎麼使用nginx進行負載平衡的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

docker容器名稱怎麼查 docker容器名稱怎麼查 Apr 15, 2025 pm 12:21 PM

可以通過以下步驟查詢 Docker 容器名稱:列出所有容器(docker ps)。篩選容器列表(使用 grep 命令)。獲取容器名稱(位於 "NAMES" 列中)。

nginx怎麼配置雲服務器域名 nginx怎麼配置雲服務器域名 Apr 14, 2025 pm 12:18 PM

在雲服務器上配置 Nginx 域名的方法:創建 A 記錄,指向雲服務器的公共 IP 地址。在 Nginx 配置文件中添加虛擬主機塊,指定偵聽端口、域名和網站根目錄。重啟 Nginx 以應用更改。訪問域名測試配置。其他注意事項:安裝 SSL 證書啟用 HTTPS、確保防火牆允許 80 端口流量、等待 DNS 解析生效。

nginx怎麼查版本 nginx怎麼查版本 Apr 14, 2025 am 11:57 AM

可以查詢 Nginx 版本的方法有:使用 nginx -v 命令;查看 nginx.conf 文件中的 version 指令;打開 Nginx 錯誤頁,查看頁面的標題。

nginx在windows中怎麼配置 nginx在windows中怎麼配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

怎麼啟動nginx服務器 怎麼啟動nginx服務器 Apr 14, 2025 pm 12:27 PM

啟動 Nginx 服務器需要按照不同操作系統採取不同的步驟:Linux/Unix 系統:安裝 Nginx 軟件包(例如使用 apt-get 或 yum)。使用 systemctl 啟動 Nginx 服務(例如 sudo systemctl start nginx)。 Windows 系統:下載並安裝 Windows 二進製文件。使用 nginx.exe 可執行文件啟動 Nginx(例如 nginx.exe -c conf\nginx.conf)。無論使用哪種操作系統,您都可以通過訪問服務器 IP

docker怎麼創建容器 docker怎麼創建容器 Apr 15, 2025 pm 12:18 PM

在 Docker 中創建容器: 1. 拉取鏡像: docker pull [鏡像名] 2. 創建容器: docker run [選項] [鏡像名] [命令] 3. 啟動容器: docker start [容器名]

怎麼查看nginx是否啟動 怎麼查看nginx是否啟動 Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

docker怎麼啟動容器 docker怎麼啟動容器 Apr 15, 2025 pm 12:27 PM

Docker 容器啟動步驟:拉取容器鏡像:運行 "docker pull [鏡像名稱]"。創建容器:使用 "docker create [選項] [鏡像名稱] [命令和參數]"。啟動容器:執行 "docker start [容器名稱或 ID]"。檢查容器狀態:通過 "docker ps" 驗證容器是否正在運行。

See all articles