Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

一、安装Tomcat和Nginx

首先安装两个apache-tomcat-8.0.41,下载地址:http://tomcat.apache.org

并安装一个nginx-1.13.0,下载地址http://nginx.org/en/download.html

都是绿色版,直接解压就能用,不需要进行环境变量之类的配置的。

这里碰到个小问题:公司电脑环境变量配的是jdk6,所以我的Tomcat8启动的时候黑窗口一闪而过,JDK版本不匹配的原因,把jdk6换成jdk8之后,Tomcat8正常启动。

二、修改Tomcat的端口号

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

我的第一个Tomcat是默认的,只修改第二个Tomcat,确保两个Tomcat能同时启动,需要修改下面三处的端口号(O(∩_∩)O我只是在默认多口号加了1~):

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

三、修改Tomcat的默认页面,用于识别访问的是哪个Tomcat

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

另一个Tomcat这里添加的是=====Tomcat1

四、修改Nginx的配置

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

配置内容

 #user  nobody;
worker_processes 1;#工作进程的个数,一般与计算机的cpu核数一致 #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;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,
#对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,
#以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65;#长连接超时时间,单位是秒 #gzip on;#启用Gizp压缩 #服务器的集群
upstream netitcast.com { #服务器集群名字
server 127.0.0.1:8080 weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
server 127.0.0.1:8081 weight=2;
} #当前的Nginx的配置
server {
listen 80;#监听80端口,可以改成其他端口
server_name localhost;#当前服务的域名 #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
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;
# }
#} }

其实也就修改了以下两个地方:

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

五、开始测试

通过startup.bat直接启动两个Tomcat。会出现两个黑窗口,里面有Tomcat的启动日志。

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

接着通过Nginx目录下的nginx.exe启动Nginx。

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

六、测试结果

如下图所示,浏览器中访问localhost,不断刷新,访问的Tomcat会在1和2中来回切换,切换的概率是由所配置的权重决定的。

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

Nginx + Tomcat 在 Windows7 上搭建负载均衡集群

集群就涉及到session共享的问题,参考http://www.linuxidc.com/Linux/2016-09/135550.htm

上一篇:Sun jdk, Openjdk, Icedtea jdk关系


下一篇:the example of dlsym