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

来源:互联网 发布:卖数据的联系方式 编辑:程序博客网 时间:2024/06/02 06:17

 一、负载均衡+动静分离

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

Xml代码  收藏代码
  1. #user  nobody;  
  2. worker_processes  1;  
  3.   
  4. #error_log  logs/error.log;  
  5. #error_log  logs/error.log  notice;  
  6. error_log  logs/error.log  info;  
  7.   
  8. pid        logs/nginx.pid;  
  9.   
  10.   
  11. events {  
  12.     worker_connections  1024;  
  13. }  
  14.   
  15.   
  16. http {  
  17.     include       mime.types;  
  18.     default_type  application/octet-stream;  
  19.   
  20.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  
  21.     #                  '$status $body_bytes_sent "$http_referer" '  
  22.     #                  '"$http_user_agent" "$http_x_forwarded_for"';  
  23.   
  24.     #access_log  logs/access.log  main;  
  25.       
  26.     
  27.     #设定请求缓冲  
  28.     server_names_hash_bucket_size 128;  
  29.     client_header_buffer_size 32k;  
  30.     large_client_header_buffers 4 32k;  
  31.     client_max_body_size 300m;  
  32.     sendfile on;  
  33.     tcp_nopush     on;  
  34.     keepalive_timeout 60;  
  35.     tcp_nodelay on;  
  36.     server_tokens off;  
  37.     client_body_buffer_size 512k;  
  38.     proxy_connect_timeout   5;  
  39.     proxy_send_timeout      60;  
  40.     proxy_read_timeout      5;  
  41.     proxy_buffer_size       16k;  
  42.     proxy_buffers           4 64k;  
  43.     proxy_busy_buffers_size 128k;  
  44.     proxy_temp_file_write_size 128k;   
  45.     client_header_timeout  3m;  
  46.     client_body_timeout    3m;  
  47.     send_timeout           3m;  
  48.    
  49.    
  50.     gzip on;#开启gzip,节省带宽  
  51.     gzip_min_length  1100;  
  52.     gzip_buffers     4 8k;  
  53.     gzip_types       text/plain text/css application/x-javascript image/bmp application/javascript;     
  54.       
  55.     output_buffers   1 32k;  
  56.     postpone_output  1460;  
  57.       
  58.     limit_rate_after 3m;#限速模块,前3M下载时不限速  
  59.     limit_rate 512k; #限速模块   
  60.   
  61.   
  62.     server {  
  63.         listen       80;  
  64.         server_name  localhost;  
  65.   
  66.         #charset koi8-r;  
  67.   
  68.         access_log  logs/host.access.log;  
  69.           
  70.           
  71.         #自动补全"/"  
  72.         if (-d $request_filename){  
  73.          rewrite ^/(.*)([^/])$ http://$host/$1$2/ last;  
  74.         }  
  75.           
  76.           
  77.         ###################动静分离配置#######################  
  78.           
  79.         ###################动态访问转向tomcat处理#######################  
  80.         #location ~ \.(jsp|page|do)?$ {  
  81.         #   proxy_set_header  Host $host;  
  82.         #   proxy_set_header  X-Real-IP  $remote_addr;  
  83.         #   proxy_pass http://mcul;#mcul与负载均衡upstream配置名称mcul一致   
  84.         #}  
  85.           
  86.         ###################设定访问静态文件直接读取不经过tomcat#########  
  87.         #location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$  {   
  88.         #   expires      30d;       
  89.         #}        
  90.         #location ~ .*\.(js|css)?$ {  
  91.         #   expires      1h;  
  92.         #}     
  93.           
  94.         ####################所有请求均有Tomcat处理############  
  95.         location / {  
  96.             root   html;  
  97.             index  index.html index.htm;  
  98.             proxy_pass  http://mcul;  #mcul与负载均衡upstream配置名称mcul一致   
  99.         }  
  100.           
  101.         ###################动静分离配置#######################  
  102.   
  103.         ##################404错误页#################  
  104.         error_page  404              /404.html;  
  105.         location = /40x.html {    
  106.             root   html;    
  107.         }  
  108.         ##################404错误页#################  
  109.   
  110.         # redirect server error pages to the static page /50x.html  
  111.         #  
  112.           
  113.         ##################50x错误页#################  
  114.         error_page   500 502 503 504  /50x.html;  
  115.         location = /50x.html {  
  116.             root   html;  
  117.         }  
  118.         ##################50x错误页#################  
  119.   
  120.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80  
  121.         #  
  122.         #location ~ \.php$ {  
  123.         #    proxy_pass   http://127.0.0.1;  
  124.         #}  
  125.   
  126.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  127.         #  
  128.         #location ~ \.php$ {  
  129.         #    root           html;  
  130.         #    fastcgi_pass   127.0.0.1:9000;  
  131.         #    fastcgi_index  index.php;  
  132.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;  
  133.         #    include        fastcgi_params;  
  134.         #}  
  135.   
  136.         # deny access to .htaccess files, if Apache's document root  
  137.         # concurs with nginx's one  
  138.         #  
  139.         #location ~ /\.ht {  
  140.         #    deny  all;  
  141.         #}  
  142.     }  
  143.   
  144.   
  145.     # another virtual host using mix of IP-, name-, and port-based configuration  
  146.     #  
  147.     #server {  
  148.     #    listen       8000;  
  149.     #    listen       somename:8080;  
  150.     #    server_name  somename  alias  another.alias;  
  151.   
  152.     #    location / {  
  153.     #        root   html;  
  154.     #        index  index.html index.htm;  
  155.     #    }  
  156.     #}  
  157.   
  158.     ############负载均衡配置###########  
  159.     upstream mcul {    
  160.         server 127.0.0.1:8080 weight=1;    
  161.         server 127.0.0.1:9080 weight=2;    
  162.     }    
  163.     ############负载均衡配置###########  
  164.   
  165.     # HTTPS server  
  166.     #  
  167.     #server {  
  168.     #    listen       443;  
  169.     #    server_name  localhost;  
  170.   
  171.     #    ssl                  on;  
  172.     #    ssl_certificate      cert.pem;  
  173.     #    ssl_certificate_key  cert.key;  
  174.   
  175.     #    ssl_session_timeout  5m;  
  176.   
  177.     #    ssl_protocols  SSLv2 SSLv3 TLSv1;  
  178.     #    ssl_ciphers  HIGH:!aNULL:!MD5;  
  179.     #    ssl_prefer_server_ciphers   on;  
  180.   
  181.     #    location / {  
  182.     #        root   html;  
  183.     #        index  index.html index.htm;  
  184.     #    }  
  185.     #}  
  186.   
  187. }  
 

    二、Session复制

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

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

