首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 服务器 > Apache >

Apache配备 添加proxy和cache模块

2012-10-19 
Apache配置 添加proxy和cache模块1.Apache安装(添加proxy和cache模块)./configure--prefix/usr/local/apa

Apache配置 添加proxy和cache模块
1.Apache安装(添加proxy和cache模块)

./configure  --prefix=/usr/local/apache2 --enable-mods-shared=most --enable-rewirte --enable-so --enable-ssl=static --with-ssl=/usr/local/ssl  --enable-proxy=shared --enable-proxy-balancer=shared --enable-proxy-http=shared --enable-proxy-ajp --enable-cache --enable-disk-cache --enable-mem-cache --enable-file-cache

2.重写URL,在其后面添加“/”<IfModule rewrite_module>

   RewriteLogLevel 3

   RewriteLog "/usr/local/apache2/logs/rewrite.log"

   RewriteEngine on

   RewriteCond %{REQUEST_URI} ^([^\.]+)$

   RewriteRule ^(.*[^/])$ $1/ [R]

</IfModule>

3.添加proxy

一般来说,负载均衡就是将客户端的请求分流给后端的各个真实服务器,达到负载均衡的目的。还有一种方式是用两台服务器,一台作为主服务器(Master),另一台作为热备份(Hot Standby),请求全部分给主服务器,在主服务器当机时,立即切换到备份服务器,以提高系统的整体可靠性。

负载均衡的设置

Apache可以应对上面这两种需求。先来讨论一下如何做负载均衡。首先需要启用Apache的几个模块:

程序代码

LoadModule proxy_module modules/mod_proxy.so

LoadModule proxy_balancer_module modules/mod_proxy_balancer.so

LoadModule proxy_http_module modules/mod_proxy_http.so

mod_proxy提供代理服务器功能,mod_proxy_balancer提供负载均衡功能, mod_proxy_http让代理服务器能支持HTTP协议。如果把mod_proxy_http换成其他协议模块(如mod_proxy_ftp),或许能支持其他协议的负载均衡,有兴趣的朋友可以自己尝试一下。

然后要添加以下配置:

程序代码

<IfModule proxy_module>

   ProxyRequests Off

   <Proxy balancer://mycluster>

        BalancerMember http://192.168.1.101:8080/EHRproject loadfactor=3



        BalancerMember http://192.168.1.121:8080/EHRproject loadfactor=1 status=+H

 

   #    BalancerMember http://192.168.1.101:8080  

     

   #    BalancerMember http://www.google.com

   #    BalancerMember http://www.baidu.com

 

 

   #     BalancerMember http://192.168.1.101:8080/test

   #byrequests(default),bytraffic,bybusyness

         ProxySet lbmethod=bybusyness

   </Proxy>

   #ProxyPass /EHRproject balancer://mycluster nocanon

    ProxyPass /EHRproject balancer://mycluster

   #ProxyPass /EHRproject http://192.168.1.101:8080/EHRproject/

   #ProxyPass /EHRproject http://192.168.1.101:8080/EHRproject

 

 

   #ProxyPass / balancer://mycluster

  

   #ProxyPass /c http://www.google.com

   #ProxyPass /d http://www.google.cn

   #ProxyPassReverse /EHRproject http://www.google.com

   #ProxyPassReverse /EHRproject balancer://mycluster

   <Location /balancer-manager>

       SetHandler balancer-manager

       order Deny,Allow

       Deny from all

       Allow from all

   </Location>

   <Location /server-status>

    SetHandler server-status

    Order Deny,Allow

    Deny from all

    Allow from all

   </Location>



</IfModule>

从上面的 ProxyRequests Off 这条可以看出,实际上负载均衡器就是一个反向代理,只不过它的代理转发地址不是某台具体的服务器,而是一个 balancer:// 协议:

ProxyPass / balancer://mycluster协议地址可以随便定义。然后,在<Proxy>段中设置该balancer协议的内容即可。 BalancerMember指令可以添加负载均衡组中的真实服务器地址。

下面那段<Location /balancer-manager>是用来监视负载均衡的工作情况的,调试时可以加上(生产环境中禁止使用!),然后访问 http://localhost/balancer-manager/ 即可看到负载均衡的工作状况。

OK,改完之后重启服务器,访问你的Apache所在服务器的地址,即可看到负载均衡的效果了。打开 balancer-manager 的界面,可以看到请求是平均分配的。

如果不想平均分配怎么办?给 BalancerMember 加上 loadfactor 参数即可,取值范围为1-100。比如你有三台服务器,负载分配比例为 7:2:1,只需这样设置:

程序代码

ProxyRequests Off

<Proxy balancer://mycluster>

    BalancerMember http://node-a.myserver.com:8080 loadfactor=7

    BalancerMember http://node-b.myserver.com:8080 loadfactor=2

    BalancerMember http://node-c.myserver.com:8080 loadfactor=1

</Proxy>

ProxyPass / balancer://mycluster

默认情况下,负载均衡会尽量让各个服务器接受的请求次数满足预设的比例。如果要改变算法,可以使用 lbmethod 属性。如:

程序代码

