设为首页收藏本站

LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区!

 找回密码
 注册

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 4053|回复: 9

[Apache] apache如何限制并发数 IP 带宽? [复制链接]

LT管理团队

社区首席勤杂工

Rank: 9Rank: 9Rank: 9

签到
81
注册时间
2008-5-19
最后登录
2012-5-3
在线时间
1762 小时
阅读权限
200
积分
14979
帖子
2524
主题
1200
精华
25
UID
1

LT总司令

发表于 2008-6-11 22:21:26 |显示全部楼层
限制并发数
下载模块:

到官方网址: http://www.nowhere-land.org/programs/mod_vhost_limit/下载模块

http://www.nowhere-land.org/prog ... st_limit-0.4.tar.gz

安装:
apxs -c mod_vhost_limit.c -o /path/to/libexec/mod_vhost_limit.so

在 httpd.conf 加入:

LoadModule vhost_limit_module libexec/mod_vhost_limit.so
AddModule mod_vhost_limit.c

配置:

MaxClients 150
ExtendedStatus On

NameVirtualHost *

<VIRTUALHOST * />
    ServerName       server1
    DocumentRoot     /some/where/1
    MaxVhostClients  100


<VIRTUALHOST * />
    ServerName       server2
    DocumentRoot     /some/where/2
    MaxVhostClients  30


<VIRTUALHOST * />
    ServerName       server3
    DocumentRoot     /some/where/3


其中: server1 被限制为 100 个并发线程数。 server2 被限制为 30 个并发线程数。 server3 没有被限制。

注:需 mod_status 的 ExtendedStatus On 支持!!

如超出限制的并发数在客户端就会出现503错误

----------------------------------------------------------------------------------------------

限制IP连接数

到这里下载模块 http://dominia.org/djao/limit/mod_limitipconn-0.04.tar.gz

安装:
tar zxvf mod_limitipconn-0.04.tar.gz
cd mod_limitipconn-0.04
make APXS=/usr/local/apache/bin/apxs  ß-----这里要按你自己的路径设置
make install APXS=/usr/local/apache/bin/apxs ß-----这里要按你自己的路径设置

