Linux Apache 第一次响应速度奇慢解决办法

Linux Apache 第一次响应速度奇慢!终于找到了解决办法!

默认安装apache后盖有很多事情要做,一般来说我们要做防盗链,防止列目录,防止多线程下载(防止多线程不是只给一个ip限制一个线程,因为通常我们在下载的同时还要浏览网页,所以限制线程的时候可以多留出余地)

mod_limitipconn.c是一个非官方的apache模块,可以用来作为WEB文件的下载限制,但是它是使用ExtendedStatus On形式,工作在应用层。当同一个IP的连接到达限制的时候,apache对get请求发送:

HTTP/1.1 503 Service Temporarily Unavailable

从而使用户不能下载,但并不能阻止这种攻击,仍旧允许连接的。不过这个东东对网管来说还是很有用的,特别是下载站的网管。简单介绍一下安装(动态模块方式安装,假设你的apache已经装在/usr/local/apache目录下):

官方地址:http://dominia.org/djao/limitipconn.html

点击这里下载:http://dominia.org/djao/limit/mod_limitipconn-0.04.tar.gz

wget http://dominia.org/djao/limit/mod_limitipconn-0.22.tar.gz

# tar -zxvf mod_limitipconn-0.22.tar.gz
# cd mod_limitipconn-0.22
# vi Makefile

修改下面两句:

APXS = apxs

改为

APXS = /usr/local/apache/bin/apxs

APXS = apacheclt

改为

APXS = /usr/local/apache/bin/apacheclt

然后:

# make
# make install

修改apache配置文件:

# vi /usr/local/apache/conf/httpd.conf

ExtendedStatus On #去掉前面的#,一定要去掉很多朋友是找不到这一行请在extra/httpd-info.conf中找找,呵呵,记得把这个文件include进来已经加了下面语句

#make install已经加了下面语句
LoadModule limitipconn_module libexec/mod_limitipconn.so

修改httpd-vhosts.conf,改完后的效果应该是这样的[php]

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /usr/local/apache/htdocs/download.domain.com
    ServerName download.domain.com
    ServerAlias download.domain.com
    ErrorLog logs/download.domain.com-error_log
    CustomLog logs/download.domain.com-access_log common


<IfModule mod_limitipconn.c>
<Location />
MaxConnPerIP 1#只允许一个线程
# 还有其它选项,如:
# NoIPLimit image/*
# OnlyIPLimit audio/mpeg video
# 可以设置多个Location
</Location>
</IfModule>


</VirtualHost>

MaxConnPerIP 3 #限制web根目录同一个IP只能同时开3进程下载

# 还有其它选项,如:
# NoIPLimit image/*
# OnlyIPLimit audio/mpeg video
# 可以设置多个Location

重启apache,用多线程的下载工具下载时,大于3个线程,其余的将得到信息:

HTTP/1.1 503 Service Temporarily Unavailable

或者根据User_Agent判断,把已知的多线程工具都给deny 掉in httpd.conf
.....
BrowserMatch "NetAnt" badguy
BrowserMatch "GetRight" badguy
BrowserMatch "JetCar" badguy
BrowserMatch "Mass Downloader" badguy
BrowserMatch "ReGet" badguy
BrowserMatch "DLExpert" badguy
BrowserMatch "FlashGet" badguy
BrowserMatch "Offline Explorer" badguy
BrowserMatch "Teleport" badguy
...........

order deny,allow
deny from env=badguy
allow from all

注意httpd里面应该有mod_setenvif模块

我用上面的方法成功了,但是从其以后就不行了,研究了一天都不知道是什么原因,被逼无奈使用了别人的方法,我转载下来。

Package: http://dominia.org/djao/limit/这里的安装建议使用动态 DSO 并 patch apache2.0, 以使得 apache2.0 可以认识在代理后方的 IP 。但是要 重新编译 apache2.0, 以下是介绍。

代码:
##Instructions for building DSO with proxy tracking:
# tar xzvf httpd-2.0.39.tar.gz
# tar xzvf mod_limitipconn-0.22.tar.gz
# cd httpd-2.0.39
# patch -p1 < ../mod_limitipconn-0.22/apachesrc.diff
# ./buildconf
# ./configure --enable-so --enable-speling --enable-rewrite --with-ssl=/usr/local/ssl --enable-forward
# make
# make install
# cd ../mod_limitipconn-0.22
# PATH=/usr/local/apache2/bin:$PATH
# make install
安装过程

# lynx http://dominia.org/djao/limit/mod_limitipconn-0.22.tar.gz
# tar -zxvf mod_limitipconn-0.22.tar.gz
# cd httpd-2.0.48
# patch -p1 < ../mod_limitipconn-0.22/apachesrc.diff
patching file configure.in
Hunk #1 succeeded at 373 (offset 55 lines).
patching file include/scoreboard.h
patching file modules/generators/mod_status.c
Hunk #1 succeeded at 746 (offset -1 lines).
patching file server/scoreboard.c
# ./configure --enable-so --enable-speling --enable-rewrite --with-ssl=/usr/local/ssl --enable-forward
# make
# make install
# cd ../mod_limitipconn-0.22
# PATH=/usr/local/apache2/bin:$PATH
# make install
----------------------------------------------------------------------
Libraries have been installed in:
/usr/local/apache2/modules
If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
- add LIBDIR to the `LD_LIBRARY_PATH' environment variable
during execution
- add LIBDIR to the `LD_RUN_PATH' environment variable
during linking
- use the `-Wl,--rpath -Wl,LIBDIR' linker flag
- have your system administrator add LIBDIR to `/etc/ld.so.conf'
See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------
chmod 755 /usr/local/apache2/modules/mod_limitipconn.so
[activating module `limitipconn' in /usr/local/apache2/conf/httpd.conf] 

检查 httpd.conf 文件,发现增加了一下一行

代码:
LoadModule limitipconn_module modules/mod_limitipconn.so 

同时需要设置以下参数在 httpd.conf 中 , 也可以在单个虚拟服务器中。

代码:
ExtendedStatus On
# Only needed if the module is compiled as a DSO
LoadModule limitipconn_module lib/apache/mod_limitipconn.so
<IfModule mod_limitipconn.c>
<Location /somewhere>
MaxConnPerIP 3
# exempting images from the connection limit is often a good
# idea if your web page has lots of inline images, since these
# pages often generate a flurry of concurrent image requests
# NoIPLimit image/*
# In this case, all MIME types other than audio/mpeg and video*
# are exempt from the limit check
# OnlyIPLimit audio/mpeg video
</Location>
</IfModule> 

注意: LoadModule limitipconn_module modules/mod_limitipconn.so 必须放在虚拟服务器之前!否则不能启动。

效果如下:我用flashget开了10线程,你可以看到只有两个线程起作用。

防盗链设置:

1. 在httpd.conf中加载mod_rewrite模块:

去掉#LoadModule rewrite_module modules/mod_rewrite.so 中的#号
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>,将AllowOverride None改成AllowOverride All
2. 在目录下新建一个.htaccess文件

参数说明:

相关推荐