1. 什么是负载均衡
负载均衡是建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
负载均衡英文名称为Load Balance,其意思就是分摊到多个操作单元上进行执行,例如Web服务器、FTP服务器、企业关键应用服务器和其它关键任务服务器等,从而共同完成工作任务。
2. 测试环境
nginx负载均衡服务器:192.168.1.196
tomcat1服务器:192.168.1.194
tomcat2服务器:192.168.1.195如下图所示:

nginx作为负载均衡服务器,用户请求先到达nginx服务器,再由nginx根据负载配置将请求转发至 tomcat1和tomcat2服务器。
3. 负载均衡配置
根据测试环境的要求在nginx.conf文件中配置负载均衡,如下:
#proxy_server
#proxy_server表示要转发的服务器名字proxy_server
upstream proxy_server {
#server表示要转发到哪个服务器,如果有多个以分号隔开
server 192.168.1.194:8080 weight=10;
server 192.168.1.195:8080 weight=20;
}
server {
#坚挺的端口
listen 80;
#访问域名,可以设置多个,以空格分开
server_name localhost;
# / 表示拦截所有请求
location / {
#拦截的请求都转发到proxy_server
proxy_pass http://proxy_server;
root html;
index index.html index.htm;
}
}注:weight是一个转发请求的权重比,weight值越高则权重越高,转发到该服务器的几率就越大
把以上这些配置信息加入到nginx反向代理服务器的配置文件nginx.conf中,具体信息如下:
#user nobody;
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 {
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#proxy_server
upstream proxy_server {
server 192.168.1.194:8080 weight=10;
server 192.168.1.195:8080 weight=20;
}
server {
#坚挺的端口
listen 80;
#访问域名,可以设置多个,以空格分开
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# / 表示拦截所有请求
location / {
##配置代理服务器,拦截的请求都转发到proxy_server
proxy_pass http://proxy_server;
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}保存nginx.conf文件,通过./nginx -t测试配置是否正确
然后进入nginx的安装目录/usr/local/nginx/sbin目录下执行./nginx -s reload命令重启nginx。
如果不知道怎么重启nginx,参考:2-nginx的启动和停止
4. 测试
打开客户端,输入192.168.1.196,由nginx负载均衡将请求转发到tomcat服务器。通过观察tomcat的访问日志或tomcat访问页面的ip地址即可知道当前请求由哪个tomcat服务器受理。


为了突出转发效果,可以按F5不断刷新页面,查看页面的ip地址。
补充:在下一篇中,将介绍keepalived原理和工作机制
















