nginx反向代理:用戶請求nginx代理服務器而後代理服務器將用戶請求轉爲服務器再由nginx代理服務器將服務器的響應反應給用戶。html
lvs負載均衡:用戶請求nginx代理服務器而後代理服務器將用戶請求轉爲服務器再由服務器直接響應用戶的請求nginx
nginx反向代理用的是ngx_http_upstream_module
module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives.web
在http語塊內定義:算法
upstream web_pools{apache
ip_hash;#wrr 默認是rr weight越大接受的請求越多 ip_hash會話保持 #第三方算法 url_hash web緩存 fair 根據後端rs 響應速度分配請求後端
server domainname|ip:port weight=2 max_fails=2 fail_timeout=20s#fail_timeout檢測失敗後多少時間後再次檢測 ip_hash時weight無用緩存
server domainname|ip:port backup;#高可用 ip_hash不支持服務器
}負載均衡
location / { proxy_pass http://localhost:8000; proxy_set_header Host $host;#後端RS配置有多個虛擬主機時將用戶請求的真實host反應給rs proxy_set_header X-Forwarded-For $remote_addr;#讓後端RS服務器記錄訪問用戶的真實IP apache web LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined nginx web 會自動記錄真實IP }