摘要:負載均衡: 負載均衡是一種技術(shù),用于在多個服務器之間分發(fā)傳入的網(wǎng)絡流量,以平衡服務器的負載,提高系統(tǒng)的可用性和性能。當您有多臺服務器時,您可以使用負載均衡將請求分發(fā)到這些服務器上,從而防止單個服務器過載而影響用戶體驗。
反向代理: 反向代理是一種服務器配置,它將傳入的客戶端請求轉(zhuǎn)發(fā)到后端服務器,并將響應從后端服務器返回給客戶端。在負載均衡環(huán)境中,反向代理位于客戶端和后端服務器之間??蛻舳藢⒄埱蟀l(fā)送到反向代理,然后反向代理將請求轉(zhuǎn)發(fā)到一個或多個后端服務器。反向代理還可以執(zhí)行其他任務,如 SSL 終止、安全性增強、緩存等。
總結(jié):在配置 Nginx 的負載均衡時,您將配置反向代理服務器,該服務器將負責將傳入的請求分發(fā)給后端服務器。這種配置可以提高系統(tǒng)的可伸縮性和穩(wěn)定性。通常情況下,您會使用 Nginx 的 upstream 指令定義后端服務器列表,并使用 location 塊將請求轉(zhuǎn)發(fā)給這些后端服務器。
1.安裝nginx(三臺主機全部需要)
上傳rpm包,使用yum安裝
[root@node1 ~]# yum install nginx-1.22.0-1.el7.ngx.x86_64.rpm -y
2.配置兩臺測試的web服務器(兩臺web服務器node3和node4都執(zhí)行)
[root@node3 ~]# cd /etc/nginx/conf.d/
[root@node3 conf.d]# mv default.conf{,.bak}
[root@node3 conf.d]# vim vhost.conf
# 配置虛擬主機
server {
listen 80;
server_name bbb.itxuan.com;
location / {
root /usr/share/nginx/html/bbb;
index index.html index.htm;
}
access_log /usr/share/nginx/html/bbb/logs/access_bbb.log main;
}
server {
listen 80;
server_name www.itxuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
access_log /usr/share/nginx/html/www/logs/access_www.log main;
}
3.配置web測試頁(node3,node4都執(zhí)行)
[root@node3 conf.d]# mkdir -p /usr/share/nginx/html/{www,bbb}/logs
[root@node3 conf.d]# echo "`hostname -I ` www" > /usr/share/nginx/html/www/index.html
[root@node3 conf.d]# echo "`hostname -I ` bbb" > /usr/share/nginx/html/bbb/index.html
4.啟動服務進行測試
[root@node1 ~]# systemctl start nginx
[root@node1 ~]# curl -H host:bbb.itxun.com 192.168.136.164
192.168.136.164 bbb
[root@node1 ~]# curl -H host:bbb.itxun.com 192.168.136.163
192.168.136.163 bbb
5.配置node1端的負載均衡
[root@node1 ~]# vim /etc/nginx/conf.d/default.conf
upstream www_server_pools{
server 192.168.136.163:80;
server 192.168.136.164:80;
}
server {
listen 80;
server_name www.itxuan.com;
#access_log /var/log/nginx/host.access.log main;
location / {
# root /usr/share/nginx/html;
# index index.html index.htm;
proxy_pass http://www_server_pools;
}
nginx檢查并重啟服務文章來源:http://www.zghlxwxcb.cn/news/detail-646259.html
[root@node1 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@node1 ~]# systemctl restart nginx文章來源地址http://www.zghlxwxcb.cn/news/detail-646259.html
6.配置node1端hosts解析并測試
[root@node1 ~]# vim /etc/hosts
192.168.136.161 www.itxuan.com
[root@node1 ~]# for ((i=1;i<=4;i++)); do curl http://www.itxuan.com; done
192.168.136.164 bbb
192.168.136.163 bbb
192.168.136.164 bbb
192.168.136.163 bbb
7.stop掉任意一個節(jié)點在進行測試
[root@node3 ~]# systemctl stop nginx
[root@node1 ~]# for ((i=1;i<=4;i++)); do curl http://www.itxuan.com; done
192.168.136.164 bbb
192.168.136.164 bbb
192.168.136.164 bbb
192.168.136.164 bbb
7.節(jié)點恢復正常后在進行測試
[root@node3 ~]# systemctl start nginx
[root@node1 ~]# for ((i=1;i<=4;i++)); do curl http://www.itxuan.com; done
192.168.136.164 bbb
192.168.136.164 bbb
192.168.136.163 bbb
192.168.136.164 bbb
到了這里,關(guān)于基于centos7.9通過nginx實現(xiàn)負載均衡以及反向代理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!