elasticsearch入門系列">elasticsearch入門系列
679
2022-05-29
一、微服務場景下Nginx的使用
Nginx默認使用輪詢算法
1.最小活躍數算法
問題:當客戶端給Nginx發送查詢商品的請求時,Nginx把請求轉發給5001 和 5002 ,如果5002處理請求比較慢,會導致請求堆積在5002。如何解決請求堆積問題?
解決方案:最小活躍數算法
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://YDT.EBusiness; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #動態負載均衡配置 upstream YDT.EBusiness{ least_conn; server localhost:5001; server localhost:5002; } }
2.Hash一致性算法
問題:當客戶端給Nginx發送查詢商品的請求時,Nginx把請求轉發給5001 和 5002 ,如果5001和5002分別都有緩存,會導致緩存命中下降,請求會回源到數據庫中去查詢數據,導致性能下降。如何提升性能?
解決方案:Hash一致性算法
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://YDT.EBusiness; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #動態負載均衡配置 upstream YDT.EBusiness{ ip_hash; server localhost:5001; server localhost:5002; } }
3.失敗重試
問題:當客戶端給Nginx發送查詢商品的請求時,Nginx把請求轉發給5001 和 5002 ,如果轉發到5001的時候,5001零時宕機了,會導致請求失敗。如何保證請求成功
解決方案:失敗重試
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://YDT.EBusiness; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #動態負載均衡配置 upstream YDT.EBusiness{ ip_hash; server localhost:5001 max_fails=2 fail_timeout=10s; server localhost:5002 max_fails=2 fail_timeout=10s; } }
4.故障轉移
問題:當客戶端給Nginx發送查詢商品的請求時,Nginx把請求轉發給5001 和 5002 ,如果轉發到5001的時候,5001永久宕機了,會導致請求失敗。如何保證請求成功
解決方案:故障轉移
#注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區 proxy_temp_path /data0/proxy_temp_dir; #設置Web緩存區名稱為cache_one,內存緩存空間大小為200MB,1天沒有被訪問的內容自動清除,硬盤緩存空間大小為30GB。 proxy_cache_path /data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g; #輪詢服務器,weight為服務器權重,與訪問頻率成正比,max_fails最大超時次數,fail_timeout服務器代理監聽超時時間 upstream backend_server { server 192.168.203.43:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.203.44:80 weight=1 max_fails=2 fail_timeout=30s; server 192.168.203.45:80 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name www.yourdomain.com 192.168.203.42; index index.html index.htm; root /data0/htdocs/www; location / { #如果后端的服務器返回502、504、執行超時等錯誤,自動將請求轉發到upstream負載均衡池中的另一臺服務器,實現故障轉移。 proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; #對不同的HTTP狀態碼設置不同的緩存時間 proxy_cache_valid 200 304 12h; #以域名、URI、參數組合成Web緩存的Key值,Nginx根據Key值哈希,存儲緩存內容到二級緩存目錄內 proxy_cache_key $host$uri$is_args$args; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; proxy_pass http://backend_server; expires 1d; } }
5.主機備份
問題:當客戶端給Nginx發送查詢商品的請求時,Nginx把請求轉發給5001 和 5002 ,如果轉發到5001和5002,兩個實例同時宕機了,會導致系統不可用,如何保證系統高度可用?
解決方案:主機備份
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://YDT.EBusiness; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #動態負載均衡配置 upstream YDT.EBusiness{ ip_hash; server localhost:5001 max_fails=2 fail_timeout=10s; server localhost:5002 max_fails=2 fail_timeout=10s; server localhost:5003 backup; } }
.NET Nginx 分布式 彈性負載均衡 ELB
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。