nginx学习九 upstream 负载均衡
生活随笔
收集整理的這篇文章主要介紹了
nginx学习九 upstream 负载均衡
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
- 語(yǔ)法 Syntax: upstream name {...}
Default:--
Context:http
?
- 后端服務(wù)器在負(fù)載均衡調(diào)度中的狀態(tài):
down 當(dāng)前的server暫時(shí)不參與負(fù)載均衡 backup 預(yù)留的備份服務(wù)器 max_fails 允許請(qǐng)求失敗的次數(shù) fail_timeout 經(jīng)過(guò)max_fails失敗后,服務(wù)暫停的時(shí)間 max_conns 限制最大的接收的連接數(shù) ?
- ?nginx 的調(diào)度算法:
輪詢 ?按時(shí)間順序逐一分配到不同的后端服務(wù)器 加權(quán)輪詢? ?weight值越大,分配到的訪問(wèn)幾率越高 ip_hash 每個(gè)請(qǐng)求按訪問(wèn)IP的hash結(jié)果分配,這樣來(lái)自同一個(gè)IP的請(qǐng)求,固定訪問(wèn)一個(gè) least_conn 最少連接數(shù),哪個(gè)機(jī)器連接數(shù)少就分發(fā) url_hash 按照訪問(wèn)的URL的hash結(jié)果來(lái)分配請(qǐng)求,是每個(gè)URL定向到同一個(gè)后端服務(wù)器 hash關(guān)鍵數(shù)值 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;
}
?
- 加權(quán)輪詢 upstream test_upstream {server localhost:8001;server localhost:8002 weight=5;server localhost:8003;
}
?
- 備份節(jié)點(diǎn)? 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 ?語(yǔ)法(1.7.2版本開(kāi)始) Syntax: hash key [consistent]; Default: -- Context: upstream ##例: upstream test_upstream {hash $request_uri;server localhost:8001; server localhost:8002;server localhost:8003; } ?
轉(zhuǎn)載于:https://my.oschina.net/langgege/blog/1930524
總結(jié)
以上是生活随笔為你收集整理的nginx学习九 upstream 负载均衡的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Java 8 CompletableFu
- 下一篇: 新生命 · 人工智能 · 未来