nginx的负载均衡
根據(jù)osi分四層負(fù)載均衡和七層負(fù)載均衡
四層:主要是在傳輸層,傳輸層能支持到tcp協(xié)議的控制,所以對客戶端的請求只需要進(jìn)行tcp/ip的包轉(zhuǎn)發(fā),就可以實(shí)現(xiàn)負(fù)載均衡
七層:主要是在應(yīng)用層,可以實(shí)現(xiàn)http協(xié)議的改寫,頭信息的改寫,安全應(yīng)用規(guī)則的控制以及轉(zhuǎn)發(fā)等的規(guī)則,nginx就是七層負(fù)載均衡的SLB
?
?
?
nginx負(fù)載均衡實(shí)現(xiàn)的原理:使用的是proxy_pass
把所有端的請求,代理轉(zhuǎn)發(fā)到對應(yīng)后端的服務(wù)器上,轉(zhuǎn)發(fā)到一組小服務(wù)池,upstream server
配置語法:
必須是在http以內(nèi),server層以外
例子:
在/opt/app下面有三個(gè)文件夾 code1 code2 code3
在每個(gè)文件夾中都放了不同展示效果的頁面
比如其中一個(gè)頁面index.html
<html> <head><meta charset="utf-8"><title>server1</title> </head> <body style="background-color:yellow;"><h1>server 1</h1> </body> </html>在 /etc/nginx/conf.d/下建了三個(gè)conf? ?server1.conf? server2.conf? ?server3.conf
比如其中一個(gè)conf中server1.conf
server{listen 8001;server_name localhost;access_log /var/log/nginx/log/server1.access.log main;location /{root /opt/app/code1;index index.html index.htm;}server2.conf
server{listen 8002;server_name localhost;access_log /var/log/nginx/log/server2.access.log main;location /{root /opt/app/code2;index index.html index.htm;}等
開始配置負(fù)載均衡
新建一個(gè)虛擬server 叫:upsream_test.conf
upstream imooc{server 116.62.103.228:8001; server 116.62.103.228:8002; server 116.62.103.228:8003; } server{listen 80;server_name localhost jeson.peak;access_log /var/log/nginx/test_proxy.access.log main;location /{proxy_pass http://imooc;include proxy_params;} }nginx -s reload -c /etc/nginx/nginx.conf
訪問 jeson.peak就可以了 ,默認(rèn)是輪詢的狀態(tài)
假設(shè)有一個(gè)服務(wù)掛掉
iptables -I INPUT -p tcp --dport 8002 -j DROP
用這個(gè)規(guī)則關(guān)掉8002之后
就不會顯示對應(yīng)的8002端口對應(yīng)的頁面了
?
清理規(guī)則 iptables -F
配置負(fù)載均衡調(diào)度中的狀態(tài)
nginx的輪詢策略與加權(quán)輪詢
ip_hash是把相同請求轉(zhuǎn)到同一臺服務(wù)器
url_hash 是基于url負(fù)載均衡的方式
?
轉(zhuǎn)載于:https://www.cnblogs.com/gaosf/p/10232131.html
總結(jié)
以上是生活随笔為你收集整理的nginx的负载均衡的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第6章 数组、指针与字符串(一)基于范围
- 下一篇: src与href的区别