• windows下nginx+tomcat分布式集群部署


    首先官网下载  http://nginx.org/en/download.html,我的本地环境为

    实现的架构:

    从图上可以看出,nginx作为负载均衡请求分发器,当请求A应用时候,分发到A集群,同理当请求B应用的时候,分发到B集群。

    nginx.conf配置

    #Nginx所用用户和组,window下不指定  
    #user  niumd niumd;  
      
    #工作的子进程数量(通常等于CPU数量或者2倍于CPU)  
    worker_processes  2;  
      
    #错误日志存放路径  
    #error_log  logs/error.log;  
    #error_log  logs/error.log  notice;  
    error_log  logs/error.log  info;  
      
    #指定pid存放文件  
    pid        logs/nginx.pid;  
      
    events {  
        #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  
        #use epoll;  
          
        #允许最大连接数  
        worker_connections  2048;  
    }  
    
    
    http {  
        include       mime.types;  
        default_type  application/octet-stream;  
      
            #定义日志格式  
        #log_format  main  '$remote_addr - $remote_user [$time_local] $request '  
        #                  '"$status" $body_bytes_sent "$http_referer" '  
        #                  '"$http_user_agent" "$http_x_forwarded_for"';  
      
        #access_log  off;  
        access_log  logs/access.log;  
      
        client_header_timeout  3m;  
        client_body_timeout    3m;  
        send_timeout           3m;  
       
        client_header_buffer_size    1k;  
        large_client_header_buffers  4 4k;  
      
        sendfile        on;  
        tcp_nopush      on;  
        tcp_nodelay     on;  
      
        #keepalive_timeout  75 20;  
      
        include    gzip.conf;
        upstream localhost {  
          #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  
          #同一机器在多网情况下,路由切换,ip可能不同  
          #ip_hash;   
          server localhost:18001;  
          server localhost:18002;
          server localhost:18003;  
          server localhost:18004;    
         } 
         upstream t1 {  
          server localhost:18001;  
         }   
          upstream t2{  
         
          server localhost:18002;  
         }   
         
        server {
            listen       80;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
             #   root   html;
             #   index  index.html index.htm;
             proxy_connect_timeout   3;  
             proxy_send_timeout      30;  
             proxy_read_timeout      30;  
             proxy_pass http://localhost;
            }
    
            location /t1.jsp {
             #   root   html;
             #   index  index.html index.htm;
             proxy_connect_timeout   3;  
             proxy_send_timeout      30;  
             proxy_read_timeout      30;  
             proxy_pass http://t1;
            }
           location /t2.jsp {
             #   root   html;
             #   index  index.html index.htm;
             proxy_connect_timeout   3;  
             proxy_send_timeout      30;  
             proxy_read_timeout      30;  
             proxy_pass http://t2;
            }
            #error_page  404              /404.html;
    
            # redirect server error pages to the static page /50x.html
            #
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
    
            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ .php$ {
            #    proxy_pass   http://127.0.0.1;
            #}
    
            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ .php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}
    
            # deny access to .htaccess files, if Apache's document root
            # concurs with nginx's one
            #
            #location ~ /.ht {
            #    deny  all;
            #}
        }
    
    
        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    
        # HTTPS server
        #
        #server {
        #    listen       443;
        #    server_name  localhost;
    
        #    ssl                  on;
        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;
    
        #    ssl_session_timeout  5m;
    
        #    ssl_protocols  SSLv2 SSLv3 TLSv1;
        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers   on;
    
        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}
    
    }
    

      Proxy.conf

    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   300;  
    proxy_send_timeout      300;  
    proxy_read_timeout      300;  
    proxy_buffer_size       4k;  
    proxy_buffers           4 32k;  
    proxy_busy_buffers_size 64k;  
    proxy_temp_file_write_size 64k;  
    

      Gzip.conf

    gzip              on;  
    gzip_min_length      1000;  
    gzip_types         text/plain text/css application/x-javascript;
    

      tomcat因为是只有一台主机,server.xml将端口号全部改为不同的。

     

    这个地方分别每台tomcat加上标识tomcat1,tomcat2,tomcat3,tomcat4

    然后分别在D: ginxtomcatapache-tomcat-6.0.18_1webappsROOT,我本地环境路径下,写个jsp文件,内容分别是tomcat1,tomcat2,tomcat3,tomcat4。。。用来测试nginx分发

    jsp内容:

     tomcat端口号修改完,以及加上jsp文件以后,就可以测试了。

    启动nginx:

    nginx -t 测试nginx.conf文件,如果有错误,会提示比如下面的错误

    start nginx。以后台运行nginx的方式启动(推荐用这种方式)

    分别启动tomcat1,tomcat2,tomcat3,tomcat4。。。用来测试nginx分发

    访问http://localhost/hello.jsp

    页面依次出现tomcat1,2,3,内容。证明成功了。。。

    因为nginx默认按轮训的方式依次分发的,如果想改变负载方式,修改这里

    关于upstream:

    1、轮询(默认)

    每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

    2、weight
    指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
    例如:

    upstream bakend {
    server 192.168.159.10 weight=10;
    server 192.168.159.11 weight=10;
    }


    3、ip_hash
    每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
    例如:

    upstream resinserver{
    ip_hash;
    server 192.168.159.10:8080;
    server 192.168.159.11:8080;
    }


    4、fair(第三方)
    按后端服务器的响应时间来分配请求,响应时间短的优先分配。

    upstream resinserver{
    server server1;
    server server2;
    fair;
    }


    5、url_hash(第三方)

    按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。

    例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法


    upstream resinserver{
    server squid1:3128;
    server squid2:3128;
    hash $request_uri;
    hash_method crc32;
    }

    tips:

    upstream resinserver{#定义负载均衡设备的Ip及设备状态
    ip_hash;
    server 127.0.0.1:8000 down;
    server 127.0.0.1:8080 weight=2;
    server 127.0.0.1:6801;
    server 127.0.0.1:6802 backup;
    }

    在需要使用负载均衡的server中增加

    proxy_pass http://resinserver/;


    每个设备的状态设置为:
    1.down 表示单前的server暂时不参与负载
    2.weight 默认为1.weight越大,负载的权重就越大。
    3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
    4.fail_timeout:max_fails次失败后,暂停的时间。
    5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

    nginx支持同时设置多组的负载均衡,用来给不用的server来使用。

    client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
    client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
    location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

    到此结束了,在生产环境下,一台nginx肯定是不够完美的,如果nginx挂掉,是不是整个应用就down了。。。生产环境下,我们可以部署多台nginx,利用f5或者lvs+keepalive来实现负载。

    f5是传输层负载。

    nginx是应用层负载。

  • 相关阅读:
    容斥原理
    泰勒展开
    初等微积分
    粒子群优化(微粒群算法)
    生成函数
    FFT例题
    图论例题
    线段求交
    期望小小结
    [Violet]天使玩偶/SJY摆棋子
  • 原文地址:https://www.cnblogs.com/xiaoblog/p/5960830.html
Copyright © 2020-2023  润新知