Nginx+Tomcat负载均衡、动静分离

    一、负载均衡+动静分离

    修改nginx/conf/nginx.conf,修改完后如下:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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  logs/access.log  main;
    
  
    #设定请求缓冲
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 300m;
    sendfile on;
    tcp_nopush     on;
    keepalive_timeout 60;
    tcp_nodelay on;
    server_tokens off;
    client_body_buffer_size 512k;
    proxy_connect_timeout   5;
    proxy_send_timeout      60;
    proxy_read_timeout      5;
    proxy_buffer_size       16k;
    proxy_buffers           4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k; 
    client_header_timeout  3m;
    client_body_timeout    3m;
    send_timeout           3m;
 
 
    gzip on;#开启gzip,节省带宽
    gzip_min_length  1100;
    gzip_buffers     4 8k;
    gzip_types       text/plain text/css application/x-javascript image/bmp application/javascript;   
    
    output_buffers   1 32k;
    postpone_output  1460;
    
    limit_rate_after 3m;#限速模块,前3M下载时不限速
    limit_rate 512k; #限速模块 


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        access_log  logs/host.access.log;
        
        
        #自动补全"/"
        if (-d $request_filename){
         rewrite ^/(.*)([^/])$ http://$host/$1$2/ last;
        }
        
        
        ###################动静分离配置#######################
        
        ###################动态访问转向tomcat处理#######################
        #location ~ \.(jsp|page|do)?$ {
        #   proxy_set_header  Host $host;
        #   proxy_set_header  X-Real-IP  $remote_addr;
        #   proxy_pass http://mcul;#mcul与负载均衡upstream配置名称mcul一致 
        #}
        
        ###################设定访问静态文件直接读取不经过tomcat#########
        #location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$  { 
        #   expires      30d;     
        #}      
        #location ~ .*\.(js|css)?$ {
        #   expires      1h;
        #}   
        
        ####################所有请求均有Tomcat处理############
        location / {
            root   html;
            index  index.html index.htm;
            proxy_pass  http://mcul;  #mcul与负载均衡upstream配置名称mcul一致 
        }
        
        ###################动静分离配置#######################

        ##################404错误页#################
        error_page  404              /404.html;
        location = /40x.html {  
            root   html;  
        }
        ##################404错误页#################

        # redirect server error pages to the static page /50x.html
        #
        
        ##################50x错误页#################
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        ##################50x错误页#################

        # 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;
    #    }
    #}

    ############负载均衡配置###########
    upstream mcul {  
        server 127.0.0.1:8080 weight=1;  
        server 127.0.0.1:9080 weight=2;  
    }  
    ############负载均衡配置###########

    # 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;
    #    }
    #}

}
 

    二、Session复制

    1.修改Tomcat/conf/server.xml, 在<Engine name="." ..></Engine>标签中最后处添加以下代码:

    注意:对于这一步各个版本的Tomcat代码可能,具体可看http://localhost:8080/docs/cluster-howto.html

<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"
                 channelSendOptions="6">

          <Manager className="org.apache.catalina.ha.session.BackupManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"
                   mapSendOptions="6"/>
          <!--
          <Manager className="org.apache.catalina.ha.session.DeltaManager"
                   expireSessionsOnShutdown="false"
                   notifyListenersOnReplication="true"/>
          -->
          <Channel className="org.apache.catalina.tribes.group.GroupChannel">
            <Membership className="org.apache.catalina.tribes.membership.McastService"
                        address="228.0.0.4"
                        port="45564"
                        frequency="500"
                        dropTime="3000"/>
            <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"
                      address="auto"
                      port="5000"
                      selectorTimeout="100"
                      maxThreads="6"/>

            <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">
              <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>
            </Sender>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>
            <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>
          </Channel>

          <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"
                 filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>

          <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"
                    tempDir="/tmp/war-temp/"
                    deployDir="/tmp/war-deploy/"
                    watchDir="/tmp/war-listen/"
                    watchEnabled="false"/>

          <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>
        </Cluster>

   2.修改应用的web.xml,在<web-app>结束标签前追加 <distributable/>

 

    三、重启,测试

 

猜你喜欢

转载自oma1989.iteye.com/blog/1751178