nginx的负载均衡
根據osi分四層負載均衡和七層負載均衡
四層:主要是在傳輸層,傳輸層能支持到tcp協議的控制,所以對客戶端的請求只需要進行tcp/ip的包轉發,就可以實現負載均衡
七層:主要是在應用層,可以實現http協議的改寫,頭信息的改寫,安全應用規則的控制以及轉發等的規則,nginx就是七層負載均衡的SLB
?
?
?
nginx負載均衡實現的原理:使用的是proxy_pass
把所有端的請求,代理轉發到對應后端的服務器上,轉發到一組小服務池,upstream server
配置語法:
必須是在http以內,server層以外
例子:
在/opt/app下面有三個文件夾 code1 code2 code3
在每個文件夾中都放了不同展示效果的頁面
比如其中一個頁面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/下建了三個conf? ?server1.conf? server2.conf? ?server3.conf
比如其中一個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;}等
開始配置負載均衡
新建一個虛擬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就可以了 ,默認是輪詢的狀態
假設有一個服務掛掉
iptables -I INPUT -p tcp --dport 8002 -j DROP
用這個規則關掉8002之后
就不會顯示對應的8002端口對應的頁面了
?
清理規則 iptables -F
配置負載均衡調度中的狀態
nginx的輪詢策略與加權輪詢
ip_hash是把相同請求轉到同一臺服務器
url_hash 是基于url負載均衡的方式
?
轉載于:https://www.cnblogs.com/gaosf/p/10232131.html
總結
以上是生活随笔為你收集整理的nginx的负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第6章 数组、指针与字符串(一)基于范围
- 下一篇: src与href的区别