Windows下Nginx与Tomcat负载均衡搭建
初入职场时便听闻“负载均衡”之名,但因工作接触有限,理解尚浅。出于好奇,近日查阅资料后,在个人笔记本上搭建了一个简易案例。虽受限于工作时间与居家条件,无法还原生产环境,但旨在深入理解其核心思想。
简而言之,当系统面临巨大访问流量导致响应迟缓或服务宕机时,负载均衡通过流量分流机制来缓解这一状况,确保服务稳定性。

首先需安装两个Tomcat实例。可以是同一版本的复制副本,也可下载不同版本,笔者选择下载了两个不同版本。下载地址为:https://tomcat.apache.org/download-80.cgi
此处以8.0版本为例,选取任意非过时的稳定版本即可。
接着启动两个Tomcat。为避免端口冲突,需将其中一个的默认端口从8080修改为9080。配置完成后,打开CMD命令行窗口。假设Tomcat分别位于D:\software\apache-tomcat-8.5.24等目录下,执行相应启动命令。成功启动后,会弹出新的窗口并显示启动日志。


在浏览器地址栏输入http://localhost:9080/,若出现Tomcat欢迎界面,则表明该实例启动成功。另一实例亦按相同步骤操作即可。


图1展示的是Tomcat 8界面,图2展示的是Tomcat 7界面。
随后安装Nginx,笔者选用稳定版,下载地址为:http://nginx.org/download/nginx-1.12.2.zip,解压后即可直接使用。

启动Nginx前,必须对其进行配置以实现负载均衡功能。进入conf文件夹,找到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;
#以下四行是新添加的,两个IP是两个tomcat的访问地址,weight表示分给该服务器的请求比重,两个都是1,则按照1:1来分配,
upstream netitcast.com{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:9080 weight=2;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#一下两行是进行修改的,http://netitcast.com和上面添加的要保持一致
location / {
proxy_pass http://netitcast.com;
proxy_redirect default;
}
#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;
# }
#}
}
再次打开CMD窗口,进入Nginx目录,执行命令:start nginx 以启动服务。随后在浏览器输入网址:http://localhost/index.jsp,反复刷新访问,会发现上方显示的图1和图2交替出现。由于配置中weight比例为1:2,即9080端口的权重较大,因此访问到图一(9080端口)的概率约为三分之二,而访问到图二(8080端口)的概率约为三分之一。
当然,这仅是一个基础示例。在生产环境中,还需解决会话保持等复杂问题。待后续理解加深后再行补充。撰写此文旨在巩固自身对负载均衡的理解,愿与大家共同进步,加油!!!