• Nginx+tomcat集群环境搭建(Windows下)


    实验环境

      windows xp sp3

      Nginx版本:1.5.12;

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

      Tomcat版本:6.0.39

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

    一、配置nginx

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

    2、  D: ginx ginx-1.5.12目录结构

    Nginx-

                  |_  conf      配置目录

                  |_  contrib.

                  |_  docs      文档目录

                  |_  logs      日志目录
                  |_  temp      临时文件目录

                  |_  html      静态页面目录

                  |_  nginx.exe 主程序

    3、  启动nginx

      windows下启动nginx非常简单,双击运行nginx.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: omcat omcat01webappsROOTindex.html中

      

    <td align="left" valign="top"><b>Apache Tomcat</b></td>  

    改为

    <td align="left" valign="top"><b>Apache Tomcat 1</b></td>  

    同理我们把tomcat02的D: omcat omcat02webappsROOTindex.html中

    <td align="left" valign="top"><b>Apache Tomcat</b></td>  

    改为

    <td align="left" valign="top"><b>Apache Tomcat 2</b></td>  

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

    <Server port="8005" shutdown="SHUTDOWN">  

     改为

    <Server port="8105" shutdown="SHUTDOWN">  

    <Connector port="8080" protocol="HTTP/1.1"   

               connectionTimeout="20000"   

        redirectPort="8443" />  

     改为

    <Connector port="8180" protocol="HTTP/1.1"   

                  connectionTimeout="20000"   

        redirectPort="8543" />  

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

    改为

    <Connector port="8109" protocol="AJP/1.3" redirectPort="8543" />  

     

     

    2、  配置nginx

    nginx.conf

    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了,如下图。

  • 相关阅读:
    18.3.2从Class上获取信息(属性)
    18.3.2从Class上获取信息(方法)
    18.3.2从Class上获取信息(构造器)
    18.3.1获得Class对象
    ClassLoader.loadClass和Class.forName的区别
    java线程池原理
    如何理解「不要用战术上的勤奋掩盖战略上的懒惰」?
    (转)生产者/消费者问题的多种Java实现方式
    Machine learning system design---Error analysis
    Machine learning system design---prioritizing what to work on
  • 原文地址:https://www.cnblogs.com/wangsongbai/p/9115801.html
Copyright © 2020-2023  润新知