简单的负载均衡
架构图
client -- > reverser server --> server 129
--> server 130
准备
1.配置3台虚拟机,操作系统使用centos7.0+,ip如下
- 192.168.154.129
- 192.168.154.130
- 192.168.154.132
2 一个纯静态html文件-index.html
<html>
<head>
<title>hi nlb!</title>
</head>
<body>
hi~, this is 192.168.154.129!
</body>
</html>
3.通用配置
# 在3台上安装nginx
yum install -y nginx
# 关闭防火墙
setenforce 0
chkconfig iptables off
firewall-cmd --state
# 稍后会修改服务器目录为/nlb
mkdir /nlb
vim /nlb/index.html # 根据ip修改准备index.html
代理机配置(反向代理+负载均衡配置)
- vim /etc/nginx/nginx.conf
upstream nlb {
ip_hash;
server 192.168.154.129:6180;
server 192.168.154.130:6180;
}
server {
listen 6180 default_server;
listen [::]:6180 default_server;
server_name www.nlb.com;
root /nlb/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://nlb;
proxy_set_header Host $host:server_port;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
- 启动服务
service nginx start
129服务器上的配置
- vim /etc/nginx/nginx.conf
server {
listen 6180 default_server;
listen [::]:6180 default_server;
server_name _;
root /nlb;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
- 启动服务
service nginx start
130服务器上的配置
- vim /etc/nginx/nginx.conf
server {
listen 6180 default_server;
listen [::]:6180 default_server;
server_name _;
root /nlb;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
- 启动服务
service nginx start