ProxyRequests Off

<Proxy balancer://mycluster>

    BalancerMember http://node-a.myserver.com:8080 loadfactor=7

    BalancerMember http://node-b.myserver.com:8080 loadfactor=2

    BalancerMember http://node-c.myserver.com:8080 loadfactor=1

</Proxy>

ProxyPass / balancer://mycluster

ProxySet lbmethod=bytraffic

lbmethod可能的取值有:

lbmethod=byrequests 按照请求次数均衡(默认)

lbmethod=bytraffic 按照流量均衡

lbmethod=bybusyness 按照繁忙程度均衡(总是分配给活跃请求数最少的服务器)

各种算法的原理请参见Apache的文档。

热备份(Hot Standby)

热备份的实现很简单,只需添加 status=+H 属性,就可以把某台服务器指定为备份服务器:

程序代码

ProxyRequests Off

<Proxy balancer://mycluster>

    BalancerMember http://node-a.myserver.com:8080

    BalancerMember http://node-b.myserver.com:8080 status=+H

</Proxy>

ProxyPass / balancer://mycluster

从 balancer-manager 界面中可以看到,请求总是流向 node-a ,一旦node-a挂掉, Apache会检测到错误并把请求分流给 node-b。Apache会每隔几分钟检测一下 node-a 的状况,如果node-a恢复,就继续使用node-a。

4.添加cache

<IfModule cache_module>

#LoadModule disk_cache_module modules/mod_disk_cache.so
# If you want to use mod_disk_cache instead of mod_mem_cache,
# uncomment the line above and comment out the LoadModule line below.
<IfModule disk_cache_module>

CacheRoot /usr/local/apache2/cache/httpd
CacheEnable disk /
CacheDirLevels 5
CacheDirLength 3

</IfModule>

#LoadModule mem_cache_module modules/mod_mem_cache.so
<IfModule mem_cache_module>

CacheEnable mem /
MCacheSize 4096
MCacheMaxObjectCount 100
MCacheMinObjectSize 1
MCacheMaxObjectSize 2048

</IfModule>

# When acting as a proxy, don't cache the list of security updates
#CacheDisable http://security.update.server/update-list/

</IfModule>

##cache配置# 
##cache文件存放目录 
# CacheRoot "/usr/local/apache2/cache" 
##启用cache调用的url根,也可以是/forum等其他根路径 
# CacheEnable disk / 
#  CacheEnable fd / 
##不启用cache的配置,对/bbsadmin下的应用不进行缓存 
#  CacheDisable /bbsadmin 
##CacheDirLevels指定了子目录的层数,CacheDirLength指定了每级子目录名的字符数。 
# CacheDirLevels 2 
# CacheDirLength 1 
# #缓存所有没有在header中设置Cache-Control或Pragma为no-cache的资源 
#  #CacheIgnoreCacheControl On  
##缓存所有没有在header中设置LastMod的资源 
#  CacheIgnoreNoLastMod On 
##设置缓存的最大失效时间,默认为86400(1天),单位是秒 
#  CacheMaxExpire 300 
##用来从回应里 Last Modified 资讯算出 expire date 
#  CacheLastModifiedFactor 0.05
#
#      #一个连接的最大请求数量                                              
#      MaxKeepAliveRequests 10000                                           
#      #NT环境,只能配置这个参数来提供性能                                  
#      <IfModule mpm_winnt.c>                                               
#      #每个进程的线程数,最大1920。NT只启动父子两个进程,不能设置启动多个进程
#      ThreadsPerChild 1900                                                 
#      每个子进程能够处理的最大请求数                                       
#      MaxRequestsPerChild 10000                                            
#      </IfModule>
    
  5.在/usr/local/apache2下建立两个目录cache、httpd。

cache、httpd目录的权限为777

否则报以下错误:

[Thu Aug 27 10:10:19 2009] [debug] mod_cache.c(131): Adding CACHE_SAVE filter for /dean.html
[Thu Aug 27 10:10:19 2009] [debug] mod_cache.c(138): Adding CACHE_REMOVE_URL filter for /dean.html
[Thu Aug 27 10:10:19 2009] [debug] mod_cache.c(639): cache: Caching url: /dean.html
[Thu Aug 27 10:10:19 2009] [debug] mod_cache.c(645): cache: Removing CACHE_REMOVE_URL filter.
[Thu Aug 27 10:10:19 2009] [debug] mod_cache.c(812): (13)Permission denied: cache: updating headers with store_headers failed. Removing cached url.

6.如果报以下错误

[Thu Aug 27 10:25:54 2009] [debug] mod_cache.c(131): Adding CACHE_SAVE filter for /dean.html
[Thu Aug 27 10:25:54 2009] [debug] mod_cache.c(138): Adding CACHE_REMOVE_URL filter for /dean.html
[Thu Aug 27 10:25:54 2009] [debug] mod_cache.c(528): cache: /dean.html not cached. Reason: HTTP Status 304 Not Modified

修改一下dean.html请求页面的内容,再重新请求,就解决了,呵呵!

原文:http://dinghaoliang.blog.163.com/blog/static/126540714200972710291670/

热点排行