• Nginx+tomcat集群环境搭建


    实验环境: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中

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <td align="left" valign="top"><b>Apache Tomcat</b></td>  
    改为

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <td align="left" valign="top"><b>Apache Tomcat 1</b></td>  
    同理我们把tomcat02的D: omcat omcat02webappsROOTindex.html中

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <td align="left" valign="top"><b>Apache Tomcat</b></td>  
    改为

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <td align="left" valign="top"><b>Apache Tomcat 2</b></td>  

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

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <Server port="8005" shutdown="SHUTDOWN">  
    改为

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <Server port="8105" shutdown="SHUTDOWN">  

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <Connector port="8080" protocol="HTTP/1.1"   
    2.                connectionTimeout="20000"   
    3.                redirectPort="8443" />  
    改为

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <Connector port="8180" protocol="HTTP/1.1"   
    2.                connectionTimeout="20000"   
    3.                redirectPort="8543" />  

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  
    改为

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1. <Connector port="8109" protocol="AJP/1.3" redirectPort="8543" />  


    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    1.   
    2、  配置nginx

    nginx.conf

    [html] view plaincopy在CODE上查看代码片派生到我的代码片
    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了,如下图。



  • 相关阅读:
    在MVC中更新ModelFirst Entity Framework POCO实体外键的方法
    美国行照片集十四:洛杉矶拉斯维加斯之旅
    深入ASP.NET MVC之三:Controller的激活
    美国行照片集之十三:感恩节之旅
    深入ASP.NET MVC 之一:IIS到路由表
    博客样式改版
    关于如何用string保存二进制数据的问题
    XSI IPC机制的优缺点
    C,C++,java,python对比
    grunt requireJS 的基础配置
  • 原文地址:https://www.cnblogs.com/duyinqiang/p/5696708.html
Copyright © 2020-2023  润新知