NGINX+TOMCAT+MEMCACHED 架构分布式集群

来源:互联网 发布:阿里云是用来干嘛的 编辑:程序博客网 时间:2024/05/17 01:05

          Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。

        近年来nginx越来越流行,很多网站都开始用nginx来做web代理,以及原来的大公司也都在考虑用nginx来代替apache和resign。由于我在上个公司时,一个电子商务网站开始用的是apache+tomcat,后来也开始考虑用nginx+tomcat+memcache来代替apache+tomcat。

        通用架构如下:

        其中用memcache来存储session,tomcat作为servlet容器,nginx作为tomcat代理,对外接口。这个时候只要保证memcache服务器不停,tomcat之间可以任意热插拔。实现了session的独立存储,需要的话,还可以存储到数据库或者文件中,也不用考虑是否使用黏性session。

       在本例中,使用到了一个memcache服务器,3个tomcat,一个nginx,rhel5.4 64位操作系统,配置如下。

 nginx配置:

nginx.conf配置如下:

[plain] view plaincopyprint?

  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.     server_tokens off;  
  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.     sendfile        on;  
  27.     #tcp_nopush     on;  
  28.   
  29.     #keepalive_timeout  0;  
  30.     keepalive_timeout  65;  
  31.   
  32.     gzip  on;  
  33.     gzip_min_length 1k;  
  34.     gzip_buffers 4 16k;  
  35.     gzip_http_version  1.1;  
  36.     gzip_comp_level  2;  
  37.     gzip_types  text/plain application/x-javascript text/css application/xml;  
  38.     gzip_vary  on;  
  39.     gzip_disable "MSIE [1-6].";  
  40.    
  41.     proxy_temp_path proxy_temp_dir;  
  42.     proxy_cache_path proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=3g;  
  43.   
  44.       
  45.     upstream shopcluster{  
  46.     server 127.0.0.1:8180 weight=1;  
  47.     server 127.0.0.1:8182 weight=1;  
  48.     }  
  49.   
  50.     upstream hmfkcluster{  
  51.     server 127.0.0.1:8080 weight=1;  
  52.     server 127.0.0.1:8081 weight=1;  
  53.     }  
  54.   
  55.     server {  
  56.         listen       80 default;  
  57.         server_name  localhost;  
  58.   
  59.         #charset utf-8;  
  60.   
  61.         #access_log  logs/host.access.log  main;  
  62.   
  63.         location / {  
  64.            #root   /data/zwlsshop/www;  
  65.         proxy_pass        http://shopcluster;         
  66.             index  index.jsp index.html index.htm;  
  67.         }  
  68.           
  69.         location ~* \.(gif|jpg|png|js|css)$ {  
  70.         access_log off;  
  71.          #  root   /data/zwlsshop/www;  
  72.             expires 2h;  
  73.         proxy_cache cache_one;  
  74.         proxy_cache_key $host$uri$is_args$args;  
  75.         proxy_cache_valid any 1h;  
  76.         if ( !-f \$request_filename) {  
  77.     #    access_log logs/imgaccess.log;  
  78.                 proxy_pass http://shopcluster;  
  79.              }  
  80.         }  
  81.   
  82.   
  83. #        location = /test/cachetest.jsp {  
  84. #            access_log logs/cachetestall.log;  
  85.          #  root   /data/zwlsshop/www;  
  86. #            expires 2h;  
  87. #            proxy_cache cache_one;  
  88. #            proxy_cache_key $host$uri$is_args$args;  
  89. #            proxy_cache_valid any 1h;  
  90. #            if ( !-f \$request_filename) {  
  91. #                proxy_pass http://shopcluster;  
  92. #             }  
  93. #        }  
  94.   
  95.   
  96. #   location ~ /purge(/.*) {  
  97. #       proxy_cache_purge    cache_one   $host$1$is_args$args;  
  98. #     
  99. #  
  100. #   }  
  101.   
  102.   
  103.     location /nginxs {  
  104.        stub_status        on;   
  105.            access_log         off;  
  106.     }  
  107.   
  108.         #error_page  404              /404.html;  
  109.   
  110.         # redirect server error pages to the static page /50x.html  
  111.         #  
  112.         error_page   500 502 503 504  /50x.html;  
  113.         location = /50x.html {  
  114.             root   html;  
  115.         }  
  116.   
  117.   
  118.         # deny access to .htaccess files, if Apache's document root  
  119.         # concurs with nginx's one  
  120.         #  
  121.         #location ~ /\.ht {  
  122.         #    deny  all;  
  123.         #}  
  124.     }  
  125.       
  126.   
  127.     server {  
  128.         listen       80;  
  129.         server_name localhost2 <a target="_blank" href="http://www.myroute.cn">www.myroute.cn  
  130.   
  131. </a>        #charset utf-8;  
  132.   
  133.         #access_log  logs/host.access.log  main;  
  134.   
  135.         location / {  
  136.            #root   /data/zwlsshop/www;  
  137.             proxy_pass        http://hmfkcluster;  
  138.             index  index.jsp index.html index.htm;  
  139.         }  
  140.   
  141. #   location ~* .+\.(gif|jpg|png|js|css)$ {  
  142. #          
  143. #       access_log off;  
  144.          #  root   /data/zwlsshop/www;  
  145. #            expires 2h;  
  146.  #           proxy_cache cache_one;  
  147. #       proxy_cache_key $host$uri$is_args$args;  
  148.  #           proxy_cache_valid any 1h;  
  149.   #          if ( !-f \$request_filename) {  
  150.         #        access_log logs/imgaccess.log;  
  151.    #             proxy_pass http://hmfkcluster;  
  152.     #         }  
  153.   
  154.   
  155. #   }  
  156.   
  157.   
  158.   
  159.         #error_page  404              /404.html;  
  160.   
  161.         # redirect server error pages to the static page /50x.html  
  162.         #  
  163.         error_page   500 502 503 504  /50x.html;  
  164.         location = /50x.html {  
  165.             root   html;  
  166.         }  
  167.   
  168.   
  169.         # deny access to .htaccess files, if Apache's document root  
  170.         # concurs with nginx's one  
  171.         #  
  172.         #location ~ /\.ht {  
  173.         #    deny  all;  
  174.         #}  
  175.     }  
  176.   
  177.   
  178.   
  179. }  

nginx使用说明:

tomcat:

tomcat只需要改动一点,即在conf文件夹下的context.xml添加下面语句

在contex里面添加:

[html] view plaincopyprint?

  1. <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"  
  2.       memcachedNodes="m1:127.0.0.1:11211"  
  3.       sticky="false"  
  4.       lockingMode="auto"  
  5.       sessionBackupAsync="false"  
  6.       sessionBackupTimeout="1000"  
  7.       transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"  
  8. gt;  

其中memcachedNodes指定memcache的ip地址以及端口。sticky指定是否在tomcat中存储session做缓存。可以做两个memcach服务器,以备份session。

0 0
原创粉丝点击