nginx反向代理:用戶請求nginx代理服務器然后代理服務器將用戶請求轉為服務器再由nginx代理服務器將服務器的響應反應給用戶。
lvs負載均衡:用戶請求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.
在http語塊內定義:
upstream web_pools{
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 }