upstream举例
upstream backend {
server backend1.ecample.com weight = 5; # wwight 代表权重
server backend2.ecample.com:8080;
server unix:/tmp/backend3;
server backend1.com backup; # backup 代表备份
server backend2.com:8080; backup;
}
后端服务器在负载均衡调度中的状态
演示:
配置.conf文件
upstream test {
server 192.168.96.188:8001 down;
server 192.168.96.188:8002; backup; # 预留的备份服务器
server 192.168.96.188:8003; max_fails=1 fail_timeout=10s; # 经过max_fails失败后,服务暂停的时间
}
server {
listen 80;
server_name localhost www.test.com;
#charset koi8-r;
#access_log /usr/local/nginx/logs/test_proxy.access.log main;
resolver 8.8.8.8;
location / {
proxy_pass http://test;
include proxy_params;
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
访问目标主机192.168.96.146。刷新之后,只能访问到server3
将192.168.96.188的这台主机server3这个服务停掉。然后再访问192.168.96.146。这时候只能访问到备份的server2