您的位置:首页 > 运维架构 > Tomcat

Nginx+Tomcat搭建高性能负载均衡集群--Windows本地测试版

2017-05-16 12:33 776 查看
一、安装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的端口号



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







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





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

四、修改Nginx的配置



配置内容

1 #user  nobody;
2 worker_processes  1;#工作进程的个数,一般与计算机的cpu核数一致
3
4 #error_log  logs/error.log;
5 #error_log  logs/error.log  notice;
6 #error_log  logs/error.log  info;
7
8 #pid        logs/nginx.pid;
9
10
11 events {
12     worker_connections  1024;#单个进程最大连接数(最大连接数=连接数*进程数)
13 }
14
15
16 http {
17     include       mime.types;#文件扩展名与文件类型映射表
18     default_type  application/octet-stream;#默认文件类型
19
20     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
21     #                  '$status $body_bytes_sent "$http_referer" '
22     #                  '"$http_user_agent" "$http_x_forwarded_for"';
23
24     #access_log  logs/access.log  main;
25
26     sendfile        on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,
27                #对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,
28                #以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
29     #tcp_nopush     on;
30
31     #keepalive_timeout  0;
32     keepalive_timeout  65;#长连接超时时间,单位是秒
33
34     #gzip  on;#启用Gizp压缩
35
36     #服务器的集群
37     upstream  netitcast.com {  #服务器集群名字
38         server    127.0.0.1:8080  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。
39         server    127.0.0.1:8081  weight=2;
40     }
41
42     #当前的Nginx的配置
43     server {
44         listen       80;#监听80端口,可以改成其他端口
45         server_name  localhost;#当前服务的域名
46
47         #charset koi8-r;
48
49         #access_log  logs/host.access.log  main;
50
51         location / {
52             root   html;
53             index  index.html index.htm;
54         proxy_pass http://netitcast.com; 55             proxy_redirect default;
56         }
57
58         #error_page  404              /404.html;
59
60         # redirect server error pages to the static page /50x.html
61         #
62         error_page   500 502 503 504  /50x.html;
63         location = /50x.html {
64             root   html;
65         }
66
67         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
68         #
69         #location ~ \.php$ {
70         #    proxy_pass   http://127.0.0.1; 71         #}
72
73         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
74         #
75         #location ~ \.php$ {
76         #    root           html;
77         #    fastcgi_pass   127.0.0.1:9000;
78         #    fastcgi_index  index.php;
79         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
80         #    include        fastcgi_params;
81         #}
82
83         # deny access to .htaccess files, if Apache's document root
84         # concurs with nginx's one
85         #
86         #location ~ /\.ht {
87         #    deny  all;
88         #}
89     }
90
91
92     # another virtual host using mix of IP-, name-, and port-based configuration
93     #
94     #server {
95     #    listen       8000;
96     #    listen       somename:8080;
97     #    server_name  somename  alias  another.alias;
98
99     #    location / {
100     #        root   html;
101     #        index  index.html index.htm;
102     #    }
103     #}
104
105
106     # HTTPS server
107     #
108     #server {
109     #    listen       443 ssl;
110     #    server_name  localhost;
111
112     #    ssl_certificate      cert.pem;
113     #    ssl_certificate_key  cert.key;
114
115     #    ssl_session_cache    shared:SSL:1m;
116     #    ssl_session_timeout  5m;
117
118     #    ssl_ciphers  HIGH:!aNULL:!MD5;
119     #    ssl_prefer_server_ciphers  on;
120
121     #    location / {
122     #        root   html;
123     #        index  index.html index.htm;
124     #    }
125     #}
126
127 }


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



五、开始测试

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



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



六、测试结果

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





集群就涉及到session共享的问题,参考http://www.linuxidc.com/Linux/2016-09/135550.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: