OFBIZ分享:利用Nginx +Memcached架设高性能的服务

近年来利用Nginx和Memcached来提高站点的服务性能的作法,如一夜春风般的遍及大江南北,越来越多的门户站点和电子商务平台都採用它们来为自己的用户提供更好的服务体验。如:网易、淘宝、京东、凡客等无一不是如此。关于它们的优点,在此我不想再多言,假设您感兴趣,能够到网络上去搜索一下就可以轻松获得这方面的资料。我们今天的重点是和大家分享将Ofbiz与Nginx和Memcached配置在一起,在达到好的效果。

1)       所须要的软件

n  Tengine:这是一个经过淘宝公司优化并开源的Nginxserver,它不仅保留了Nginx的全部长处,还增加了其他一些非常好的扩展。

n  Memcached :请到官网上下载最新版本号。

n  memc-nginx和srcache-nginx:这是淘宝公司的project师开发的,用于Nginx直接存取Memcached的两个扩展。请到网上下载它最新的版本号。在baidu中搜索memc-nginx和srcache-nginx就能够非常easy找到它们。

2)        软件安装

3)        原理

 

增加nginx和memcached后,当nginx收到用户的请求后,仅仅有动态的内容会被转发到ofbiz的tomcat进行处理,全部的静态内容都由nginx直接处理了,大大的减少了tomcatserver和后台数据库的压力。此外,由于memcached的存在,nginx会首先到memcached中提取数据,仅仅有数据不存在时才会到磁盘上读取文件或转给tomcat进行处理,并将取得的结果数据存入memcached中,这样,当下一次再请求同样的数据时,就直接从memcached中取了。不再受磁盘IO数的限制,大大的提高了效率。

 

由于直接由nginx对memcached进行存取,所以不须要对ofbiz的代码进行额外的改动,配置更加灵活。

 

4)        配置文件

以下我们来重点看一下nginx的配置文件。须要说明的是这个配置文件里,仅仅对经常使用的了部分组件进行了配置,假设没有包括您所须要的配置部分,你能够自己依照同样的思路去加入上就可以。

#user nobody;

worker_processes  2;

daemon on;

#error_log logs/error.log;

#error_log logs/error.log  notice;

error_log logs/error.log  info;

#error_log logs/error.log  debug;

 

#pid        logs/nginx.pid;

 

 

events {

   use epoll;

   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"';

 

  

   upstream memcache{

       server 127.0.0.1:12357;

       keepalive 512;

    }

   

   #

   upstream ofbiz_tomcat{

       server 127.0.0.1:8080;

    }

   

 

#ofbiz server

server {

   listen       80;

   server_name _*;

   

    #charset koi8-r;

   access_log logs/ecshop.ofbiz.cn.log  main;

    #处理memcached

     location /memc {

        internal;#仅仅同意本地訪问,提高了数据安全

        memc_connect_timeout100ms;

        memc_send_timeout100ms;

        memc_read_timeout100ms;

        set$memc_key $query_string;

        memc_passmemcache;

    }

   

   #依据uri和參数,在memcached中查找,假设找到就返回给client,找不到就,将动态的请求转请给tomcat

location~* /(control|products)/ {

set $key$uri$args;

srcache_fetchGET /memc $key;

srcache_storePUT /memc $key;

 

     proxy_set_header Host $host;

     proxy_set_header X-Forwarded-For $remote_addr;

    proxy_pass http://ofbiz_tomcat;

    }

    #依据uri和參数,在memcached中查找,假设找到就返回给client,找不到就,将动态的请求转请给tomcat

   location ~* .(jsp|jspx)$

{

set $key$uri$args;

srcache_fetchGET /memc $key;

srcache_storePUT /memc $key;

 

    proxy_set_header Host $host;

     proxy_set_header X-Forwarded-For $remote_addr;

    proxy_pass http://ofbiz_tomcat;

    }

   

   

     #依据uri和參数,在memcached中查找,假设找到就返回给client,找不到就,到磁盘读取文件给client,并存处memcached

location/ {

set $key$uri$args;

srcache_fetchGET /memc $key;

srcache_storePUT /memc $key;

 

       root   <OFBIZ_HOME>/baseapps/ecommerce/webapp/ecshop;

       index  index.html index.htm  index.jsp default.jsp;

    }

    #依据uri和參数,在memcached中查找,假设找到就返回给client,找不到就,到磁盘读取文件给client,并存处memcached

 

location  /ordermgr/{

set $key$uri$args;

srcache_fetchGET /memc $key;

srcache_storePUT /memc $key;

 

       root   <OFBIZ_HOME>/baseapps/order/webapp;

       index  index.html index.htm   ;

  

   }  

     #依据uri和參数,在memcached中查找,假设找到就返回给client,找不到就,到磁盘读取文件给client,并存处memcached

 

location  /partymgr/{

set $key$uri$args;

srcache_fetchGET /memc $key;

srcache_storePUT /memc $key;

 

       root   <OFBIZ_HOME>/baseapps/party/webapp;

       index  index.html index.htm   ;

    }

     #依据uri和參数,在memcached中查找,假设找到就返回给client,找不到就,到磁盘读取文件给client,并存处memcached

 

   location  /catalog/ {

       root   <OFBIZ_HOME>/baseapps/product/webapp;

       index  index.html index.htm   ;

   }  

        

#依据uri和參数,在memcached中查找,假设找到就返回给client,找不到就,到磁盘读取文件给client,并存处memcached

   

location  /images/ {

set $key$uri$args;

srcache_fetchGET /memc $key;

srcache_storePUT /memc $key;

 

       root  <OFBIZ_HOME>/core/images/webapp;

       index  index.html index.htm   ;

   }   

     #依据uri和參数,在memcached中查找,假设找到就返回给client,找不到就,到磁盘读取文件给client,并存处memcached

 

location^~ /backend2013/ {

set $key$uri$args;

srcache_fetchGET /memc $key;

srcache_storePUT /memc $key;

 

       root  <OFBIZ_HOME>/themes/backend2013/webapp;

       index  index.html index.htm   ;

   }     

   

    #redirect server error pages to the static page /50x.html

    #

   error_page  404 500 502 503504  /50x.html;

   location = /50x.html {

       root  <OFBIZ_HOME>/core/images/webapp/errorpage;

    }

   

   #禁止nginx下载ftl,xml,groovy,sh等文件,由于这些文件在ofbiz中可能是我们的程序的组成部分

   location ~* .(ftl|xml|groovy|sh)$

    {

        deny all;

    }

   

   #location ~* .(gif|jpg|jpeg|png|bmp|swf)$

    #{

    #   expires  30d;

    #}

 

    #location~* .(js|css)$

    #{

    #   expires 1h;

    #}

   

}#end server

   

 

}



限制说明:

以上的配置,仅仅是一个初始的配置,还须要再进一步优化,比方,须要差别对待GET和POST方法的处理,不然可能会有数据无法正确更新问题,可是由于时间有限,不再细讲,欢迎大家一起讨论共同进步。

Gorun8电子商城是基于著名的开源项目OFBIZ的,并结合了中国人的使用习惯和审美观点而打造的一款适合中国人用的OFBIZ类电子商务系统。将gorun8打造成稳健、易用、高效电子商务平台。让广大企业用户能够利用gorun8以最低的成本,最快的速度开启电子商务营销之门


原文地址:https://www.cnblogs.com/mengfanrong/p/3870403.html