Nginx + Tomcat Windows下的负载均衡配置

一、为什么需要对Tomcat服务器做负载均衡?
    Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果超过500的并发数会出现Tomcat不能响应新的请求的情况,严重影响网站的运行。同时如果访问量非常大的情况下,Tomcat的线程数会不断增加。因此会占据大量内存,严重时出现内存溢出的现象,这时需要重启Tomcat以释放内存,阻断了网站的运行。
    所以对Tomcat做负载均衡便很有必要。目前可以和Tomcat做负载均衡的主流服务器是Apache,但是Nginx由于功能多、配置简单等优点成为很多负载均衡服务器的首选。Nginx的并发数可达到50000,所以理论上可以和Tomcat以1:100的比例来配置,这边可以很好的解决网站并发瓶颈问题。

二、Nginx+Tomca在Windows下负载均衡的配置:
    先秀一下我的环境: Nginx1.6.3 + Tomcat 6.0(2个)、Win 10。
    下面是配置过程:
    1、将nginx-1.6.3.rar直接解压到D:盘。
    2、将apache-tomcat-6.0.26.rar解压到D:盘,既然是做负载均衡将tomcat拷贝两份,分别命名tomcat_1、tomcat_2。
    3、将要发布的项目部署分别部署到两个Tomcat根目录下的webapps下,保证两个Tomcat下的项目名相同。
    4、修改tomcat_2的配置文件,该配置文件位于/conf下,文件名为server.xml, 修改其中的<Server port="8005" shutdown="SHUTDOWN">            为<Server port="8006" shutdown="SHUTDOWN">,修改其中的

<Connector port="8080 protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"/>

<Connector port="8088 protocol="HTTP/1.1"
        connectionTimeout="20000"
redirectPort="8443"/>
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

(ajp是一个协议,tomcat可以用来服务器集群,也可以在csdn找到更详细的ajp资料)为

<Connector port="7009" protocol="AJP/1.3" redirectPort="8443" />

5、修改Nginx配置文件nginx.conf,该文件位于Nginx根目录下的/conf目录下
    a、在#gzip on;后面加入下面配置:

upstream localhost {
    server localhost:8080 weight=5;
server localhost:8080 weight=5;
ip_hash;
}

其中server localhost:8080为tomcat_1的启动地址, server localhsot:8088为tomcat_2的启动地址,ip_hash用于做session同步。 
    b、修改

location / {
root html;
index index.html index.htm;
}

改为:

location / {
root html;
index index.html index.htm;
proxy_pass http://localhost;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}

其中proxy_pass参数和upstream localhost{}对应。

经过上面这些步骤,负载均衡配置完成,下面分别启动tomcat_1、tomcat_2,然后双击nginx根目录下nginx.exe文件或者使用start nginx启动(关闭是: nginx -s stop),打开浏览器,输入地址: http://localhost便可看到tomcat的首页了。

最新文章

  1. 在SharePoint中创建可自定义属性的文件夹
  2. 2-2. Initializing Objects with Initializer Lists
  3. sphinx网址
  4. Matlab稀疏矩阵
  5. windows phone 操作 http异步返回结果
  6. hdu 2519 新生晚会 排列组合
  7. 【noip模拟】局部最小值
  8. hustOJ 添加 golang 支持
  9. oracle查询查询出某字段为空后前台不显示的小测试1
  10. mysql时间戳转换
  11. win10传奇手册CHM打开无法阅读解决
  12. python中切片的理解
  13. Python- 索引 B+数 比如书的目录
  14. SPI OLED 驱动
  15. Actions对Element的一些操作解析
  16. Codeforces Round #535 (Div. 3) 解题报告
  17. nodejs直接调用grunt(非调用批处理)
  18. Asp.net MVC]Asp.net MVC5系列——实现编辑、删除与明细信息视图
  19. vue2.0 keep-alive 最佳实战(转载)
  20. EF实体框架处理实体之间关联关系与EF延迟机制(下)

热门文章

  1. linux 压缩包覆盖问题
  2. py-faster-rcnn几个辅助脚本
  3. 【BZOJ-4380】Myjnie 区间DP
  4. javascript判断数字是integer还是float
  5. 项目中angular js的接口url统一管理
  6. 将list集合的元素按照添加顺序的倒序进行排列取出
  7. sql server中对xml进行操作
  8. &lt;&lt;编程之美&gt;&gt;1.2读后有感
  9. 常见ES5方法
  10. Java 时间的表示