环境准备
主机 | IP |
control01 | 192.168.29.128 |
nginx01 | 192.168.29.101 |
nginx02 | 192.168.29.102 |
nginx03 | 192.168.29.103 |
一、配置反向代理
control01 nginx配置
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { proxy_pass http://192.168.29.101; # proxy_pass关键字 #root html; #index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
执行systemctl reload nginx使配置生效
访问control01的nginx
这里提前修改nginx01 主机 index页面,直接显示反向代理效果。
二、配置负载均衡
control01 配置
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream example{ server 192.168.29.101:80; server 192.168.29.102:80; } server { listen 80; server_name localhost; location / { proxy_pass http://example; #root html; #index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
执行systemctl reload nginx使配置生效
访问control01的nginx查看效果
刷新页面显示
这样就实现了一个简单的负载均衡配置
注意:这里的负载均衡其实是对一组服务器进行了反向代理,默认的规则是轮询;
upstream 跟 server块是同一级别的,都在http块之内;
upstream 的名字跟proxy_pass后面的名字对应;
三、负载均衡策略
control01 配置
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream example{ server 192.168.29.101 weight 5 down; server 192.168.29.102 weight 3;
server 192.168.29.103 weight 1 backup; } server { listen 80; server_name localhost; location / { proxy_pass http://example; #root html; #index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
weight:权重,给server组配置不同的权重,权重越大的调度到的概率也就越高
down: server下线,配置了之后就不会调度到该server上了
backup:备份server,只有当其他server都不可用时才可调度,有可用server时,不可接受调度。
以上调度策略可以组合使用,实际生产环境很少使用。
标签:index,负载,http,192.168,server,nginx,html,反向 From: https://www.cnblogs.com/ggborn-001/p/17024963.html