proxy_pass、upstreamversusresolver
upstream
upstreamAn upstream service configuration item will be created,For handing overproxy_pass Forwardip.
upstream x.cn { server 192.168.192.134:80; }
Only ifproxy_passsWhen calling,upstreamWill take effect:
upstream x.cn { server 192.168.192.134:80; } server { listen 80; server_name 1.cn; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/1.cn; location / { proxy_pass http://x.cn; } }
When accessing the server1.cnTime,Read proxy_passProxy configuration,Getupstream ip,将请求Forward到 192.168.192.134
hostforx.cn
proxy_passReverse proxy
byproxy_pass,Can proxy requests to designated services,E.g:
server { listen 80; server_name localhost; location /proxy { proxy_pass http://x.cn; } }
When visitinglocalhost/proxyTime,nginxWill automatically resolvex.cn ip,Proxy request to Parsedipin.
whenx.cnUnable to resolve or not setdns服务器Time,Will report an error
nginx: [emerg] host not found in upstream "x.cn" in /www/server/panel/vhost/nginx/1.cn.conf:9
when没有设置upstreamTime,proxy_pass将bydnsServer resolutionip,Add one by defaultupstream ip,用于实现Forward请求.
反向Proxy configuration项:
#Proxy configuration参数 proxy_connect_timeout 180; proxy_send_timeout 180; proxy_read_timeout 180; proxy_set_header Host $host; proxy_set_header X-Forwarder-For $remote_addr;
resolve
upstream 1.cn { server 192.168.192.134:80; } server { listen 80; server_name 1.cn; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/1.cn; resolver 114.114.114.114; location / { set $testCn 1.cn; proxy_pass http://$testCn; } }
when设置resolveRear, nginxWill ignore its own settingsdns,Nativehosts,直接byresolveofdnsServer dynamic acquisitionip,用于Forward
只有by变量设置域名,resolveofdns解析Will take effect
upstreamLoad balancing
On top,We have learnedupstream可以设置代理服务器ofip,Forward请求,This agentip,是可以设置多个of,E.g:
upstream 1.cn { server 192.168.192.134:80; server 127.0.0.1:80; server 192.168.192.139:80; } server { listen 80; server_name x.cn; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/1.cn; location / { set $testCn 1.cn; proxy_pass http://$testCn; } }
我们同Time在3Servers,Add site1.cn,并且配置不一样of数据显示,Used to distinguish.
note: in casenginxProxy local,Please do not visit the same domain,E.g,Local access1.cn,会造成Forward到Native1.cn->再Forward到Native1.cn,Cause an error
所以我在这边of配置项改for了访问主服务器ofx.cn,Proxy to main server,And other2台副服务器of1.cnin.
This is achievednginxLoad balancing
upstreamWeight load
by设置 weight,即可设置权重区分Load balancing.E.g:
upstream 1.cn { server 192.168.192.134:80 weight=2; server 127.0.0.1:80 weight=5; server 192.168.192.139:80; } server { listen 80; server_name x.cn; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/x.cn; location / { set $testCn 1.cn; proxy_pass http://$testCn; } }
设置权重Rear,nginx在代理Time将尽可能平均of前提下by权重大小进行分配代理服务器.E.ga=5,b=3,c=2
请求代理顺序可能for abacabacaba,在尽量平均请求of前提下increase节点of请求数.
upstream ip hashModulo grouping
upstream 1.cn { ip_hash; server 192.168.192.134:80 weight=2; server 127.0.0.1:80 weight=5; server 192.168.192.139:80; } server { listen 80; server_name x.cn; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/x.cn; location / { set $testCn 1.cn; proxy_pass http://$testCn; } }
increaseip_hash配置项Rear,weightWill fail,nginx将by请求ipModulo,sameipof请求将分配到固定of一Servers上
upstream backup
when其他上游节点全部出现异常Time,nginx才会将请求Forward到backup:
upstream 1.cn { server 192.168.192.134:80; server 127.0.0.1:80 backup; server 192.168.192.139:80; } server { listen 80; server_name x.cn; index index.php index.html index.htm default.php default.htm default.html; root /www/wwwroot/x.cn; location / { set $testCn 1.cn; proxy_pass http://$testCn; } }