nginx系列友情鏈接:
nginx高性能WEB服務器系列之一簡介及安裝
https://www.cnblogs.com/maxtgood/p/9597596.html
nginx高性能WEB服務器系列之二命令管理
https://www.cnblogs.com/maxtgood/p/9597990.html
nginx高性能WEB服務器系列之三版本升級
https://www.cnblogs.com/maxtgood/p/9598113.html
nginx高性能WEB服務器系列之四配置文件詳解
https://www.cnblogs.com/maxtgood/p/9598333.html
nginx高性能WEB服務器系列之五--實戰項目線上nginx多站點配置
https://www.cnblogs.com/maxtgood/p/9598610.html
nginx高性能WEB服務器系列之六--nginx負載均衡配置+健康檢查
https://www.cnblogs.com/maxtgood/p/9599068.html
nginx高性能WEB服務器系列之七--nginx反向代理
https://www.cnblogs.com/maxtgood/p/9599335.html
nginx高性能WEB服務器系列之八--nginx日志分析與切割
https://www.cnblogs.com/maxtgood/p/9599542.html
nginx高性能WEB服務器系列之九--nginx運維故障日常解決方案
https://www.cnblogs.com/maxtgood/p/9599752.html
注:原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。
nginx的強大之處不必要我細說,當初第一次接觸nginx的時候就發現了它的強大之處,並且自我覺得非常有必要出一篇記錄nginx的各個功能及坑點。
歡迎大家對nginx感興趣的朋友們來一起學習與及時提出錯誤及誤點。有問題的可以在評論區@我。
一:nginx負載均衡配置
其實負載均衡的意思很簡單明了,網上很多原理一大堆的解釋,可能看的似懂非懂。這里本人畫了一個簡單粗暴的原理圖,僅供參考:
解釋:其實nginx 作為一個輕量級、高性能的 web server 主要可以干的就兩件事情,
第一件事就是直接作為http server(代替apache,對PHP需要FastCGI處理器支持);
第二件事就是作為反向代理服務器實現負載均衡
因為nginx在處理並發方面的優勢,現在這個應用非常常見。
當然了Apache的 mod_proxy和mod_cache結合使用也可以實現對多台app server的反向代理和負載均衡,但是在並發處理方面apache還是沒有 nginx擅長。
這里介紹一種實踐負載均衡+健康檢查的方法。
直接vim /usr/local/nginx/conf/nginx.conf 修改upstream 段配置文件:
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"'; sendfile on; keepalive_timeout 65; upstream worldcup { server 10.124.25.28:8001; server 10.124.25.29:8001; }
nginx配置文件詳解的時候也提到了,注意upstream后面接的關鍵詞,如果需要單台,同端口負載不同請求的時候,需要制定不同upstream,以下提供一個實踐實例。
實踐示例,僅供參考,不做解釋:
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' sendfile on; keepalive_timeout 65; upstream keep_one { server 192.168.1.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.2:8080 weight=1 max_fails=2 fail_timeout=30s; } upstream keep_two { server 192.168.1.3:8081 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.4:8081 weight=1 max_fails=2 fail_timeout=30s; } server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } location /one { root html; index index.html index.htm; proxy_pass http://keep_one/; proxy_set_header Host $http_host; proxy_set_header Cookie $http_cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; client_max_body_size 300m; } location /two { root html; index index.html index.htm; proxy_pass http://keep_two/; proxy_set_header Host $http_host; proxy_set_header Cookie $http_cookie; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; client_max_body_size 300m; } } }
提供兩個負載接口,同台服務器,同個IP,同個端口。
二:nginx負載后端的監控檢查
其實以上配置文件中已經配置了健康檢查的例子,以下介紹兩種健康檢查的方式。
方法一:
添加upstream的時候,直接ip+port后接weight=1 max_fails=2 fail_timeout=30s;
###如以下代碼
upstream fastdfs_tracker { server 192.168.1.1:8080 weight=1 max_fails=2 fail_timeout=30s; server 192.168.1.2:8080 weight=1 max_fails=2 fail_timeout=30s; }
解釋:weight為配置的權重,在fail_timeout內檢查max_fails次數,失敗則剔除均衡。
方法二:
添加upstream的時候,在最后一行添加
###如以下代碼:
upstream test{ #負載均衡配置,默認的策略,按時間先后,有其他按ip hash,權重 server 192.168.1.1:8080; server 192.168.1.2:8080; server 192.168.1.3:8080; check interval=3000 rise=2 fall=3 timeout=3000 type=http port=7070;
}
解釋:# interval=3000:間隔3秒檢查一次,rise=2:檢查2次ok后端節點up,fall=3:三次檢查失敗后端節點down,timeout=3000:超時時間3秒,type=http:發http檢查請求類型,port=8080檢查端口,可省略,默認和server 192.168.1.1:8080中的端口一致。
至此關於nginx最常用的負載均衡+健康檢查已經配置完成,后系列中還會介紹到相對常用的nginx的反向代理。