Xml代码  收藏代码
  1. <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"  
  2.                  channelSendOptions="6">  
  3.   
  4.           <Manager className="org.apache.catalina.ha.session.BackupManager"  
  5.                    expireSessionsOnShutdown="false"  
  6.                    notifyListenersOnReplication="true"  
  7.                    mapSendOptions="6"/>  
  8.           <!--  
  9.           <Manager className="org.apache.catalina.ha.session.DeltaManager"  
  10.                    expireSessionsOnShutdown="false"  
  11.                    notifyListenersOnReplication="true"/>  
  12.           -->  
  13.           <Channel className="org.apache.catalina.tribes.group.GroupChannel">  
  14.             <Membership className="org.apache.catalina.tribes.membership.McastService"  
  15.                         address="228.0.0.4"  
  16.                         port="45564"  
  17.                         frequency="500"  
  18.                         dropTime="3000"/>  
  19.             <Receiver className="org.apache.catalina.tribes.transport.nio.NioReceiver"  
  20.                       address="auto"  
  21.                       port="5000"  
  22.                       selectorTimeout="100"  
  23.                       maxThreads="6"/>  
  24.   
  25.             <Sender className="org.apache.catalina.tribes.transport.ReplicationTransmitter">  
  26.               <Transport className="org.apache.catalina.tribes.transport.nio.PooledParallelSender"/>  
  27.             </Sender>  
  28.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.TcpFailureDetector"/>  
  29.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor"/>  
  30.             <Interceptor className="org.apache.catalina.tribes.group.interceptors.ThroughputInterceptor"/>  
  31.           </Channel>  
  32.   
  33.           <Valve className="org.apache.catalina.ha.tcp.ReplicationValve"  
  34.                  filter=".*\.gif|.*\.js|.*\.jpeg|.*\.jpg|.*\.png|.*\.htm|.*\.html|.*\.css|.*\.txt"/>  
  35.   
  36.           <Deployer className="org.apache.catalina.ha.deploy.FarmWarDeployer"  
  37.                     tempDir="/tmp/war-temp/"  
  38.                     deployDir="/tmp/war-deploy/"  
  39.                     watchDir="/tmp/war-listen/"  
  40.                     watchEnabled="false"/>  
  41.   
  42.           <ClusterListener className="org.apache.catalina.ha.session.ClusterSessionListener"/>  
  43.         </Cluster>  

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

 

    三、重启,测试

 

0 0
原创粉丝点击