利用nginx實現動靜分離的負載均衡集群實戰教程
目錄
- 前言
- 一. 實驗環境
- 1.1 系統及服務
- 1.2 本次要實現的架構圖
- 二. nginx負載均衡詳解
- 2.1 什么是負載均衡?
- 2.2 負載均衡的5中方式
- 三. 安裝nginx作為流量分發器
- 3.1 安裝nginx前準備 安裝依賴工具
- 3.2 開始編譯
- 3.3 開始編譯安裝nginx
- 3.4 生成運行的nginx用戶
- 3.5 啟動nginx并測試
- 四. 配置nginx成為分發器
- 4.1 先備份配置文件
- 4.2 把nginx設置成分發器,實現動靜分離
- 五. 配置兩臺http服務器
- 5.1 配置mufeng42服務器 配置web服務器:
- 5.2 配置mufeng43服務器
- 5.3 測試
- 總結
前言
大家好,我是沐風曉月,今天我們利用nginx來作為負載,實現兩臺apache服務器的動靜分離集群實戰;
一. 實驗環境
1.1 系統及服務
本次用到的操作系統及服務:
本次實驗一共需要3臺服務器,一臺nginx做為負載均衡分發器和動靜分離的分發器,兩臺apache做為后端服務器,使用nginx實現兩臺apache服務器的負載均衡和動靜分離。
操作系統: centos7.6
nginx 版本: 1.22 版本
apache版本: 系統默認自帶的2.4.6
php版本: 系統默認自帶的 5.4.16
apache和php版本,都可以升級為最新版本,可以從官網下載安裝。
1.2 本次要實現的架構圖
一般我們的服務器分很多種,有文件服務器,圖片服務器,數據庫服務器。
還有各種不同的服務:
- 靜態文件處理:可以使用nginx 或apache
- 動文件處理: apache ,tomcat
- 圖片文件處理: squid
本文中我們使用nginx實現動靜分離的負載均衡集群。
二. nginx負載均衡詳解
2.1 什么是負載均衡?
服務器的負載均衡是指將來自客戶端的請求分攤到多臺服務器上,以達到提高系統性能、增加系統可靠性、避免單點故障等目的的技術。
通過負載均衡,可以使得多臺服務器共同處理客戶端的請求,從而提高系統的整體性能和可用性。
在負載均衡中,通常會把多臺服務器組成一個服務器集群,客戶端向負載均衡器發送請求,負載均衡器會根據一定的算法將請求分配到服務器集群中的一臺或多臺服務器上進行處理。負載均衡的算法有很多種,常見的有輪詢、隨機、最小連接數等。
負載均衡還可以通過一些高級功能來實現更加復雜的負載均衡策略,例如會話保持、健康檢查、動態調整權重等。這些功能可以根據實際需求進行配置和調整,使得負載均衡系統更加靈活和高效。
2.2 負載均衡的5中方式
Nginx 的 upstream 負載的5種方式,目前最常用 前3 種方式:
1) 輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器 down 掉,能自動剔除。
2) weight
指定輪詢幾率,weight 和訪問比率成正比,用于后端服務器性能不均的情況。
3) ip_hash
每個請求按訪問 ip 的 hash 結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決 session 的問題。
4) air(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。
5) url_hash(第三方)
按訪問url的hash結果來分配請求,使同樣的url定向到同一個后端服務器,后端服務器為緩存時比較有效
三. 安裝nginx作為流量分發器
3.1 安裝nginx前準備 安裝依賴工具
[root@mufeng41 ~]# yum -y install gcc gcc-c++ autoconf automake[root@mufeng41 ~]# yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
沐風曉月在做這一步安裝的時候,忘了掛載鏡像,浪費了不少時間,所以要提前掛載,配置好yum源哦。
上傳nginx壓縮包,進行解壓
[root@mufeng41 ~]# ll nginx-1.12.2.tar.gz -rw-r--r--. 1 root root 981687 8月 27 2019 nginx-1.12.2.tar.gz[root@mufeng41 ~]# tar xf nginx-1.12.2.tar.gz -C /usr/local/src/
登錄并查看
root@mufeng41 ~]# cd !$cd /usr/local/src/[root@mufeng41 src]# lsnginx-1.12.2[root@mufeng41 src]# cd nginx-1.12.2/[root@mufeng41 nginx-1.12.2]# lsauto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src[root@mufeng41 nginx-1.12.2]#
3.2 開始編譯
./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module
對參數的解釋:
- –with-http_dav_module 啟用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL:創建集合,COPY和MOVE方法)默認情況下為關閉,需編譯開啟
- –with-http_stub_status_module 啟用ngx_http_stub_status_module支持(獲取nginx自上次啟動以來的工作狀態)
- –with-http_addition_module 啟用ngx_http_addition_module支持(作為一個輸出過濾器,支持不完全緩沖,分部分響應請求)
- –with-http_sub_module 啟用ngx_http_sub_module支持(允許用一些其他文本替換nginx響應中的一些文本)
- –with-http_flv_module 啟用ngx_http_flv_module支持(提供尋求內存使用基于時間的偏移量文件)
- –with-http_mp4_module 啟用對mp4文件支持(提供尋求內存使用基于時間的偏移量文件)
3.3 開始編譯安裝nginx
使用make && make install
進行安裝
[root@mufeng41 nginx-1.12.2]# make && make install
如何判斷是否執行成功?
答: echo $?
3.4 生成運行的nginx用戶
[root@mufeng41 nginx-1.12.2]# useradd -u 8000 -s /sbin/nologin nginx[root@mufeng41 nginx-1.12.2]# id nginxuid=8000(nginx) gid=8000(nginx) 組=8000(nginx)[root@mufeng41 nginx-1.12.2]#
3.5 啟動nginx并測試
如果你不知道nginx配置文件和啟動腳本在哪,可以搜一下,使用find / -name nginx.conf
.
啟動服務
[root@itlaoxin163 ~]# find / -name nginx.conf/usr/local/nginx/conf/nginx.conf# 啟動[root@mufeng41 nginx-1.12.2]# /usr/local/nginx/sbin/nginx [root@mufeng41 nginx-1.12.2]# netstat -antup |grep 80tcp0 0 0.0.0.0:80 0.0.0.0:* LISTEN 25286/nginx: master udp0 0 0.0.0.0:58076 0.0.0.0:*
查看效果
[root@mufeng41 nginx-1.12.2]# systemctl stop firewalld.service[root@mufeng41 nginx-1.12.2]# curl -I 127.0.0.1HTTP/1.1 200 OKServer: nginx/1.12.2Date: Fri, 24 Mar 2023 11:06:29 GMTContent-Type: text/htmlContent-Length: 612Last-Modified: Fri, 24 Mar 2023 11:01:53 GMTConnection: keep-aliveETag: "641d8321-264"Accept-Ranges: byte
四. 配置nginx成為分發器
4.1 先備份配置文件
[root@mufeng41 conf]# pwd/usr/local/nginx/conf[root@mufeng41 conf]# cp nginx.conf nginx.conf.bak[root@mufeng41 conf]#
4.2 把nginx設置成分發器,實現動靜分離
配置如下圖:
配置分發器
location / { root html; index index.html index.htm;if ($request_uri ~* \.html$){ proxy_pass http://htmlservers; }if ($request_uri ~* \.php$){ proxy_pass http://phpservers; } proxy_pass http://picservers;}
注釋:
location 的作用是根據請求的 URI,將請求轉發到不同的后端服務器上進行處理。具體解釋如下:
- location /:表示所有請求(URI)都會被這個 location 塊所匹配。
- root html:表示當訪問的 URI對應的文件不存在時,會在 nginx 安裝目錄下的 html 目錄中查找對應的文件。
- index index.html
- index.htm:表示當訪問的 URI 對應的目錄中沒有指定的默認文件時,會嘗試訪問 index.html 或 index.htm 文件。
- if ($request_uri ~* .html$):表示如果請求的 URI 包含 .html,則執行下面的語句。
- proxy_pass http://htmlservers:表示將請求轉發到名為 htmlservers 的后端服務器處理。
- if ($request_uri ~* .php$):表示如果請求的 URI 包含 .php,則執行下面的語句。
- proxy_pass http://phpservers:表示將請求轉發到名為 phpservers 的后端服務器處理。
- proxy_pass http://picservers:表示將請求轉發到名為 picservers 的后端服務器處理,這個語句沒有條件限制,如果以上兩個if 語句都不匹配,則會執行這個語句。
接下來設置負載均衡對應的IP
定義負載均衡設備的IP
在nginx配置文件最后一行}前添加一下內容:
代碼如下:
upstream htmlservers { server 192.168.1.42:80; server 192.168.1.43:80; } upstream phpservers{ server 192.168.1.42:80; server 192.168.1.43:80; } upstream picservers { server 192.168.1.42:80; server 192.168.1.43:80; }
配置文件是否有錯誤
[root@mufeng41 conf]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
重啟nginx
[root@mufeng41 conf]# /usr/local/nginx/sbin/nginx -s reload
五. 配置兩臺http服務器
接下來,需要在 mufeng42和mufeng43上操作
5.1 配置mufeng42服務器 配置web服務器:
[root@mufeng42 ~]# yum install httpd php -y
生成靜態測試文件
[root@mufeng42 ~]# echo 192.168.1.42 > /var/www/html/index.html
在創建一個php文件:
[root@itlaoxin162 ~]# vim /var/www/html/test.php
寫入內容:
echo "我是42服務器";echo "我是沐風曉月"<?phpphpinfo();?>
啟動apache
[root@mufeng42 ~]# systemctl restart httpd
5.2 配置mufeng43服務器
安裝http并生成靜態文件
[root@mufeng43 ~]# yum install httpd php -y[root@mufeng43 ~]# echo 192.168.1.43 > /var/www/html/index.html
建立php文件
[root@mufeng43 ~]# cd /var/www/html/[root@mufeng43 html]# vi mufeng.php[root@mufeng43 html]# cat mufeng.php echo "我是43服務器";<?phpphpinfo();?>
啟動配置文件
[root@mufeng43 html]# systemctl restart httpd
5.3 測試
到目前為止,nginx負載均衡就結束了,接下來就可以測試了:
測試靜態頁面
瀏覽器輸入: http://192.168.1.41/ 進行測試
測試轉發動態頁面:
瀏覽器輸入 http://192.168.1.41/test.php
總結
到此這篇關于利用nginx實現動靜分離的負載均衡集群實戰的文章就介紹到這了,更多相關nginx負載均衡集群內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!