编辑httpd.conf
添加
全局变量:
  < IfModule mod_limitipconn.c >
      < Location / >   # 所有虚拟主机的/目录
          MaxConnPerIP 3     # 每IP只允许3个并发连接
          NoIPLimit image/*  # 对图片不做IP限制
    < /Location >

  < Location /mp3 >  # 所有主机的/mp3目录
    MaxConnPerIP 1         # 每IP只允许一个连接请求   
    OnlyIPLimit audio/mpeg video    # 该限制只对视频和音频格式的文件
    < /Location >
< /IfModule >

  

或者虚拟主机的:
< VirtualHost xx.xxx.xx.xx > ##ip 地址
    ServerAdmin easy@phpv.net
    DocumentRoot /home/easy
    ServerName www.phpv.net
  < IfModule mod_limitipconn.c >
      < Location / >   
      MaxConnPerIP 5        
          NoIPLimit image/*      
      < /Location >
      < Location /mp3 >    # 所有主机的/mp3目录
      MaxConnPerIP 2         # 每IP只允许一个连接请求   
      OnlyIPLimit audio/mpeg video # 该限制只对视频和音频格式的文件
      < /Location >
  < /IfModule >
  < /VirtualHost >



----------------------------------------------------------------------------------------------

限制带宽:

下载模块 ftp://ftp.cohprog.com/pub/apache/module/1.3.0/mod_bandwidth.c
安装:
/usr/local/apache/bin/apxs -c ./mod_bandwidth.c -o /usr/local/apache/libexec/mod_bandwidth.so  



<-------以上/usr/local/apache请设置为你的路径

编辑httpd.conf
添加:
LoadModule bandwidth_module libexec/mod_bandwidth.so
AddModule mod_bandwidth.c

重启你的apache

相关文档:

Global configuration directives :

    * BandWidthDataDir
      Syntax : BandWidthDataDir <directory>
      Default : "/tmp/apachebw"
      Context : server config

Sets the name of the root directory used by mod_bandwidth to store its internal temporary information. Don't forget to create the needed directories : <directory>/master and <directory>/link

    * BandWidthModule
      Syntax : BandWidthModule <On|Off>
      Default : Off
      Context : per server config

Enable or disable totaly the whole module. By default, the module is disable so it is safe to compile it in the server anyway.

PLEASE, NOTE THAT IF YOU SET A BANDWIDTH LIMIT INSIDE A VIRTUALHOST BLOCK, YOU ALSO __NEED__ TO PUT THE "BandWidthModule On" DIRECTIVE INSIDE THAT VIRTUALHOST BLOCK !

IF YOU SET BANDWIDTH LIMITS INSIDE DIRECTORY BLOCKS (OUTSIDE OF ANY VIRTUALHOST BLOCK), YOU ONLY NEED TO PUT THE "BandWidthModule On" DIRECTIVE ONCE, OUTSIDE OF ANY VIRTUALHOST OR DIRECTORY BLOCK.

    * BandWidthPulse
      Syntax : BandWidthPulse <microseconds>
      Default :
      Context : per server config

Change the algorithm used to calculate bandwidth and transmit data. In normal mode (old mode), the module try to transmit data in packets of 1KB. That mean that if the bandwidth available is of 512B, the module will transmit 1KB, wait 2 seconds, transmit another 1KB and so one.

Seting a value with "BandWidthPulse", will change the algorithm so that the server will always wait the same amount of time between sending packets but the size of the packets will change. The value is in microseconds. For example, if you set "BandWidthPulse 1000000" (1 sec) and the bandwidth available is of 512B, the sever will transmit 512B, wait 1 second, transmit 512B and so on.

The advantage is a smother flow of data. The disadvantage is a bigger overhead of data transmited for packet header. Setting too small a value (bellow 1/5 of a sec) is not realy useful and will put more load on the system and generate more traffic for packet header.

Note also that the operating system may do some buffering on it's own and so defeat the purpose of setting small values.

This may be very useful on especialy crowded network connection : In normal mode, several seconds may happen between the sending of a full packet. This may lead to timeout or people may believe that the connection is hanging. Seting a value of 1000000 (1 sec) would guarantee that some data are sent every seconds...
感谢大家对linuxtone的支持!

LT管理团队

向Linuxsir的哈密瓜大哥看齐 ...

Rank: 9Rank: 9Rank: 9

注册时间
2008-6-5
最后登录
2012-5-23
在线时间
1624 小时
阅读权限
200
积分
34138
帖子
715
主题
205
精华
4
UID
46

LT同学

发表于 2008-6-13 18:34:43 |显示全部楼层

使用道具 举报

Rank: 1

注册时间
2008-6-18
最后登录
2008-9-12
在线时间
11 小时
阅读权限
10
积分
6
帖子
11
主题
1
精华
0
UID
88
发表于 2008-8-7 13:51:34 |显示全部楼层

使用道具 举报

Rank: 7Rank: 7Rank: 7

注册时间
2008-8-16
最后登录
2011-5-19
在线时间
199 小时
阅读权限
100
积分
604
帖子
87
主题
26
精华
2
UID
209

LT粉丝

发表于 2008-9-8 09:09:29 |显示全部楼层
可以的,支持下~~~

使用道具 举报

Rank: 7Rank: 7Rank: 7

注册时间
2008-9-3
最后登录
2008-9-9
在线时间
2 小时
阅读权限
100
积分
47
帖子
13
主题
0
精华
0
UID
244
发表于 2008-9-8 10:10:30 |显示全部楼层
原来是自问自答啊,学习了!!!

使用道具 举报

LT管理团队

守住每一天

Rank: 9Rank: 9Rank: 9

注册时间
2008-5-30
最后登录
2012-5-23
在线时间
641 小时
阅读权限
200
积分
16234
帖子
1273
主题
176
精华
11
UID
31
发表于 2008-9-8 11:07:23 |显示全部楼层
顶下.
好文章哈.
如此限制非常不错.
<-sina_sign,1054764633,7->

使用道具 举报

Rank: 2

注册时间
2010-5-17
最后登录
2010-7-8
在线时间
5 小时
阅读权限
20
积分
53
帖子
80
主题
0
精华
0
UID
9245
发表于 2010-5-23 00:59:18 |显示全部楼层

使用道具 举报

Rank: 6Rank: 6

注册时间
2009-3-27
最后登录
2012-5-21
在线时间
123 小时
阅读权限
70
积分
2738
帖子
216
主题
9
精华
0
UID
2172

LT粉丝

发表于 2010-7-19 10:03:11 |显示全部楼层
看了下好像只能限制下载带宽 如果要限制上传的带宽呢?
人生最杯具的是找了个你不爱的人 而且这个人也不爱你

使用道具 举报

Rank: 8Rank: 8

注册时间
2010-7-1
最后登录
2012-5-23
在线时间
336 小时
阅读权限
90
积分
17119
帖子
184
主题
23
精华
0
UID
9673
发表于 2010-7-20 09:26:41 |显示全部楼层
恩 支持
》》》》》

使用道具 举报

Rank: 6Rank: 6

注册时间
2009-9-27
最后登录
2012-4-27
在线时间
78 小时
阅读权限
70
积分
1266
帖子
120
主题
22
精华
0
UID
4731
发表于 2010-8-4 17:42:34 |显示全部楼层
嘿嘿  不错

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

IT运维专家网感谢红之盟网络提供带宽支持

合作联系: QQ:67888954/MSN:cnseek@msn.com/mail:netseek@linuxtone.org

Archiver|手机版|感谢所有关心和支持过LinuxTone的朋友们 转载本站内容请注明原作者名及出处 ( 京ICP备08103151 )   |

GMT+8, 2012-5-23 10:35 , Processed in 0.157296 second(s), 12 queries , Memcache On.

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部