一台nginx的负载均衡服务器(172.25.254.131)
两台安装httpd作为web端
准备工作
安装nginx
yum -y install gcc openssl-devel pcre-devel useradd -s /sbin/nologin nginx cd ~/nginx-1.10.1/ ./configure \ >--prefix=/usr/local/nginx \ >--user=nginx \ >--group=nginx \ >--with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-http_dav_module #在安装pcre的时候,如果使用的是源码安装,则在这里需要只用pcre的位置 make && make install
ln -s /usr/local/nginx/sbin/nginx /sbin/
nginx -t
nginx
nginx -s reload
安装appache
[root@web1 ~]# yum -y install httpd [root@web1 ~]# systemctl restart httpd [root@web1 ~]# netstat -ntlp |grep 80 tcp6 0 0 :::80 :::* LISTEN 80049/httpd [root@web1 ~]# echo 172.25.254.134>>/var/www/html/index.html [root@web1 ~]# curl 172.25.254.134 172.25.254.134 [root@web1 ~]# firewall-cmd --add-port=80/tcp --permanent [root@web1 ~]# firewall-cmd --reload [root@web1 ~]# firewall-cmd --list-all ports: 80/tcp [root@web2 ~]# yum -y install httpd [root@web2 ~]# systemctl restart httpd [root@web2 ~]# netstat -ntlp |grep 80 tcp6 0 0 :::80 :::* LISTEN 80049/httpd [root@web1 ~]# echo 172.25.254.135>>/var/www/html/index.html [root@web1 ~]# curl 172.25.254.135 172.25.254.135 [root@web1 ~]# firewall-cmd --add-port=80/tcp --permanent [root@web1 ~]# firewall-cmd --reload [root@web1 ~]# firewall-cmd --list-all ports: 80/tcp
开始配置nginx的负载均衡
一般2000万pv以下的负载均衡都可以使用nginx实现,依赖于Nginx_http_upstream_module模块。所支持的代理方式有proxy_pass,fastcgi_pass,memcached_pass.
upstream模块应放于nginx.conf配置的http{}内。默认的算法是wrr权重轮询
参数:weight max_fails(根据应用场景,小的话,用户体验度高,但是要求节点足够多,同时在负载大于80%后,不会向该节点扔请求,大的话可以提高服务器的利用率 ),backup,fail_timeout,down
[root@lb01 ~]# cd /usr/local/nginx/conf/
[root@lb01 conf]# egrep -v "#|^$" nginx.conf.default
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 / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
[root@lb01 conf]# egrep -v "#|^$" nginx.conf.default >nginx.conf
[root@lb01 conf]# vim nginx.conf
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; upstream web_pools{
#可以跟ip_hash;这样,backup不能使用,权重也没有效果,一个客户端永远访问一台 server 172.25.254.134:80 weight=5; server 172.25.254.135:80 weight=5; server 172.25.254.158:80 weight=5 backup; #nginx自带的一个高可用 }
#nginx自带判断功能,单节点故障,会自己剔除故障节点,节点故障修复,又会自己添加进来 server { listen 80; server_name www.lb01test.com; location / { root html; index index.html index.htm;
proxy_set_header Host $host; proxy_pass http://web_pools; } } }
[root@lb01 conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@lb01 conf]# nginx -s reload
访问:
[root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.134 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.134 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.134 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.134 [root@lb01 conf]# curl http://www.lbtest.com 172.25.254.135