• nginx(Window下安装 & 配置文件参数说明 & 实例)


    一、为什么需要对Tomcat服务器做负载均衡: 

    Tomcat服务器作为一个Web服务器,其并发数在300-500之间,如果有超过500的并发数便会出现Tomcat不能响应新的请求的情况,严重影响网站的运行。另外,在访问量大的情况下,Tomcat的线程数会不断增加。由于Tomcat自身对内存的占用有控制,当对内存的占用达到最大值时便会出现内存溢出,对网站的访问严重超时等现象,这时便需要重新启动Tomcat以释放占用的内存,这样做便会阻断网站运行。 

    所以对Tomcat做负载均衡便很有必要。目前可以和Tomcat做负载均衡的主流服务器是Apache,但是Nginx由于功能多、配置简单等优点逐渐成为很多负载均衡服务器的首选。Nginx的并发数可达到50000,所以理论上可以和Tomcat以1:100的比例来配置,这样便可以很好的解决网站并发瓶颈问题。而且Nginx、apache是基于http反向代理方式,位于ISO模型的第七层应用层。直白些就是TCP UDP 和http协议的区别,Nginx不能为基于TCP协议的应用提供负载均衡。

     二. 配置方法

      1.下载相应的服务器,本人两个Tomcat的版本都采用6.0的.Nginx采用1.5.6版本

      

      2. Nginx介绍

         目录结构

         Nginx-

                  |_  conf   配置目录

                  |_  contrib

                  |_  docs 文档目录

                  |_  logs  日志目录

                  |_  temp 临时文件目录

                  |_  html 静态页面目录

                  |_  nginx.exe 主程序

       window下安装Nginx极其简单,解压缩到一个无空格的英文目录即可(个人习惯,担心中文出问题),双击nginx启动,这里我安装到:D: est目录. 若果想停止nginx,dos环境运行命令:nginx -s stop.

    nginx.conf配置

       Nginx配置文件默认在conf目录,主要配置文件为nginx.conf,我们安装在D:server ginx-0.8.20、默认主配置文件为D:server ginx-0.8.20 ginx.conf。下面是nginx作为前端反向代理服务器的配置。

     
     
    1. Nginx.conf代码  
    2.   
    3.   #Nginx所用用户和组,window下不指定  
    4. #user  nobody;  
    5.   
    6. #工作的子进程(通常等于CPU数量或者1倍于CPU)  
    7. worker_processes  1;  
    8.   
    9. #错误日志存放路径  
    10. #error_log  logs/error.log;  
    11. #error_log  logs/error.log  notice;  
    12. #error_log  logs/error.log  info;  
    13.   
    14. #指定pid存放文件  
    15. #pid        logs/nginx.pid;  
    16.   
    17.   
    18. events {  
    19.     #允许最大连接数  
    20.     worker_connections  1024;  
    21. }  
    22.   
    23.   
    24. http {  
    25.     include       mime.types;  
    26.     default_type  application/octet-stream;  
    27.        
    28.      #定义日志格式  
    29.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
    30.     #                  '$status $body_bytes_sent "$http_referer" '  
    31.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
    32.   
    33.     #access_log  logs/access.log  main;  
    34.   
    35.     sendfile        on;  
    36.     #tcp_nopush     on;  
    37.   
    38.     #keepalive_timeout  0;  
    39.     keepalive_timeout  65;  
    40.       
    41.     #客户端上传文件大小控制  
    42.     client_max_body_size 8m;  
    43.       
    44.     #gzip  on;  
    45.       upstream localhost {    
    46.                   server localhost:8080;  
    47.                   server localhost:8000;  
    48.          #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。                 
    49.          #同一机器在多网情况下,路由切换,ip可能不同                  
    50.                ip_hash;    
    51.                    }   
    52.                      
    53.     server {  
    54.         listen       9999;  
    55.         server_name  localhost;  
    56.   
    57.         #charset koi8-r;  
    58.   
    59.         #access_log  logs/host.access.log  main;  
    60.   
    61.         location / {  
    62.             root html;  
    63.             index index.html index.htm;  
    64.             #此处的 http://localhost与upstream localhost对应  
    65.             proxy_pass  http://localhost;  
    66.               
    67.             proxy_redirect off;  
    68.             proxy_set_header Host $host;  
    69.             proxy_set_header X-Real-IP $remote_addr;  
    70.             proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  
    71.             client_max_body_size   10m;   
    72.             client_body_buffer_size  128k;  
    73.             proxy_connect_timeout  100;  
    74.             proxy_send_timeout   100;  
    75.             proxy_read_timeout 100;  
    76.             proxy_buffer_size 4k;  
    77.             proxy_buffers  4 32k;  
    78.             proxy_busy_buffers_size 64k;  
    79.             proxy_temp_file_write_size  64k;  
    80.         }  
    81.   
    82.         #error_page  404              /404.html;  
    83.   
    84.         # redirect server error pages to the static page /50x.html  
    85.         #  
    86.         error_page   500 502 503 504  /50x.html;  
    87.         location = /50x.html {  
    88.             root   html;  
    89.         }  
    90.   
    91.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
    92.         #  
    93.         #location ~ .php$ {  
    94.         #    proxy_pass   http://127.0.0.1;  
    95.         #}  
    96.   
    97.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
    98.         #  
    99.         #location ~ .php$ {  
    100.         #    root           html;  
    101.         #    fastcgi_pass   127.0.0.1:9000;  
    102.         #    fastcgi_index  index.php;  
    103.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
    104.         #    include        fastcgi_params;  
    105.         #}  
    106.   
    107.         # deny access to .htaccess files, if Apache's document root  
    108.         # concurs with nginx's one  
    109.         #  
    110.         #location ~ /.ht {  
    111.         #    deny  all;  
    112.         #}  
    113.     }  
    114.   
    115.   
    116.     # another virtual host using mix of IP-, name-, and port-based configuration  
    117.     #  
    118.     #server {  
    119.     #    listen       8000;  
    120.     #    listen       somename:8080;  
    121.     #    server_name  somename  alias  another.alias;  
    122.   
    123.     #    location / {  
    124.     #        root   html;  
    125.     #        index  index.html index.htm;  
    126.     #    }  
    127.     #}  
    128.   
    129.   
    130.     # HTTPS server  
    131.     #  
    132.     #server {  
    133.     #    listen       443 ssl;  
    134.     #    server_name  localhost;  
    135.   
    136.     #    ssl_certificate      cert.pem;  
    137.     #    ssl_certificate_key  cert.key;  
    138.   
    139.     #    ssl_session_cache    shared:SSL:1m;  
    140.     #    ssl_session_timeout  5m;  
    141.   
    142.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
    143.     #    ssl_prefer_server_ciphers  on;  
    144.   
    145.     #    location / {  
    146.     #        root   html;  
    147.     #        index  index.html index.htm;  
    148.     #    }  
    149.     #}  



     

    修改Nginx配置文件nginx.conf

    ①       在#gzip  on;后面加入下面配置: 

     
     
    1. upstream localhost {     
    2.            server localhost:8080;     
    3.            server localhost:8088;     
    4.           ip_hash;   
     
    1. }   

    其中serverlocalhost:8080为第一个Tomcat的启动地址,server localhost:8088为第二个Tomcat的启动地址,ip_hash用于做session同步其实是不起作用的。

     ② 修改第一个server{}配置中的listen  80;改为新的端口号,因为我的本机80端口被其他服务器占用,因此将此处改为listen 9999;

     
     
    1. location / {  
     
     
    1.           root   html;    
    2.           index  index.html index.htm;   
    3. }    
    4. 改为:   
    5. location / {  
    6.                         root html;  
    7.             index index.html index.htm;  
    8.             #此处的 http://localhost与upstream localhost对应  
    9.             proxy_pass  http://localhost;  
    10.               
    11.             proxy_redirect off;  
    12.             proxy_set_header Host $host;  
    13.             proxy_set_header X-Real-IP $remote_addr;  
    14.             proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  
    15.             client_max_body_size   10m;   
    16.             client_body_buffer_size  128k;  
    17.             proxy_connect_timeout  100;  
    18.             proxy_send_timeout   100;  
    19.             proxy_read_timeout 100;  
    20.             proxy_buffer_size 4k;  
    21.             proxy_buffers  4 32k;  
    22.             proxy_busy_buffers_size 64k;  
    23.             proxy_temp_file_write_size  64k;  
    24.         }  

    3.tomcat的配置

      修改其中一个tomcat的server.xml配置文件即可

    第一处端口修改:

     
     
    1. <!--  修改port端口:18006 俩个tomcat不能重复,端口随意,别太小-->    
    2. lt;Server port="8000" shutdown="SHUTDOWN">    

    第二处端口修改

     

     
    1. <!-- port="18081" tomcat监听端口,随意设置,别太小 -->    
    2. <Connector port="18081" protocol="HTTP/1.1"     
    3.                connectionTimeout="20000"     
    4.                redirectPort="8443" />    

     第三处端口修改:

    Java代码 

     
     
    1. "8009" protocol="AJP/1.3" redirectPort="8443" />  


    验证配置与测试负载均衡

      首先测试nginx配置是否正确,测试命令:nginx -t  (默认验证:conf ginx.conf),也可以指定配置文件路径

     其次验证两个tomcat,启动两个tomcat不出现端口冲突即为成功

    最后验证配置负载均衡,启动nginx双机nginx.exe文件或者用start nginx启动 打开浏览器输入地址http://localhost:9999看到下面画面表示成功:

    此图说明已经成功跳转到tomcat 输入测试路径可以看到测试项目的首页就表示基本成功。

         至此window下nginx+tomcat负载均衡配置结束,关于tomcat Session的问题通常是采用memcached,或者采用nginx_upstream_jvm_route,他是一个Nginx的扩展模块,用来实现基于 Cookie的 Session Sticky的功能。如果tomcat过多不建议session同步,server间相互同步session很耗资源,高并发环境容易引起Session风暴。请根据自己应用情况合理采纳session解决方案。

    ========================================================================================= 

    在nginx目录下,运行 nginx -t ,出现如下图所示则配置正确。

    然后启动nginx

    一些命令:

    start nginx 启动nginx

    nginx -s stop 停止nginx

    配置Tomcat,端口号分别为9001,9002 ,调试至正常运行。启动tomcat

    两个Tomcat部署完毕后,输入http://localhost:9000/,刷新多次后,注意上图中标注的地方,THIS IS TOMCAT-1,THIS IS TOMCAT-2  说明访问了不同的tomcat .

    至此,windows下nginx tomcat 负载均衡配置结束。

    =========================================================================================

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

    二、Nginx+Tomca在Windows下负载均衡的配置:
        先秀一下我的环境: Nginx1.3.11 + Tomcat 6.0(2个)、Win 7。
        下面是配置过程:
        1、将nginx-1.3.11.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">,修改其中的

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

            为 

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

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

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

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

    1
    2
    3
    4
    5
    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、修改

    1
    2
    3
    4
    location / {
        root html;
        index index.html index.htm;
    }

        改为:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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的首页了。

  • 相关阅读:
    maven项目报错:Class path contains multiple SLF4J bindings
    ubuntu18.04 点击启动器实现窗口最小化
    Eclipse lombok get set方法报错
    try-with-resources 让java资源关闭代码更简洁
    yang文件语法格式
    RabbitMQ 交换器、持久化
    RabbitMQ 简介
    systemctl命令配置系统服务
    Karaf基础知识
    Linux shell模拟多线程拷贝
  • 原文地址:https://www.cnblogs.com/therunningfish/p/5417212.html
Copyright © 2020-2023  润新知