nginx学习九 upstream 负载均衡
生活随笔
收集整理的這篇文章主要介紹了
nginx学习九 upstream 负载均衡
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
- 語法 Syntax: upstream name {...}
Default:--
Context:http
?
- 后端服務器在負載均衡調度中的狀態:
down 當前的server暫時不參與負載均衡 backup 預留的備份服務器 max_fails 允許請求失敗的次數 fail_timeout 經過max_fails失敗后,服務暫停的時間 max_conns 限制最大的接收的連接數 ?
- ?nginx 的調度算法:
輪詢 ?按時間順序逐一分配到不同的后端服務器 加權輪詢? ?weight值越大,分配到的訪問幾率越高 ip_hash 每個請求按訪問IP的hash結果分配,這樣來自同一個IP的請求,固定訪問一個 least_conn 最少連接數,哪個機器連接數少就分發 url_hash 按照訪問的URL的hash結果來分配請求,是每個URL定向到同一個后端服務器 hash關鍵數值 hash自定義的key ?
- 例?/etc/nginx/conf.d/default.con server {listen 8001;server_name localhost;#charset koi8-r;access_log /var/log/nginx/server.access.log main;location / {root /opt/app/code1;index index.html index.htm;}
... ...
}server {listen 8002;server_name localhost;#charset koi8-r;access_log /var/log/nginx/server.access.log main;location / {root /opt/app/code2;index index.html index.htm;}
... ...
}server {listen 8003;server_name localhost;#charset koi8-r;access_log /var/log/nginx/server.access.log main;location / {root /opt/app/code3;index index.html index.htm;}
... ...
}server {listen 80;server_name localhost;#charset koi8-r;access_log /var/log/nginx/test_proxy.access.log main;location / {proxy_pass http://test_upstream;}
... ...
}##普通輪詢
upstream test_upstream {server localhost:8001;server localhost:8002;server localhost:8003;
}
?
- 加權輪詢 upstream test_upstream {server localhost:8001;server localhost:8002 weight=5;server localhost:8003;
}
?
- 備份節點? upstream test_upstream {server localhost:8001 down;?server localhost:8002 backup;server localhost:8003 max_fails=1 fail_timeout=10s;
}
?
- ip_hash? upstream test_upstream {ip_hash;server localhost:8001; server localhost:8002;server localhost:8003;
}
?
- url_hash ?語法(1.7.2版本開始) Syntax: hash key [consistent]; Default: -- Context: upstream ##例: upstream test_upstream {hash $request_uri;server localhost:8001; server localhost:8002;server localhost:8003; } ?
轉載于:https://my.oschina.net/langgege/blog/1930524
總結
以上是生活随笔為你收集整理的nginx学习九 upstream 负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 8 CompletableFu
- 下一篇: 新生命 · 人工智能 · 未来