一、负载均衡作用:缓解用户访问网站服务器的压力
环境准备:
主机名 | IP地址 | 部署软件 |
---|---|---|
lb01 | 10.0.0.5 | nginx |
web01 | 10.0.0.7 | nginx |
web02 | 10.0.0.8 | nginx |
web03 | 10.0.0.9 | nginx |
二、 web服务器网站文件配置信息
[root@web1 ~]# cat /etc/nginx/conf.d/mmm.conf
server {
listen 80;
server_name www.newboy.com;
location / {
root /html/mmm/;
index index.html;
}
}
[root@web2 ~]# cat /etc/nginx/conf.d/mmm.conf
server {
listen 80;
server_name www.newboy.com;
location / {
root /html/mmm/;
index index.html;
}
}
[root@web3 ~]# cat /etc/nginx/conf.d/mmm.conf
server {
listen 80;
server_name www.newboy.com;
location / {
root /html/mmm/;
index index.html;
}
}
三、web服务器存放网站文件目录
[root@web1 ~]# cat /html/mmm/index.html
web-01
[root@web2 ~]# cat /html/mmm/index.html
web-02
[root@web3 ~]# cat /html/mmm/index.html
web-03
四、web服务器/etc/nginx/nginx.conf文件配置
[root@web1 ~]# cat /etc/nginx/nginx.conf
#把没用的注释文件删掉之后
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/mmm.conf; #这里修改为网站配置文件的具体文件名
}
[root@web1 ~]# scp /etc/nginx/nginx.conf 10.0.0.8:/etc/nginx/
[root@web1 ~]# scp /etc/nginx/nginx.conf 10.0.0.9:/etc/nginx/
五、重启 nginx服务
systemctl restart nginx
六、利用负载均衡服务器测试web页面是否能够访问
[root@lb1 ~]# curl -H host:www.newboy.com 10.0.0.7
web-01
[root@lb1 ~]# curl -H host:www.newboy.com 10.0.0.8
web-02
[root@lb1 ~]# curl -H host:www.newboy.com 10.0.0.9
web-03
七、编写负载均衡服务器配置文件信息
[root@lb1 ~]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
include /etc/nginx/mime.types;
upstream oldboy { #定义主机组名称
server 10.0.0.7:80; #web节点
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server { #网站配置文件
listen 80;
server_name www.newboy.com;
location / {
proxy_pass http://oldboy; #调用主机组
}
}
}
八、重启nginx服务
systemctl restart nginx
九、配置host解析
10.0.0.5 www.newboy.com
十、网页测试访问,刷新页面