- 源碼包安裝NGINX A,搭建Web Server,任意HTML頁(yè)面,其8080端口提供Web訪(fǎng)問(wèn)服務(wù),截圖成功訪(fǎng)問(wèn)http(s)://[Server1]:8080并且回顯Web頁(yè)面。
- 保留Server1,但是不允許直接訪(fǎng)問(wèn)Server 1,再部署1套NGINX B,利用其實(shí)現(xiàn)反向代理功能,要求有且僅有訪(fǎng)問(wèn)http://elite.chaitin.com 可以訪(fǎng)問(wèn)到Web Server1的8080服務(wù)。(截圖并詳細(xì)說(shuō)明NGINX上配置/參數(shù))
- (加強(qiáng)提升題目)假設(shè)再加1套Web Server 2,其8080端口提供與Server 1相同服務(wù),如何配置NGINX A實(shí)現(xiàn)訪(fǎng)問(wèn)http://elite.chaitin.com 的請(qǐng)求可以被負(fù)載分發(fā)到Server1或者Server2?
- 在上文題目2基礎(chǔ)之上,增加節(jié)點(diǎn)NGINX B,如何配置實(shí)現(xiàn)高可用?demo演示當(dāng)NGINX A故障時(shí),流量自動(dòng)切換到NGINX B(客戶(hù)端訪(fǎng)問(wèn)幾乎無(wú)感知)。詳細(xì)說(shuō)明VRRP協(xié)議在本題所述場(chǎng)景下的應(yīng)用。
- (加強(qiáng)提升題目)在上文題目3基礎(chǔ)之上,配置和驗(yàn)證NGINX支持哪些負(fù)載均衡算法及其效果?并結(jié)合NGINX配置參數(shù)詳細(xì)說(shuō)明不同負(fù)載均衡算法工作原理。
- (加強(qiáng)提升題目)在上文題目3基礎(chǔ)之上,配置和驗(yàn)證主動(dòng)和被動(dòng)兩種健康檢查方式及其效果?并結(jié)合NGINX配置參數(shù)詳細(xì)說(shuō)明不同健康檢查方式區(qū)別和工作原理。
實(shí)驗(yàn)環(huán)境:
Ubuntu 20.04 64bit
實(shí)驗(yàn)前關(guān)閉防火墻和selinux
server1 | 8.140.24.100 |
---|---|
server2 | 8.140.240.19 |
要求1 nginx的部署
要求1. 源碼包安裝NGINX A,搭建Web Server,任意HTML頁(yè)面,其18080端口提供Web訪(fǎng)問(wèn)服務(wù),截圖成功訪(fǎng)問(wèn)http(s)://[Server1]:18080并且回顯Web頁(yè)面。
注:因?yàn)榉?wù)器的8080端口沒(méi)有開(kāi)放到外網(wǎng)訪(fǎng)問(wèn),只開(kāi)放了18080-18086的端口到外網(wǎng);所以我將題目中的8080端口改成了18080端口來(lái)做實(shí)驗(yàn)(端口的改變不會(huì)影響實(shí)驗(yàn)的效果)
獲取nginx源碼包
wget https://nginx.org/download/nginx-1.22.1.tar.gz
安裝源碼編譯環(huán)境
yum install gcc gcc-c++ pcre pcre-devel openssl openssl-devel -y
將此目錄設(shè)置為nginx源碼存放目錄
/usr/local/src
將源碼包移動(dòng)到該目錄下
[root@centos ~]# mv nginx-1.22.1.tar.gz /usr/local/src/
將源碼包文件解壓縮
[root@centos ~]# cd /usr/local/src/
[root@centos src]# tar xf nginx-1.22.1.tar.gz
編譯參數(shù)詳解
[root@centos nginx-1.22.1]# ./configure --help
--prefix= 指向安裝目錄
--sbin-path 指向(執(zhí)行)程序文件(nginx)
--conf-path= 指向配置文件(nginx.conf)
--pid-path= 指向 pid 文件( nginx.pid )--lock-path= 指向 lock 文件( nginx.lock)(安裝文件鎖定,防止安裝文件被別人利用
或自己誤操作。)
--user= 指定程序運(yùn)行時(shí)的非特權(quán)用戶(hù)
--group= 指定程序運(yùn)行時(shí)的非特權(quán)用戶(hù)組
--builddir= 指向編譯目錄
--with-rtsig_module 啟用rtsig 模塊支持(實(shí)時(shí)信號(hào))--with-select_module 啟用 select 模塊支持(一種輪詢(xún)模式,不推薦在高載環(huán)境下使用)
禁用:--without-select _module
--with-poll _module 啟用 poll 模塊支持(功能與select相同,與select特性相同,為
種輪詢(xún)模式,不推薦在高載環(huán)境下使用)
--with-file-aio 啟用file aio 支持(一種 APL文件傳輸格式
--with-ipv6 啟用ipv6 支持
--with-file-aio 啟用file aio 支持(一種 APL文件傳輸格式--with-ipv6 啟用ipv6支持
--with-http_ssl_module 啟用 ngx http_ssl module 支持(使支持https 請(qǐng)求,需已安裝
openssl)
--with-http realip_module 啟用 ngx http_realip_module 支持 這個(gè)模塊允許從請(qǐng)求標(biāo)
頭更改客戶(hù)端的IP 地址值,默認(rèn)為關(guān) )
--with-http addition module 啟用 ngx http addition module 支持(作為一個(gè)輸出過(guò)濾器,支持不完全緩沖,分部分響應(yīng)請(qǐng)求 )
--with-http xslt module 啟用 ngx http_xslt _module 支持(過(guò)濾轉(zhuǎn)換 XML請(qǐng)求)
--with-http image filter module 啟用 ngx http image filter module 支持 ( 傳輸JPEG/GIF/PNG 圖片的一個(gè)過(guò)濾器)(默認(rèn)為不啟用。gd 庫(kù)要用到) --with-http_geoip_module 啟用 ngx_http_geoip_module 支持(該模塊創(chuàng)建基于與 MaxMind GeoIP 二進(jìn)制文件相配的客戶(hù)端 IP 地址的 ngx_http_geoip_module 變量) --with-http_sub_module 啟用 ngx_http_sub_module 支持(允許用一些其他文本替換 nginx 響應(yīng)中的一些文本)
--with-http_dav_module 啟用ngx_http_dav_module支持(增加PUT,DELETE,MKCOL: 創(chuàng)建集合,COPY 和 MOVE 方法)默認(rèn)情況下為關(guān)閉,需編譯開(kāi)啟
--with-http_flv_module 啟用 ngx_http_flv_module 支持(提供尋求內(nèi)存使用基于時(shí)間的 偏移量文件)
--with-http_gzip_static_module 啟用 ngx_http_gzip_static_module 支持(在線(xiàn)實(shí)時(shí)壓 縮輸出數(shù)據(jù)流)
--with-http_random_index_module 啟用 ngx_http_random_index_module 支持(從 目錄中隨機(jī)挑選一個(gè)目錄索引)
--with-http_secure_link_module 啟用 ngx_http_secure_link_module 支持(計(jì)算和檢 查要求所需的安全鏈接網(wǎng)址)
--with-http_degradation_module 啟用 ngx_http_degradation_module 支持(允許在 內(nèi)存不足的情況下返回 204 或 444 碼)
--with-http_stub_status_module 啟用ngx_http_stub_status_module支持(獲取nginx 自上次啟動(dòng)以來(lái)的工作狀態(tài))
--without-http_auth_basic_module 禁用 ngx_http_auth_basic_module(該模塊是可以 使用用戶(hù)名和密碼基于 http 基本認(rèn)證方法來(lái)保護(hù)你的站點(diǎn)或其部分內(nèi)容) --without-http_autoindex_module 禁用 disable ngx_http_autoindex_module 支持 (該模塊用于自動(dòng)生成目錄列表,只在 ngx_http_index_module 模塊未找到索引文件時(shí)發(fā) 出請(qǐng)求。)
--without-http_geo_module 禁用 ngx_http_geo_module 支持(創(chuàng)建一些變量,其值依 賴(lài)于客戶(hù)端的 IP 地址)
--without-http_map_module 禁用 ngx_http_map_module 支持(使用任意的鍵/值對(duì)設(shè) 置配置變量)
--without-http_split_clients_module 禁用 ngx_http_split_clients_module 支持(該模 塊用來(lái)基于某些條件劃分用戶(hù)。條件如:ip 地址、報(bào)頭、cookies 等等) --without-http_referer_module 禁用 disable ngx_http_referer_module 支持(該模塊 用來(lái)過(guò)濾請(qǐng)求,拒絕報(bào)頭中 Referer 值不正確的請(qǐng)求)
--http-proxy-temp-path= 設(shè)定 http 代理臨時(shí)文件路徑
--http-fastcgi-temp-path= 設(shè)定 http fastcgi 臨時(shí)文件路徑 --http-uwsgi-temp-path= 設(shè)定 http uwsgi 臨時(shí)文件路徑
--http-scgi-temp-path= 設(shè)定 http scgi 臨時(shí)文件路徑
-without-http 禁用 http server 功能
--without-http-cache 禁用 http cache 功能
--with-mail 啟用 POP3/IMAP4/SMTP 代理模塊支持
--with-mail_ssl_module 啟用 ngx_mail_ssl_module 支持
通用配置選項(xiàng)
--prefix=<path> 指定 Nginx 的安裝路徑,所有其他的路徑都要依賴(lài)于該選項(xiàng)
--sbin-path=<path> 指定 Nginx 二進(jìn)制文件的路徑。如果沒(méi)有指定,那么這 個(gè)路徑將依賴(lài)于--prefix 選項(xiàng)
--conf-path=<path> 指定 Nginx 的配置文件的路徑,如果在命令行沒(méi)有指定 配置文件,那么將會(huì)通過(guò)這里指定路徑。
--error-log-path=<path> 文件,除非有其它的配置。
--pid-path=<path> /var/run 下
--lock-path=<path> 共享存儲(chǔ)器互斥鎖文件的路徑
--user=<user> worker 進(jìn)程運(yùn)行的用戶(hù)
--group=<group> worker 進(jìn)程運(yùn)行的組
--with-file-aio. 為 FreeBSD4.3 +和 linux 2.6.22 +系統(tǒng)啟用異步 I/O
--with-debug 這個(gè)選項(xiàng)用于啟用調(diào)試日志,在生產(chǎn)環(huán)境的系統(tǒng)中不推薦使用
檢查安裝了nginx后是否有這個(gè)用戶(hù)
[root@centos nginx-1.22.1]# id nginx
uid=997(nginx) gid=995(nginx) 組=995(nginx)
源碼編譯安裝nginx
[root@centos nginx-1.22.1]# ./configure \
> --user=nginx \
> --group=nginx \
> --prefix=/usr/local/nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module
[root@centos nginx-1.22.1]# make && make install
給nginx寫(xiě)啟動(dòng)腳本
[root@centos sbin]# pwd
/usr/local/nginx/sbin
[root@centos sbin]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
啟動(dòng)報(bào)錯(cuò)及原因排查
在這個(gè)報(bào)錯(cuò)信息里面發(fā)現(xiàn)80端口被占用,所以我修改了nginx默認(rèn)的配置文件,將監(jiān)聽(tīng)端口設(shè)置為81
location匹配優(yōu)先級(jí)
1.精確匹配 (=)
2.字符串匹配,不做正則匹配檢查(^~)
3.正則匹配
4.字符串匹配,如果有正則優(yōu)先正則
5.所有都匹配不到,默認(rèn)匹配 /
加載并啟動(dòng)配置文件
[root@centos sbin]# systemctl daemon-reload #加載配置文件
[root@centos sbin]# systemctl start nginx #啟動(dòng)nginx
查看服務(wù)器IP:
根據(jù)題目一的要求,將配置文件改為監(jiān)聽(tīng)18080端口,訪(fǎng)問(wèn)頁(yè)面的路徑改為/var/www/html
server {
listen 18080; #監(jiān)聽(tīng)18080端口
server_name elite.chaitin.com; #指定服務(wù)器的域名
location / { #/表示匹配任意請(qǐng)求路徑
root /var/www/html; #指定響應(yīng)目錄
index index.html index.htm; #訪(fǎng)問(wèn)該目錄下的 index.html 或 index.htm 文件
服務(wù)器會(huì)首先查找 index.html 文件,如果找不到則查找 index.htm 文件。
}
寫(xiě)一個(gè)訪(fǎng)問(wèn)頁(yè)面并重啟nginx服務(wù)
root@iZ2zei3ltzorcuiiynrseqZ:~# echo 'Welcome to Chaitin!' > /var/www/html/index.html
root@iZ2zei3ltzorcuiiynrseqZ:~# systemctl daemon-reload
root@iZ2zei3ltzorcuiiynrseqZ:~# systemctl restart nginx
訪(fǎng)問(wèn)成功!
要求2 nginx反向代理
要求2. 保留Server1,但是不允許直接訪(fǎng)問(wèn)Server 1,再部署1套NGINX B,利用其實(shí)現(xiàn)反向代理功能,要求有且僅有訪(fǎng)問(wèn)http://elite.chaitin.com 可以訪(fǎng)問(wèn)到Web Server1的8080服務(wù)。(截圖并詳細(xì)說(shuō)明NGINX上配置/參數(shù))
步驟一:
另開(kāi)一臺(tái)server2 服務(wù)器 8.140.240.19,直接安裝nginx
root@iZ2zei3ltzorcuiiynrsemZ~# apt-get install nginx
將原配置文件備份,避免錯(cuò)誤修改配置后不能找回原有文件
root@iZ2zei3ltzorcuiiynrsemZ:cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
因?yàn)槲业姆?wù)器做了限制,兩臺(tái)服務(wù)器之間不能互通,所以我們使用兩臺(tái)服務(wù)器的內(nèi)網(wǎng)地址
server2
server1
server2測(cè)試與server1的內(nèi)網(wǎng)連通信
步驟二:
server2的nginx配置文件
server {
listen 80; #監(jiān)聽(tīng)端口
server_name _; #所有不匹配elite.chaitin.com的請(qǐng)求全都返回404
location / {
return 404;
}
}
#訪(fǎng)問(wèn)elite.chaitin.com的請(qǐng)求都會(huì)轉(zhuǎn)發(fā)到http://172.28.253.102:18080 并將結(jié)果返回給客戶(hù)端
server{
listen 80;
server_name elite.chaitin.com;
location / {
proxy_pass http://172.28.253.102:18080;
}
}
寫(xiě)dns解析:
root@iZ2zei3ltzorcuiiynrsemZ:~# vim /etc/hosts
172.28.253.107 elite.chaitin.com
測(cè)試訪(fǎng)問(wèn):
server2上訪(fǎng)問(wèn)
訪(fǎng)問(wèn)server2的IP只能看到server2的nginx默認(rèn)頁(yè)面,看不到server1的頁(yè)面
要求3 nginx負(fù)載均衡
要求3.假設(shè)再加1套Web Server 2,其8080端口提供與Server 1相同服務(wù),如何配置NGINX A實(shí)現(xiàn)訪(fǎng)問(wèn)http://elite.chaitin.com 的請(qǐng)求可以被負(fù)載分發(fā)到Server1或者Server2?
這里我用自己的本地虛擬機(jī)做實(shí)驗(yàn)
server1 | 192.168.15.133 |
---|---|
server2 | 192.168.15.138 |
負(fù)載均衡調(diào)度器 | 192.168.15.140 |
所有服務(wù)器都關(guān)閉防火墻和selinux
步驟一:server1和server2基礎(chǔ)配置
每臺(tái)服務(wù)器上都安裝nginx服務(wù)
server1和server2都做:
server1和server2的配置相同
server {
listen 8080;
server_name elite.chaitin.com;
root /chaitin/www; #自己定義的訪(fǎng)問(wèn)頁(yè)面
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
加載啟動(dòng)配置文件,創(chuàng)建web頁(yè)面,拷貝給server2
域名解析
測(cè)試訪(fǎng)問(wèn)頁(yè)面
步驟二:負(fù)載均衡調(diào)度器配置:
upstream server_pools { #接收來(lái)自server中的請(qǐng)求
server 192.168.15.138:8080 weight=1; #兩個(gè)服務(wù)器的權(quán)重都一樣,會(huì)把所有請(qǐng)求相對(duì)均勻的分配到兩臺(tái)服務(wù)器上
server 192.168.15.133:8080 weight=1;
}
server{
listen 8080;
server_name _; #所有不是請(qǐng)求elite.chaitin.com域名的8080服務(wù)都會(huì)被禁止并返回403的狀態(tài)碼
location / {
return 403;
}
}
server{
listen 8080;
server_name elite.chaitin.com;
location / {
proxy_pass http://server_pools; #訪(fǎng)問(wèn)8080端口且以elite.chaitin.com為域名的請(qǐng)求,會(huì)把請(qǐng)求轉(zhuǎn)發(fā)到server_pools的負(fù)載均衡池
}
}
}
域名解析
啟動(dòng)nginx并訪(fǎng)問(wèn)測(cè)試
請(qǐng)求能夠被均勻的分配到兩臺(tái)服務(wù)器上
要求4 nginx高可用
要求4. 在上文題目2基礎(chǔ)之上,增加節(jié)點(diǎn)NGINX B,如何配置實(shí)現(xiàn)高可用?demo演示當(dāng)NGINX A故障時(shí),流量自動(dòng)切換到NGINX B(客戶(hù)端訪(fǎng)問(wèn)幾乎無(wú)感知)。詳細(xì)說(shuō)明VRRP協(xié)議在本題所述場(chǎng)景下的應(yīng)用。
server1和server2安裝keepalived
[root@server1 ~]# yum install keepalived
server1和server2寫(xiě)腳本文件
[root@server1 yum.repos.d]# vi /usr/local/src/nginx_check.sh
#!/bin/bash
# 檢查是否開(kāi)啟nginx
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl stop keepalived
fi
server1
[root@server1 ~]# vim /etc/keepalived/keepalived.conf
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.15.143 #當(dāng)前keepalived所在的主機(jī)ip
smtp_connect_timeout 30
router_id 192.168.15.143 #當(dāng)前keepalived所在的主機(jī)ip
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_http_port{
script "/usr/local/src/nginx_check.sh"
interval 2 #每隔2s檢測(cè)一次腳本
weight -20 #權(quán)重減20
}
vrrp_instance VI_1 {
state MASTER #將當(dāng)前主機(jī)設(shè)置為master節(jié)點(diǎn)
interface eth0 #主機(jī)使用的網(wǎng)卡
virtual_router_id 51
priority 100 #優(yōu)先級(jí),主節(jié)點(diǎn)的優(yōu)先級(jí)更大
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.66 #虛擬ip,必須和master、backup處于同一網(wǎng)段
}
}
server2
[root@server2 ~]# vim /etc/keepalived/keepalived.conf
vrrp_gna_interval 0
}
vrrp_script chk_http_port{
script "/usr/local/src/nginx_check.sh"
interval 2 #每隔2s檢測(cè)一次腳本
weight -20 #權(quán)重減20
}
vrrp_instance VI_1 {
state BACKUP#將當(dāng)前主機(jī)設(shè)置為master節(jié)點(diǎn)
interface eth0 #主機(jī)使用的網(wǎng)卡
virtual_router_id 51
priority 90 #優(yōu)先級(jí),主節(jié)點(diǎn)的優(yōu)先級(jí)更大
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.66 #虛擬ip,必須和master、backup處于同一網(wǎng)段
}
}
都啟動(dòng)keepalived服務(wù)
[root@server2 ~]# vim /etc/keepalived/keepalived.conf
測(cè)試:
將server1的服務(wù)關(guān)閉
systemctl stop nginx
要求5 負(fù)載均衡算法
要求5. (加強(qiáng)提升題目)在上文題目3基礎(chǔ)之上,配置和驗(yàn)證NGINX支持哪些負(fù)載均衡算法及其效果?并結(jié)合NGINX配置參數(shù)詳細(xì)說(shuō)明不同負(fù)載均衡算法工作原理。
輪詢(xún)算法
輪詢(xún)(Round Robin):這是默認(rèn)的負(fù)載均衡算法。Nginx將每個(gè)新的客戶(hù)端請(qǐng)求按照服務(wù)器列表的順序分發(fā),依次輪流選擇下一個(gè)服務(wù)器。這是最簡(jiǎn)單的負(fù)載均衡算法,適用于后端服務(wù)器性能相近的情況。
加權(quán)輪詢(xún)(Weighted Round Robin):在這種算法中,每個(gè)后端服務(wù)器都分配了一個(gè)權(quán)重值,高權(quán)重的服務(wù)器會(huì)收到更多的請(qǐng)求。這對(duì)于在后端服務(wù)器之間分配不同的負(fù)載容量非常有用,以確保性能更好的服務(wù)器獲得更多的請(qǐng)求。
upstream server_pools {
server 192.168.15.138:8080 weight=1; #兩臺(tái)服務(wù)器響應(yīng)請(qǐng)求的方式為1:2
server 192.168.15.133:8080 weight=2;
}
server{
listen 8080;
server_name _;
location / {
return 403;
}
}
server{
listen 8080;
server_name elite.chaitin.com;
location / {
proxy_pass http://server_pools;
}
}
}
重啟服務(wù)并測(cè)試
[root@lvs ~]# vim /etc/nginx/nginx.conf
[root@lvs ~]# systemctl daemon-reload
[root@lvs ~]# systemctl restart nginx
源地址哈希法
IP哈希(IP Hash):Nginx使用客戶(hù)端的IP地址來(lái)計(jì)算哈希值,并將請(qǐng)求分發(fā)到具有相同哈希值的后端服務(wù)器。這個(gè)算法確保相同IP的客戶(hù)端始終訪(fǎng)問(wèn)相同的后端服務(wù)器,適用于需要會(huì)話(huà)保持的情況,如Web應(yīng)用程序的會(huì)話(huà)管理。
在負(fù)載均衡服務(wù)器上配置
upstream server_pools {
hash $remote_addr consistent; #使用客戶(hù)端的IP地址進(jìn)行哈希計(jì)算,
#并且使用consistent關(guān)鍵字確保負(fù)載均衡在后端服務(wù)器發(fā)生變化時(shí)盡量減少數(shù)據(jù)遷移。
server 192.168.15.138:8080;
server 192.168.15.133:8080;
}
server{
listen 8080;
server_name _;
location / {
return 403;
}
}
server{
listen 8080;
server_name elite.chaitin.com;
location / {
proxy_pass http://server_pools;
}
}
}
重啟nginx服務(wù)
[root@lvs ~]# systemctl daemon-reload
[root@lvs ~]# systemctl restart nginx
另開(kāi)一臺(tái)客戶(hù)端主機(jī)
寫(xiě)域名解析文件
[root@localhost ~]# vi /etc/hosts
192.168.15.140 elite.chaitin.com #訪(fǎng)問(wèn)的是負(fù)載均衡服務(wù)器
測(cè)試
可以發(fā)現(xiàn)客戶(hù)端請(qǐng)求的一直都是同一個(gè)服務(wù)器
最小連接數(shù)算法
最少連接(Least Connections):Nginx跟蹤每個(gè)后端服務(wù)器的活動(dòng)連接數(shù),然后將請(qǐng)求發(fā)送到具有最少活動(dòng)連接數(shù)的服務(wù)器。這有助于分配負(fù)載并確保請(qǐng)求發(fā)送到連接較少的服務(wù)器,從而實(shí)現(xiàn)負(fù)載均衡。
upstream server_pools {
least_conn; #使用最小連接數(shù)算法
server 192.168.15.138:8080;
server 192.168.15.133:8080;
}
server{
listen 8080;
server_name _;
location / {
return 403;
}
}
server{
listen 8080;
server_name elite.chaitin.com;
location / {
proxy_pass http://server_pools;
}
}
}
重啟nginx服務(wù)
因?yàn)闇y(cè)試需要用到多臺(tái)客戶(hù)端,但我的服務(wù)器沒(méi)有那么多,所以就不測(cè)試?yán)玻?/p>
要求6 健康檢查
要求6. (加強(qiáng)提升題目)在上文題目3基礎(chǔ)之上,配置和驗(yàn)證主動(dòng)和被動(dòng)兩種健康檢查方式及其效果?并結(jié)合NGINX配置參數(shù)詳細(xì)說(shuō)明不同健康檢查方式區(qū)別和工作原理。
主動(dòng)健康檢查
原理:
主動(dòng)健康檢查是通過(guò)Keepalived自身執(zhí)行的周期性檢查來(lái)確定服務(wù)器的狀態(tài)。Keepalived會(huì)定期發(fā)送請(qǐng)求(通常是ICMP ping或TCP連接請(qǐng)求)到服務(wù)器,并根據(jù)服務(wù)器的響應(yīng)來(lái)確定其是否處于健康狀態(tài)。
配置:
主動(dòng)健康檢查需要在Keepalived配置文件中定義一個(gè)或多個(gè)健康檢查腳本,這些腳本將定期運(yùn)行以檢查服務(wù)器的健康狀態(tài)。在配置文件中使用track_script部分來(lái)定義這些檢查腳本
track_script {
chk_script {
script "/etc/keepalived/check_script.sh" # 健康檢查腳本的路徑
interval 2 # 檢查間隔時(shí)間,單位秒
weight 2 # 權(quán)重
}
}
腳本文件
[root@server1 ~]# vim /etc/check_script.sh
#!/bin/bash
# 定義要檢查的目標(biāo)URL
TARGET_URL="http://localhost:80"
# 發(fā)送HTTP請(qǐng)求并獲取響應(yīng)
HTTP_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" $TARGET_URL)
# 檢查HTTP響應(yīng)狀態(tài)碼是否為200(正常)
if [ "$HTTP_RESPONSE" = "200" ]; then
# 返回0表示健康
exit 0
else
# 返回1表示不健康
exit 1
fi
被動(dòng)健康檢查:
原理:
被動(dòng)健康檢查是通過(guò)監(jiān)聽(tīng)服務(wù)器上的服務(wù)來(lái)確定服務(wù)器的狀態(tài)。Keepalived將偵聽(tīng)服務(wù)器上的服務(wù)端口,如果無(wú)法連接到該端口,將認(rèn)為服務(wù)器處于不健康狀態(tài)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-697242.html
配置:
被動(dòng)健康檢查不需要特別的配置,只需確保Keepalived配置文件中的虛擬IP與服務(wù)器上的服務(wù)端口一致即可。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-697242.html
到了這里,關(guān)于Nginx全家桶配置詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!