Day50
課堂筆記
2019年5月10日
編譯安裝負(fù)載均衡深入
?
兩個模塊:
upstream服務(wù)器池,里面放服務(wù)器
upstream backend{
????server ?10.0.0.7:80 weight=1;
????server ?10.0.0.7:80 weight=1;
server ?10.0.0.9:80 weight=1;
}
?
這種分配方式叫做負(fù)載均衡算法
第一類為靜態(tài)調(diào)度算法:
和節(jié)點無關(guān)的調(diào)度算法
l rr輪詢(默認(rèn)調(diào)度算法,靜態(tài)調(diào)度算法)
l wrr(權(quán)重輪詢,靜態(tài)調(diào)度算法)
l ip_hash(靜態(tài)調(diào)度算法) ?hash(ip)===oldboy 調(diào)度給某一臺機(jī)器
負(fù)載不均衡
解決會話保持https://blog.51cto.com/oldboy/1331316
?
第一類為動態(tài)調(diào)度算法:
l fair(動態(tài)調(diào)度算法)
l least_conn最小連接調(diào)度算法
l url_hash算法 同一個URL地址,調(diào)度給同一臺節(jié)點。
hash(url)===oldgril調(diào)度給某一臺機(jī)器
http://nginx.org/en/docs/http/ngx_http_upstream_module.html調(diào)度給A服務(wù)器
web緩存服務(wù)器的負(fù)載均衡
一致性HASH算法
proxy
1)將匹配URI為name的請求拋給http://127.0.0.1/remote/。
?
?server {
????????listen ??????80;
????????server_name ?blog.etiantian.org;
????????location / {
????????????proxy_pass http://backend;
????????????proxy_set_header Host ?$host;
????????}
???}
?
???
???????server {
????????listen ??????80;
????????server_name ?www.etiantian.org;
????????location / {
????????????proxy_pass http://backend;
????????????proxy_set_header Host ?$host;
????????????proxy_set_header X-Forwarded-For $remote_addr;
????????}
???}
?
根據(jù)路徑轉(zhuǎn)發(fā):
location /name/ {
????proxy_pass http://127.0.0.1/remote/;
}
?
?
?
?
根據(jù)url中的目錄地址實現(xiàn)代理轉(zhuǎn)發(fā)說明
?
location /name/ {
????proxy_pass http://127.0.0.1/remote/;
}
?
案例背景:通過Nginx實現(xiàn)動靜分離,即通過Nginx反向代理配置規(guī)則實現(xiàn)讓動態(tài)資源和靜態(tài)資源及其他業(yè)務(wù)分別由不同的服務(wù)器解析,以解決網(wǎng)站性能、安全、用戶體驗等重要問題。
?
?
upstream static_pools {
?????server 10.0.0.7:80 ?weight=1;
????}
?
upstream upload_pools {
?????????server 10.0.0.8:80 ?weight=1;
????}
?
upstream default_pools {
?????????server 10.0.0.9:80 ?weight=1;
???}
?
#www.etiantian.org/static/xxxxx路徑調(diào)度到static_pools
location /static/ {
proxy_pass http://static_pools;
}
location /upload/ {
proxy_pass http://upload_pools;
}
location / {
proxy_pass http://default_pools;
}
?
?
?
域名不變,基于路徑實現(xiàn)動靜分離:
http://www.etiantian.org/static/
http://www.etiantian.org/upload/
?
http://www.etiantian.org/
http://www.etiantian.org/new/
?
http://www.etiantian.org/static/
?
?
基于擴(kuò)展名實現(xiàn)剛才的案例
location ~ .*.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
proxy_pass http://static_pools;
}
location / {
proxy_pass http://default_pools;
}
?
location /upload/ {
proxy_pass http://upload_pools;
}
?
?
[root@lb01 conf]# cat nginx.conf.base_uri
worker_processes ?1;
events {
????worker_connections ?1024;
}
http {
????include ??????mime.types;
????default_type ?application/octet-stream;
????sendfile ???????on;
????keepalive_timeout ?65;
?
upstream static_pools {
?????server 10.0.0.7:80 ?weight=1;
????}
?
upstream upload_pools {
?????????server 10.0.0.8:80 ?weight=1;
????}
?
upstream default_pools {
?????????server 10.0.0.9:80 ?weight=1;
???}
?
????server {
????????listen ??????80;
????????server_name ?www.etiantian.org;
?
location /static/ {
proxy_pass http://static_pools;
????????proxy_set_header Host ?$host;
????????proxy_set_header X-Forwarded-For $remote_addr;
}
location /upload/ {
proxy_pass http://upload_pools;
????????????proxy_set_header Host ?$host;
????????????proxy_set_header X-Forwarded-For $remote_addr;
}
location / {
proxy_pass http://default_pools;
????????proxy_set_header Host ?$host;
????????proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
?
?
?
直接通過不同的域名實現(xiàn)動靜分離:
www.etiantian.org動態(tài)
img.etiantian.org靜態(tài)
?
?
?
基于不同類型的設(shè)備實現(xiàn)轉(zhuǎn)發(fā):
HTTP報文的請求頭里,會提供http_user_agent信息。
http_user_agent用戶的設(shè)備:
?
?
location / {
????????if ($http_user_agent ~* "android")
??????????{
proxy_pass http://android_pools; #<==這是android服務(wù)器池,需要提前定義upstream。
??????????}
????????if ($http_user_agent ~* "iphone")
??????????{
proxy_pass http://iphone_pools; #<==這是iphone服務(wù)器池,需要提前定義upstream。
????????????}
????????proxy_pass http://pc_pools;
???????}
?
???
location / {
?????????if ($http_user_agent ~* "MSIE")
??????????{
????????????proxy_pass http://static_pools;
??????????}
??????????if ($http_user_agent ~* "Chrome")
??????????#if ($http_user_agent ~* "Firefox")
??????????{
????????????proxy_pass http://upload_pools;
??????????}
????????proxy_pass http://default_pools;
????????include proxy.conf;
???????}
?
?
?
??????location / {
????????if ($http_user_agent ~* "android")
??????????{
????????????proxy_pass http://static_pools;
??????????}
????????if ($http_user_agent ~* "iphone")
??????????{
????????????proxy_pass http://upload_pools;
????????????}
????????proxy_pass http://default_pools;
???????}
?
[root@lb01 conf]# curl -A "android" http://www.etiantian.org/upload/
upload server
[root@lb01 conf]# curl -A "iphone" http://www.etiantian.org/static/ ???????????
static server
?
?
?
[root@lb01 conf]# curl -A "android" http://www.etiantian.org
www7
[root@lb01 conf]# curl -A "iphone" http://www.etiantian.org/
www8
[root@lb01 conf]# curl ?http://www.etiantian.org/
www9
?
?
?
安卓:
http://192.168.12.201:8000/upload/
upload
?
iphone
http://192.168.12.201:8000/static/
static
?
其他:
http://192.168.12.201:8000/
http://192.168.12.201:8000/new/
?
?
[root@lb01 conf]# curl -A "android" http://www.etiantian.org/upload/
upload
[root@lb01 conf]# curl -A "iphone" http://www.etiantian.org/static/ ???????????
static
?
[root@lb01 conf]# curl -A "android" http://www.etiantian.org
www7
[root@lb01 conf]# curl -A "iphone" http://www.etiantian.org/
www8
[root@lb01 conf]# curl ?http://www.etiantian.org/
www9
?
?
實踐:
1、基于路徑轉(zhuǎn)發(fā) ?????????實現(xiàn)動靜分離
2、基于擴(kuò)展名轉(zhuǎn)發(fā) ???????實現(xiàn)動靜分離
3、基于user_agent轉(zhuǎn)發(fā) ???實現(xiàn)PC 手機(jī) 不同手機(jī)分離
?
?
?
?
?
直接通過不同的域名實現(xiàn)動靜分離:
?
www.etiantian.org動態(tài)
?
img.etiantian.org靜態(tài)
?
負(fù)載均衡器已經(jīng)配好了動態(tài)和靜態(tài)的負(fù)載均衡:
?server {
????????listen ??????80;
????????server_name ?blog.etiantian.org;
????????location / {
????????????proxy_pass http://backend;
????????????proxy_set_header Host ?$host;
????????} ??
???} ???
?server {
????????listen ??????80;
????????server_name ?img.etiantian.org img1.etiantian.org;
????????location / {
????????????proxy_pass http://backend;
????????????proxy_set_header Host ?$host;
????????} ??
???} ?
?
???
Web01上創(chuàng)建靜態(tài)服務(wù):
?[root@web01 /etc/nginx/conf.d]# cat img.conf
server {
????????listen ??????80;
????????server_name ?img.etiantian.org;
????????location / {
????root ??/usr/share/nginx/html/img;
????????????index ?index.html;
????????}
????}
?
??
Web02上創(chuàng)建靜態(tài)服務(wù):
???
[root@web02 /application/nginx/conf/extra]# cat 05_img.conf
server {
????????listen ??????80;
????????server_name ?img.etiantian.org;
????????location / {
????root ??html/img;
????????????index ?index.html;
????????}
????}
?
?
?
?
?
?
?
?
?
?
?
?