一、实验环境
主机名与IP
3台VM虚拟机,1台做负载均衡,2台做RS。
HOSTNAME | IP | 说明 |
lb01 | 192.168.5.210 | 主负载均衡器 |
web01 | 192.168.5.212 | web01服务器 |
web02 | 192.168.5.213 | web02服务器 |
软件:
系统:CentOS 6.9 x86_64
软件:nginx-1.15.2.tar.gz(http://nginx.org/download/nginx-1.15.2.tar.gz)
二、安装Nginx软件
3台服务器均安装Nginx。
1、安装依赖包
yum -y install openssl openssl-devel pcre pcre-devel
2、安装nginx软件包
useradd nginx -s /sbin/nologin -M tar zxvf nginx-1.15.2.tar.gz ./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make make install
三、配置一个虚拟主机
1、配置明细
在两台web服务器上操作,配置如下:
[root@web01 nginx-1.15.2]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } 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; keepalive_timeout 65; server { listen 80; server_name www.test.com; location / { root html/test; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
2、创建测试文件
mkdir /usr/local/nginx/html/test echo "www.test.com 212" > /usr/local/nginx/html/test/index.html
3、测试结果
在windows客户端测试,需先开通服务器防火墙的80端口,或关闭防火墙。
四、负载均衡配置
在lb01服务器上操作,配置如下:
[root@lb01 nginx-1.15.2]# cat /usr/local/nginx/conf/nginx.conf worker_processes 1; events { worker_connections 1024; } 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; keepalive_timeout 65; upstream test_server_pools { server 192.168.5.212:80 weight=1; server 192.168.5.213:80 weight=1; } server { listen 80; server_name www.test.com; location / { proxy_pass http://test_server_pools; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
参数说明:
upstream:Nginx负载均衡依赖于 ngx_http_upstream_module 模块
proxy_pass:proxy_pass指令属于 ngx_http_proxy_module 模块
五、测试
1、修改hosts地址进行测试,Windows系统hosts文件路径:C:\Windows\System32\drivers\etc
192.168.5.210 www.test.com
2、浏览器端采用缓存刷新页面,请求配均匀的分配到后端服务器。