Nginx+tomcat集群环境搭建

Nginx版本:1.12.2;

下载地址:http://nginx.org/en/download.html

 

Tomcat版本:6.0.39

下载地址:http://tomcat.apache.org/download-60.cgi

 

一、配置nginx

1、  在C盘根目录建立nginx文件夹,把下载的nginx发布包nginx-1.12.2.zip解压到该目录。

2、  C:\nginx\nginx-1.5.12目录结构

3、  启动nginx

windows下启动nginx非常简单,双击运行nginx.exe即可(注意有的电脑可能不行,双击以后,任务管理器进程里没有ngnix的进程,这个时候就要通过cmd窗口来执行ngnix.exe文件)。Nginx默认运行在80端口,检查nginx是否启动我们只需要在浏览器中输入http://localhost便可看到如下页面,说明我们nginx已经启起来了。

1、  停止nginx

如果需要停止nginx,需要打开一个命令行窗口,进入nginx解压的目录,也就是进入nginx.exe文件所在的目录,输入命令nginx –s stop 便可停止nginx。

 

二、集群配置

1、  配置tomcat

在D盘根目录建立tomcat文件夹,解压2份tomcat6.0.39发布包到该目录下,分别命名为tomcat01,tomcat02。为了便于观察我们访问的是哪个tomcat,我们修改tomcat01的D:\tomcat\tomcat01\webapps\ROOT\index.html中


然后修改tomcat02里的index.html文件


2个tomcat我们在同一台计算机上,为了让2个tomcat的端口不冲突,我们把tomcat02的D:\tomcat\tomcat02\conf\server.xml中

[html]  view plain  copy
  1. <Server port="8005" shutdown="SHUTDOWN">  
改为

[html]  view plain  copy
  1. <Server port="8105" shutdown="SHUTDOWN">  

[html]  view plain  copy
  1. <Connector port="8080" protocol="HTTP/1.1"   
  2.                connectionTimeout="20000"   
  3.                redirectPort="8443" />  
改为

[html]  view plain  copy
  1. <Connector port="8180" protocol="HTTP/1.1"   
  2.                connectionTimeout="20000"   
  3.                redirectPort="8543" />  

[html]  view plain  copy
  1. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  
改为

[html]  view plain  copy
  1. <Connector port="8109" protocol="AJP/1.3" redirectPort="8543" />  


[html]  view plain  copy
  1.   
2、  配置nginx

nginx.conf

[html]  view plain  copy
  1. #Nginx所用用户和组,window下不指定   
  2. #user  niumd niumd;   
  3.   
  4. #工作的子进程数量(通常等于CPU数量或者2倍于CPU)  
  5. worker_processes  2;  
  6.   
  7. #错误日志存放路径  
  8. #error_log  logs/error.log;  
  9. #error_log  logs/error.log  notice;  
  10. #error_log  logs/error.log  info;  
  11.   
  12. #指定pid存放文件  
  13. #pid        logs/nginx.pid;  
  14.   
  15.   
  16. events {  
  17.     #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  
  18.     #use epoll;  
  19.       
  20.     #允许最大连接数  
  21.     worker_connections  1024;  
  22. }  
  23.   
  24.   
  25. http {  
  26.     include       mime.types;  
  27.     default_type  application/octet-stream;  
  28.   
  29.     #定义日志格式   
  30.     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  31.                       '$status $body_bytes_sent "$http_referer" '  
  32.                       '"$http_user_agent" "$http_x_forwarded_for"';  
  33.   
  34.     access_log  logs/access.log  main;  
  35.       
  36.     client_header_timeout  3m;  
  37.     client_body_timeout    3m;  
  38.     send_timeout           3m;  
  39.       
  40.     client_header_buffer_size    1k;  
  41.     large_client_header_buffers  4 4k;  
  42.       
  43.   
  44.     sendfile        on;  
  45.     tcp_nopush      on;  
  46.     tcp_nodelay     on;  
  47.   
  48.     #keepalive_timeout  0;  
  49.     keepalive_timeout  65;  
  50.   
  51.     #gzip  on;  
  52.       
  53.     upstream localhost {    
  54.           #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。    
  55.           #同一机器在多网情况下,路由切换,ip可能不同    
  56.           ip_hash;     
  57.           server localhost:8080;    
  58.           server localhost:8180;    
  59.          }    
  60.   
  61.   
  62.     server {  
  63.         listen       80;  
  64.         server_name  localhost;  
  65.   
  66.         #charset koi8-r;  
  67.   
  68.         #access_log  logs/host.access.log  main;  
  69.   
  70.         location / {  
  71.             proxy_connect_timeout   3;    
  72.             proxy_send_timeout      30;    
  73.               proxy_read_timeout      30;    
  74.             proxy_pass http://localhost;    
  75.         }  
  76.   
  77.         #error_page  404              /404.html;  
  78.   
  79.         # redirect server error pages to the static page /50x.html  
  80.         #  
  81.         error_page   500 502 503 504  /50x.html;  
  82.         location = /50x.html {  
  83.             root   html;  
  84.         }  
  85.   
  86.     }  
  87. }  

3、  查看反向代理配置结果

启动nginx、tomcat01、tomcat02。

浏览器输入http://localhost便看到tomcat01的管理界面,如下图。



然后透明停止tomcat02,刷新页面,nginx自动帮我们切换到tomcat02了,如下图。


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李晓LOVE向阳

你的鼓励是我持续的不断动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值