nginx反向代理实战之轮询、Ip_hash、权重

实验环境

192.168.200.111 web1 centos7
192.168.200.112 web2 centos7
192.168.200.113 wev3 centos7

三台主机环境:

都安装Nginx、以192.168.200.111为主环境实验

web3(192.168.200.113)操作为例

[root@web3 ~]# cd /usr/local/nginx/conf/
[root@web3 conf]# vim nginx.conf

user  nginx nginx;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;
pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  10240;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  main;
    sendfile        on;
    client_header_timeout 60;
    client_body_timeout 60;
    server_tokens off;
    #tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
  #upstream用于定义负载均衡组
    upstream web_pool {
    server 192.168.200.113    weight=5;
    server 192.168.200.112    weight=5;
    server 192.168.200.111    weight=5 backup;
   }
    server {
        listen       80;
        server_name  www.etiantion.org;
        charset utf-8;
    location / {
        root html;
        index index.html index.html;
        proxy_pass http://web_pool;
}
   #status用于采集用户访问数量
    location /status {
        stub_status on;
        access_log off;
    }
   }
  }

设置映射关系

[root@web3 ~]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.200.113 www.etiantian.org

测试负载均衡器到web服务器之间能否ping通

在nginx主配置文件中我们未指定算法,所以默认时轮询算法

轮询算法

[root@web3 conf]# curl 192.168.200.113
192.168.200.111
[root@web3 conf]# curl 192.168.200.113
192.168.200.112
[root@web3 conf]# curl 192.168.200.113
192.168.200.111
[root@web3 conf]# curl 192.168.200.113
192.168.200.112
[root@web3 conf]# curl 192.168.200.113
192.168.200.111
[root@web3 conf]# curl 192.168.200.113
192.168.200.112

ip-hash算法

 #upstream用于定义负载均衡组
    upstream web_pool {
    ip_hash;
    server 192.168.200.111    weight=5;
    server 192.168.200.112    weight=5;
    server 192.168.200.113    weight=5 ;       #此时backup要删除,用backup会报错
   }

[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.112

权重算法:权值越大分配几率越大,一般按权值比来分配

 #upstream用于定义负载均衡组
    upstream web_pool {
    server 192.168.200.113    weight=1;
    server 192.168.200.112    weight=5;
    server 192.168.200.111    weight=5 backup;
   }

[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.111
[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.112
[root@web conf]# curl 192.168.200.113
192.168.200.112

原文地址:https://www.cnblogs.com/CMX_Shmily/p/11526218.html