實驗環境
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