Posts Tagged: Apache


5
一 11

Debian/Ubuntu上的Apache管理脚本

a2enmod, a2dismod, a2ensite, a2dissite

a2enmod/a2dismod  启用/禁用模块
a2ensite/a2dissite 启用/禁用虚拟站点

实际上,另外三个文件只是一个指向 a2enmod 的软链接。

Debian与Ubuntu上自带的Apache配置文件,是有自己独特的组织风格的(SUSE也独特),于是想应该有自己的“独特”的管理方式。man -k apache了一下,发现这四个“工具”。

用法非常简单,不跟参数运行,有会“向导”,也可直接跟模块名,或者site名。


24
二 10

使用expires模块降低apache负载

使用expires模块声明静态文件过期时间-减少客户端不必要的请求

<Location />
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/* “access plus 1 month”
ExpiresByType text/css “access plus 1 month”
ExpiresByType text/javascript   “access plus 1 month”
ExpiresByType application/x-javascript “access plus 1 month”
</IfModule>
</Location>
另一种方式:
<FilesMatch “\.(css|js|jpg|gif|png)$”>
<IfModule mod_expires.c>
ExpiresActive  On
ExpiresDefault A2592000
</IfModule>
</FilesMatch>

3
二 10

编译安装Apache

温习编译Apahce的相关知识

预备知识

Makefile的惯例

  1. make clean 清除当前目录下在 make 过程中产生的文件。它不能删除软件包的配置文件,也不能删除 build 时创建的那些文件。
  2. make distclean 类似于”clean”,但增加删除当前目录下的的配置文件、build 过程产生的文件。
  3. make install-strip 和”make install”类似,但是会对复制到安装目录下的可执行文件进行 strip 操作。

make的常用选项

  1. -jN , –jobs[=N],指定并行执行的命令数目。
  2. -n –just-print,–dry-run,–recon,只打印出所要执的命令,但并不实际执行命令。
  3. -s –silent,–quit,不显示所执行的命令。

编译Apache的要求

  1. 磁盘空间
  2. ANSI-C编译器及编译环境
  3. 确保准确的时间
  4. Perl 5 [可选]
  5. apr/apr-util >= 1.2

apr和apr-util包含在Apache httpd的发行源代码中,并且在绝大多数情况下使用都不会出现问题。当然,如果apr或apr-util的1.0或1.1版本已经安装在你的系统中了,则必须将你的apr/apr-util升级到1.2版本,或者将httpd单独分开编译。要使用发行源代码中自带的apr/apr-util源代码进行安装,你必须手动完成:
# 编译和安装 apr 1.2
cd srclib/apr
./configure –prefix=/usr/local/apr-httpd/
make
make install

# 编译和安装 apr-util 1.2
cd ../apr-util
./configure –prefix=/usr/local/apr-util-httpd/ –with-apr=/usr/local/apr-httpd/
make
make install

# 配置 httpd
cd ../../
./configure –with-apr=/usr/local/apr-httpd/ –with-apr-util=/usr/local/apr-util-httpd/

Apache的模块状态

通常我们认为Apache的模块分五类:多路处理模块(MPM),基本模块(Base),扩展模块(Extension),实验性模块(Experimental),第三方模块(External)。
关于各模块的状态详情可以查看手册。第三方模块不包含在发行版中,手册中只标识了四种状态:

M 多路处理模块 必须有且仅有一个MPM被静态编译到服务器中。
B 基本模块 默认包含,必须明确禁用。
E 扩展模块 默认不包含,必须明确启用。
X 试验模块 默认不包含,必须明确启用。

Apache的基本模块

Apache 2.2.14 默认被静态编译进httpd的模块:

核心模块

不可或缺
core.c
http_core.c
mod_so.c
prefork.c(Linux上默认是prefork.c)

认证相关模块

mod_authn_file.c
mod_authn_default.c
mod_authz_host.c
mod_authz_groupfile.c
mod_authz_user.c
mod_authz_default.c
mod_auth_basic.c

其它模块

mod_include.c
mod_filter.c
mod_log_config.c
mod_env.c
mod_setenvif.c
mod_version.c
mod_mime.c
mod_status.c
mod_autoindex.c
mod_asis.c
mod_cgi.c
mod_negotiation.c
mod_dir.c
mod_actions.c
mod_userdir.c
mod_alias.c

我的Apache的常用模块

最简单的认证模块组合

[B]mod_auth_basic.c 或
[E]mod_auth_digest.c 加 (2.2.14版时,已经由实验模块转为扩展模块)
[B]mod_authn_file.c 加
[B]mod_authz_user.c
注意!basic加密方式密码是明文传送的,不安全,建议使用digest方式的认证。

保护认证而加载的模块

[B]mod_authn_default.c
[B]mod_authz_default.c
出于安全考虑,强烈建议加载!

最常用的访问控制模块

[B]mod_authz_host.c
提供基与主机名、IP地址以及请求特征的访问控制。

其它常用模块

[B]mod_log_config.c
[B]mod_alias.c
[B]mod_dir.c
[B]mod_mime.c
[B]mod_setenvif.c
[E]mod_rewrite.c
[E]mod_deflate.c
[E]mod_expires.c
[E]mod_headers.c

我常用的编译指令


1. ./configure --prefix=/opt/httpd-2.2.14 --with-mpm=prefork --enable-mods-shared=all LDFLAGS='-s'
2. ./configure --prefix=/opt/httpd-2.2.14 --with-mpm=prefork --enable-mods-shared=all --enable-cache --enable-mem-cache --enable-ssl --enable-file-cache --enable-disk-cache --enable-proxy --enable-proxy-http LDFLAGS='-s'
3. ./configure --prefix=/opt/httpd-2.2.14 --with-mpm=prefork --enable-mods-shared=all --enable-dir=static --enable-authz-host=static --enable-auth-basic=static --enable-authn_file=static --enable-authz_user=static --enable-authn-default=static --enable-authz-default=static --enable-setenvif=static --enable-alias=static --enable-log-config=static --enable-deflate=static --enable-rewrite=static --enable-mime=static --enable-expires=static --enable-cache=static --enable-mem-cache=static --enable-headers=shared --enable-disk-cache=shared --enable-file-cache=shared LDFLAGS='-s'

注意:因为Apache的./configure生成的Makefile不支持make install-strip,所以使用 LDFLAGES=’-s’来编译被strip的代码。

参考:
深入理解软件包的配置、编译与安装
Apache 2.2 手册-编译与安装
Apache 2.2 手册-动态共享对象支持
Apache 2.2 手册-各模块的简介描述
Apache 2.2 手册-描述模块的术语
Apache 2.2 手册-指令速查


3
二 10

mod_authn_default 和 mod_authz_default

mod_authn_default
该模块是一个失败补救(fallback)模块,它在未正确配置认证模块(比如mod_auth_basic缺失mod_authz_user模块的,语法检查检查不出来)的情况下简单拒绝一切认证信息。起到保护数据的作用。

mod_authz_default
在未正确配置授权支持模块的情况下简单拒绝一切授权请求。同样起到保护数据的作用。

假如没有加载default模块,那么,错误的认证配置,会使服务端产生一个500状态码,默认情况下,会生成一个“The server encountered an internal error or misconfiguration and was unable to complete your request.”的提示,有助于管理员调试

假如加载了default模块,那么错误的认证配置,会使客户端始终不能通过认证,最终返回一个401状态码。

出于安全考虑,这两个模块在启用认证的情况下,一定要加载!


1
二 10

修改Apache的Max open files限制

相信绝大多数人,都是直接对 /etc/security/limits.conf  文件进行的修改,添加类似下面的两行:

*     soft         nofile           10240
*     hard       nofile           10240

这样也能达到效果。但是经验告诉我,能在局部修改,就不要动全局——因为我们不好评估全局会对哪些应用造成影响(就修改nofile参数一事来说,目前还没有发现修改全局对系统的明显影响,但是理论上可以肯定,将全局的限制放宽,意味着使系统容易受到“资源耗尽”类的攻击,然而在我的工作经历中,还没有遇到一例类似事故发生过),所以我建议将这些局部性的修改放到各自的启动脚本里。

在apachectl脚本里,有这样的语句:

#
# Set this variable to a command that increases the maximum
# number of file descriptors allowed per child process. This is
# critical for configurations that use many file descriptors,
# such as mass vhosting, or a multithreaded server.
ULIMIT_MAX_FILES=”ulimit -S -n `ulimit -H -n`”
# ——————–                              ——————–
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

# Set the maximum number of file descriptors allowed per child process.
if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
    $ULIMIT_MAX_FILES
fi

所以,我推荐直接修改apachectl,在ULIMIT_MAX_FILES=”ulimit -S -n `ulimit -H -n`” 之前,设定nofile的“硬限制”,如下:

# number of file descriptors allowed per child process. This is
# critical for configurations that use many file descriptors,
# such as mass vhosting, or a multithreaded server.
ulimit -H -n 5000
ULIMIT_MAX_FILES=”ulimit -S -n `ulimit -H -n`”
# ——————–                              ——————–
# ||||||||||||||||||||   END CONFIGURATION SECTION  ||||||||||||||||||||

# Set the maximum number of file descriptors allowed per child process.
if [ "x$ULIMIT_MAX_FILES" != "x" ] ; then
    $ULIMIT_MAX_FILES
fi

这样修改的好处在于,只有Apache的“nofile”受到影响,不影响其它。
原公司的所有应用的管理脚本中(apachectl、nginxctl),都进行了修改,并且使用”isystem”(自己写的一套脚本、小程序的集合)进行统一管理,以至于到后来,我几乎已经忘掉了这些事情,但是系统仍然运行良好,没有再出过 “too many open files” 的问题。

注意:修改过nofile的hard限制以后,需要先stop,再start,httpd进程的限制才能使用新改的参数。

查看当前运行中的进程的limit信息,可以利用/proc(当前Linux内存为2.6),比如apache的一个进程号是10232:

cat /proc/10232/limits


1
二 10

Apache的认证

Apache的认证模型

Apache的认证模块分成三个部分:认证类型模块,认证支持模块,认证授权模块。

认证类型模块(auth):
mod_auth_basic
mod_auth_digest

认证支持模块(authn):
mod_authn_alias
mod_authn_anon
mod_authn_file
mod_authn_dbd
mod_authn_dbm
mod_authn_default
mod_authnz_ldap

认证授权模块(authz):
mod_authnz_ldap
mod_authz_dbm
mod_authz_default
mod_authz_user
mod_authz_groupfile
mod_authz_owner

注意:
mod_authnz_ldap模块即包含认证功能也提供授权功能。mod_authn_alias本身并实现认证功能,但是允许其它认证支持模块以更灵活的方式进行配置。
mod_authz_host 模块提供基于主机名、IP地址、请求特征的访问控制,但并不属于认证支持系统。

一般常用的AuthType 是”Basic/Digest”,这个认证需要用到认证模块mod_auth_basic/mod_auth_digest,和认证支持模块mod_authn_file和认证授权模块mod_authz_user。

就是说,最常用的认证组合方式是:
mod_auth_basic/mod_auth_digest
    +
mod_authn_file
    +
mod_authz_user

即是说,一个使用认证以及访问控制(支持Order Allow,Deny ,Allow from all这样的指令)Apache系统,至少要用到下面四或者五个模块:
mod_auth_basic/mod_auth_digest
    +
mod_authn_file
    +
mod_authz_user
    +
mod_authz_host

不太熟悉Apache认证的管理员,为了优化Apache占用的内存,往往会注释掉一些用不到的模块,而又因为不明白认证模块之间的关系,而导致认证总是不能成功,而apache(目前我使用的版本:2.2.3)的语法检查检查不出来mod_authz_user模块的缺失,故常常使管理员困惑。

重要提醒
Basic认证类型不加密来自用户浏览器的密码,因此不应该用于保护敏感数据。对于敏感的数据,应使用Digest方式来进行认证(比较老的浏览器不支持,不过时至今日,应该已经不是问题了)。

参考:http://www.bsdmap.com/UNIX_html/ApacheMenu_zh_CN/howto/auth.html


1
二 10

快速了解Apache环境

当前使用的Apache的版本为2.2.3,支持以下使用方式:
apachectl -v
apachectl -V
apachectl -l
apachectl -L
apachectl -S
apachectl -M

apachectl -V

1. 查看Server version。
2. 查看编译时间。
3. APR 及 APR-Util 的版本。
4. 硬件架构/平台(64-bit/32-bit)
5. 查看 Server MPM(Prefort/Worker)
6. 是否支持线程(threaded)
7. 定位 HTTPD_ROOT
8. 定位配置文件
9. ……

apachectl -l

静态编译进httpd程序的模块。比如:
Compiled in modules:
core.c
prefork.c
http_core.c
mod_so.c
这个命令也可以用来查看 Server MPM,一般情况下,要么是prefork.c,要么是worker.c。

apachectl -L

查看目前Apache支持的指令,其中不包括加载模块后模块支持的指令。
apachectl -L | grep ^[A-Z]

apachectl -S / apachectl -t -D DUMP_VHOSTS

查看Apache下配置的虚拟主机情况。注间这里的“default server”是有特殊作用的。有时候我们需要专门统过调整Vhost配置段的位置或者vhosts配置文件被加载的顺序来调整、设立“default server”。

apachectl -M / apachectl -t -D DUMP_MODULES

查看apache目前配置里加载的模块。


11
五 09

Apache关闭ETag头儿

在主配置文件段里添加了

FileETag None

但实际上并不总是有效,原因还没有找到。于是使用下面的方式:

<IfModule mod_headers.c>
header unset Etag
</IfModule>

可行。


25
三 09

使用ssl加密http协议

使用mod_ssl模块,使Apache可以使用SSL协议加密HTTP协议,用来保护HTTP会话。

最简洁的配置样例:

<VirtualHost _default_:443>
ServerName    www.bsdmap.com
DocumentRoot  /opt/httpd/htdocs
SSLEngine      on
SSLCertificateFile        /opt/httpd/conf/ssl/www.bsdmap.com.cert
SSLCertificateKeyFile   /opt/httpd/conf/ssl/www.bsdmap.com.key
</VirtualHost>

SSLEngine                 ON 启动SSL引擎
SSLCertificateFile       指定证书
SSLCertificateKeyFile  指定相关密钥

使用SSL加密分为正式场合,比如网上银行、支付宝等对应用安全要求较高的应用上;另一个为非正试场合,比如自己也可以配置一个SSL的web服务。区别在于,正式场合https应用使用的SSL证书是经过第三方数字签名的,而非正式应用,则是非正式机构或者是自己制作、签名的。

成生密钥对儿:

在创建证书之前,要有一个共公/私有密钥对儿。以www.bsdmap.com为例:

openssl genrsa -des3 -rand file1:file2:file3 -out www.bsdmap.com.key 1024

genrsa:告诉openssl要生成一个RSA的密钥对儿。

des3:表示私钥应该被一个”pass phrase”编码和保护。

rand:给OpenSSL提供任意的、随机的数据以确保生成的密钥是唯一的和不可预知的。这个开关在Windows里没有必要,在Windows下使用其他的方式获得随机数据。

out:保存位置。

1024:密钥位数。

当使用了-des3选项,成生了受“口令”保护的密钥对儿时,启动apache时需要输入口令才能启动。假如不想要每次都输入“口令”(不安全),可以使用不受“口令”保护的密钥,去掉-des3选项即可,也可以使用下面的命令转换一下:

openssl rsa -in www.bsdmap.com.key -out www.bsdmap.com.key.nopassword

显示密钥文件的信息:

openssl rsa -noout -text -in www.bsdmap.com.key

该命令也可用于查看ssh-key的信息,其实质是一样的。

 

创建一个证书签署请求(正式应用场合):

正式场合的应用,需要一个经证书颁发机构签署的证书,为此,我们先成生一个证书,提交由第三机构签名认证。使用命令:

openssl req -new -key www.bsdmap.com.key -out www.bsdmap.com.csr

需要填写一些地域、组织机构类的信息。需要特殊说明的是:在一个网站站书里,Common Name (eg, YOUR name)一栏标识完全限定的域名(FQDN),假如此处与以后使用证书的站点名不同,浏览器将发布一个错误,无法访问。

完成之后,证书就存储在www.bsdmap.com.csr里。下面的命令可以显示这个证书的信息:

openssl req -noout -text -in www.bsdmap.com.csr

然后,可以所这个证书签署请求提交给一个CA(证书签署机构)处理。

Verisign和Thawte是两家可以选择的CA:

https://digitalid.verisign.com/server/apacheNotice.htm

https://www.thawte.com/ssl-digital-certificates/buy-ssl-certificates/

创建自签署证书(非正式应用场合):

非正式的应用,可以自己签署自己的证书:

openssl x509 -req -days 365 -in www.bsdmap.com.csr -signkey www.bsdmap.com.key -out www.bsdmap.com.cert

并且,必须使用:chmod 400 www.bsdmap.com.key 来保护密钥文件。

另一个问题:

貌似SSL不能与基于主机名的虚拟主机一同工作。

有一个协议把一个已有的HTTP连接升级到TLS,但是并不是所有的浏览器支持此项功能(RFC 2817)。

参考:
SSL原理解析

http://www.yesky.com/ServerIndex/77125243130347520/20040426/1791592.shtml

SSL原理解密

http://fanqiang.chinaunix.net/a6/b8/20010608/121000441.html


3
九 08

LAMP架构中的关键瓶颈在哪里

以下为引用:

我的想法源起于这样一个事情,有一次一个网站的技术总监问我,为什么他们的网站那么慢,要怎么办。当时,我的MSN里Zend总部的工程师正好在线,我就 问他PHP响应比较慢了,怎么办?他当时直接告诉我,数据库问题!肯定是数据库没有优化设计好。所以,我没有给那个技术总监确切的答案了,因为他们的数据 库设计我们是不能涉及的。所以就给了大概的数据库优化的建议。这样的事情屡次发生,我就开始怀疑,为什么Zend总部的工程师每次都跟我 说是数据库的问题呢,难道我们不能从PHP层面来解决这个问题吗?答案是不能!因为PHP目前的运行速度已经是很快了,通过Zend的性能分析也能看到一 个用户的点击,PHP的运行时间只有10%不到,那PHP在干吗?它在等。等数据库的查询结果。这个方面在目前的PHP产品中有了很大 的提高,那就是Caching和网页静态化两个方案。Caching可能大家会比较陌生,但是网也静态化现在连PHP产品的用户都非常清楚了。速度快、容 易被搜索到等等,好处不言而喻。开玩笑地说,现在网站的主页实现网页静态化只需要硬盘足够大。至于Caching就比较复杂些,也是大多数PHPer感到 头疼的地方。甚至于有些人会用C来实现。因为Caching中的数据有效期验证、查找、提取、更新等等都是比较难处理。当然,也有人会用数据库来处理 Caching问题。


9
七 08

Apache–访问日志中不记录对图片的请求

Apache–访问日志中不记录对图片的请求

本示例将避免对图片的请求出现在访问日志中。 你可以随便改改就将它用于避免特定目录或特定主机的请求被记入日志。

SetEnvIf Request_URI \.gif image-request
SetEnvIf Request_URI \.jpg image-request
SetEnvIf Request_URI \.png image-request
CustomLog logs/access_log common env=!image-request


24
六 08

服务器网页缓存的深入分析

转自:http://www.yuanma.org/data/2007/0909/article_2851.htm

Expires、Cache-Control、Last-Modified、ETag是RFC
2616(HTTP/1.1)协议中和网页缓存相关的几个字段。前两个用来控制缓存的失效日期,后两个用来验证网页的有效性。要注意的是,
HTTP/1.0有一个功能比较弱的缓存控制机制:Pragma,使用HTTP/1.0的缓存将忽略Expires和Cache-Control头。我们
这里以Apache2.0服务器为例,只讨论HTTP/1.1协议。

Expires
Expires字段声明了一个网页或URL地址不再被浏览器缓存的时间,一旦超过了这个时间,浏览器都应该联系原始服务器。RFC告诉我们:“由于推断的失效时间也许会降低语义透明度,应该被谨慎使用,同时我们鼓励原始服务器尽可能提供确切的失效时间。”
对于一般的纯静态页面,如html、gif、jpg、css、js,默认安装的Apache服务器,不会在响应头添加这个字段。Firefox浏览器接受
到相应后,如果发现没有Expires字段,浏览器根据文件的类型和“Last-Modified”字段来推断出一个合适的失效时间,并存储在客户端。推
测出的时间一般是接受到响应时间后的三天左右。
Apache的expires_module模块可以在Http响应头部自动加上Expires字段。在Apache的httpd.conf文件中进行如下配置:
#启用expires_module模块
LoadModule expires_module modules/mod_expires.so
# 启用有效期控制
ExpiresActive On
# GIF有效期为1个月
ExpiresByType image/gif A2592000
# HTML文档的有效期是最后修改时刻后的一星期
ExpiresByType text/html M604800
#以下的含义类似
ExpiresByType text/css “now plus 2 month”
ExpiresByType text/js “now plus 2 day”
ExpiresByType image/jpeg “access plus 2 month”
ExpiresByType image/bmp “access plus 2 month”
ExpiresByType image/x-icon “access plus 2 month”
ExpiresByType image/png “access plus 2 month”
对于动态页面,如果在页面内部没有通过函数强制加上Expires,例如header(”Expires: ” . gmdate(”D,
d M Y H:i:s”) . ” GMT”),Apache服务器会把Wed, 11 Jan 1984 05:00:00
GMT作为Expires字段内容,返回给浏览器。即认为动态页面总是失效的。而浏览器仍然会保存已经失效的动态页面。
可以发现Firefox浏览器总是缓存所有页面,不管失效、不失效还是没有声明失效时间。即使缓存中声明了一个网页的实效日期是
1970-01- 01
08:00:00,浏览器仍然会发送该文件在缓存中的Last-Modified和ETag字段。如果在服务器端验证通过,返回304状态,浏览器就还会
使用此缓存。
Cache-Control
Cache-Control字段中可以声明多些元素,例如no-cache, must-revalidate,
max-age=0等。这些元素用来指明页面被缓存最大时限,如何被缓存的,如何被转换到另一个不同的媒介,以及如何被存放在持久媒介中的。但是任何一个
Cache-Control指令都不能保证隐私性或者数据的安全性。“private”和“no-store”指令可以为隐私性和安全性方面提供一些帮
助,但是他们并不能用于替代身
份验证和加密。
Apache的mod_cern_meta模块允许文件级Http响应头部的控制,同时它也可以配置Cache-Control头(或任何其他头)。响应
头文件是放在原始目录的子目录中,根据原始文件名所命名的一个文件。具体用法请参阅Apache的官方网站。其中Cache-Control :
max-age表示失效日期。如果没有启动mod_cern_meta模块,Apache服务器会把Expires字段中的日期换算成以秒为单位的一个
delta值,赋值给max-age。如果启动mod_cern_meta模块,并且配置了max-age值,Apache会将这个覆盖Expires字
段。同时,max-age隐含了Canche-Control: public。这样浏览器接受到的Cache-Control :
max-age和Expires值就是一致的。
如果失效日期Cache-Control : max-ag=0或者是负值,浏览器会在对应的缓存中把Expires设置为1970-01-01 08:00:00。
Last-Modified
Last-Modified和ETag是条件请求(Conditional
Request)相关的两个字段。如果一个缓存收到了针对一个页面的请求,它发送一个验证请求询问服务器页面是否已经更改,在HTTP头里面带上”
ETag”和”If Modify Since”头。服务器根据这些信息判断是否有更新信息,如果没有,就返回HTTP
304(NotModify);如果有更新,返回HTTP 200和更新的页面内容,并且携带新的”ETag”和”LastModified”。
使用这个机制,能够避免重复发送文件给浏览器,不过仍然会产生一个HTTP请求。
一般纯静态页面本身都会有Last-Modified信息,Apache服务器会读取页面文件中的Last-Modified信息,并添加到http响应头部。
对于动态页面,如果在页面内部没有通过函数强制加上Last-Modified,例如header(”Last-Modified:
” . gmdate(”D, d M Y H:i:s”) . ”
GMT”),Apache服务器会把当前时间作为Last-Modified,返回给浏览器。
无论是纯静态页面还是动态页面,Firefox浏览器巧妙地按照接受到服务器响应的时间设置缓存页面的Last-Modified,而不是按照http响应头部中的Last-Modified字段。
ETag
既然有了Last-Modified,为什么还要用ETag字段呢?因为如果在一秒钟之内对一个文件进行两次更改,Last-Modified就会不正确。因此,HTTP/1.1利用Entity Tag头提供了更加严格的验证。
Apache服务器默认情况下,会对所有的静态、动态文件的响应头添加ETag字段。在Apache的httpd.conf文件中可以通过FileETag指令配置该选项。
FileETag指令配置了当文档是基于一个文件时用以创建 Etag(entity tag)响应头的文件的属性。在Apache
1.3.22及以前,ETag的值是对文件的索引节(INode),大小(Size)和最后修改时间(MTime)进行Hash后得到的。如果一个目录的
配置包含了‘FileETag INode MTime Size’而其一个子目录包含了‘FileETag
-INode’那么这个子目录的设置(并会被其下任何没有进行覆盖的子目录继承)将等价于‘FileETag MTime Size’。
在多台负载平衡的服务器环境下,同一个文件会有不同的etag或者文件修改日期,浏览器每次都会重新下载。设置‘FileETag None’可以使响应头不再包含ETag字段。
三种典型web服务器Header设置内容过期方法
一、Internet 信息服务 (IIS)的内容过期设置
如果IIS 网站中有时间敏感信息,可以配置设置来保证过期信息不被代理服务器或 Web 浏览器缓存。可以配置网站内容,使之在任
何的时间自动过期。当启用内容过期时,Web 浏览器将比较当前日期和截止日期,以便决定是显示缓存页还是从服务器请求更新的页
。Microsoft ASP.NET 这样的服务器端技术可用于动态更改提供的内容。通常,时间敏感信息只限于单个文件、目录或网站;不过,您也可以为某台计算机上的所有网站设置内容过期。
必须是本地计算机上 Administrators 组的成员或者必须被委派了相应的权限,才能执行下列步骤。作为安全性的最佳操作,请使用不属于 Administrators 组的帐户登录计算机,然后使用运行方式命令以管理员身份运行 IIS管理器
在命令提示符下,键入 runas /user:administrative_accountname “mmc %systemroot%\system32\inetsrv\iis.msc”。
设置网站内容的过期时间
1. 在 IIS 管理器中,展开本地计算机;右键单击要设置内容过期的网站、虚拟目录或文件,然后单击“属性”。
2. 单击“HTTP 头”选项卡。
3. 选中“启用内容过期”复选框。
4. 单击“立即过期”、“此时间段后过期”或“过期时间”,然后在对应的框中输入所需的过期信息。
5. 单击“确定”。
二、APACHE服务的内容过期设置
Apache配置摘录及解释
i. 过期相关设置
LoadModule headers_module modules/mod_headers.so
#Load 修改header的模块。
LoadModule expires_module modules/mod_expires.so
#Load 设定过期header的模块。
Header append Via: CCN-BJ-4-502
#增加一个Via header,值配置成设备的hostname。
KeepAliveTimeout 60
#设置连接的保持时间为60秒。
ExpiresActive On
#启用过期header功能。
ExpiresDefault A604800
#缺省过期时间为“访问后的604800秒”
<Directory /data/download>
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
ExpiresByType text/html A300
#text/html类型文件的过期设置为“访问后的300秒”
ExpiresByType text/css A259200
#text/css类型文件的过期设置为“访问后的259200秒”
ExpiresByType application/x-javascript A300
# application/x-javascript类型文件的过期设置为“访问后的300秒”
ExpiresByType image/gif A2592000
#image/gif类型文件的过期设置为“访问后的2592000秒”
ExpiresByType application/x-shockwave-flash A2592000
# application/x-shockwave-flash类型文件的过期设置为“访问后的2592000秒”
</Directory>
上述配置文件中load的两个模块:mod_headers.so 和mod_expires.so 可以让Apache具有对header的一些定制功能。
ExpiresByType: 表示按照文件类型-MIME-TYPE设定过期策略;
A300: 表示在Access后300秒后过期;
ExpiresByType text/css A2592000: 表示Mime type是text/css的文件,在Access后2592000秒过期。
ExpiresDefault A604800: 表示除了单独制定的文件类型等过期策略外的其他内容,按照这个缺省的策略设定:访问后604800秒过期。
上面的方法可以实现根据web发布的不同文件类型,针对不同的发布目录进行过期策略设置。在按照如上方法设置后,Apache会自动的产生两个相关的http header,举例如下:
HTTP/1.1 200 OK
Date: Tue, 27 Mar 2007 17:44:21 GMT
Server: Apache/2.0.54 (Unix)
Last-Modified: Thu, 25 Jan 2007 07:45:45 GMT
ETag: “72df3a-93-99499c40”
Accept-Ranges: bytes
Content-Length: 147
Cache-Control: max-age=2592000
Expires: Thu, 26 Apr 2007 17:44:21 GMT
Via: CCN-BJ-4-575
Keep-Alive: timeout=60, max=100
Connection: Keep-Alive
Content-Type: image/gif
Length: 147 [image/gif]
其中:Date + Max-age = Expires.
Max-age是个时间长度,对应web
server上面设置的过期时间;Expires是根据max-age算出来的过期时间点,两者是一致的,不同cache在判断内容是否过期时会严格比较
系统时间和上述过期时间,或者比较age(在cache中存住的时间长度)和max-age的值。
三、lighttpd
lighttpd默认是没有开启expire模块的,需要我们在使用是手工开启这个模块支持。把mod_expire前面的“#”号去掉。
# vi /usr/local/lighttpd/etc/lighttpd.conf
“mod_expire”,
设定过期时间
* 设定指定url的过期时间:
expire.url = (
“/images/” => “access 3 hours”,
“/admin/” => “access 3 hours”,
“/area/” => “access 3 hours”,
“/calendar/” => “access 3 hours”,
“/common/” => “access 3 hours”,
“/front/” => “access 3 hours”,
“/inc/” => “access 3 hours”,
“/jeditor/” => “access 3 hours”,
“/js/” => “access 3 hours”,
“/script/” => “access 3 hours”,
“/theme/” => “access 3 hours”,
“/upload/” => “access 3 hours”,
“/view/” => “access 3 hours”,
“/help/” => “access 3 hours”,
“/htm/” => “access 5 minutes”
)
设置Etag
在配置文件中增加,etag.use-inode=”disable”(i节点不参与etag的运算),保证多台服务器生成的Etag值一致。

25
四 08

Nginx versus Apache


21
十二 07

mod_rewrite中的正则

文本
  .           任意一个单字符
  [chars]     字符类: "chars"中的任意一个字符
  [^chars]    字符类: 不在"chars"中的字符
  text1|text2 选择: text1 或 text2

量词
  ?           前面的字符出现 0 或 1 次
  *           前面的字符出现 0 或 N 次(N > 0)
  +           前面的字符出现 1 或 N 次(N > 1)

分组
  (text)      text 组
              (常用于设置一个选择的边界,或用于生成后引用:
               在RewriteRule中可以用 $N 引用第N个分组)


  ^           锚定到行首
  $           锚定到行尾

转义
  \c          对给定的字符c进行转义
              (比如对".[]()"进行转义,等等)

19
十二 07

Apache条件认证

参考:http://www.lslnet.com/linux/dosc1/50/linux-339195.htm

RewriteEngine on
RewriteCond %{Request_URI} .*/45-40/.*-------如果url包含45-40的目录
RewriteCond %{REMOTE_HOST} ^210.78.111.222$--而且是来自210.78.111.222
RewriteRule .* - [E=allow_in:yes,L]----------设置环境变量allow_in=yes,并且停止RewriteRule

RewriteCond %{HTTP_USER_AGENT} .*OpenWave.*–如果HTTP头里包含OpenWave
RewriteCond %{REMOTE_HOST} ^210.78.13.4$—–而且是来自210.78.13.4
RewriteRule .* – [E=allow_in:yes,L]———-设置环境变量allow_in=yes,并且停止RewriteRule

<Location />;

Order Allow,Deny—————————–访问控制,顺序是允许,拒绝,默认是拒绝
Allow from env=allow_in———————-如果设置了allow_in环境变量,则允许通过
Allow from 127.0.0.1 200.200.200.200/26——允许localhost和200.200.200.200/26网段的主机访问

AuthType Basic——————————-访问认证
AuthName “认证”
AuthUserFile /usr/local/apache/conf/passwd—密码文件位置
Require valid-user—————————要求合法的用户

Satisfy any———————————-只要符合上边的allow或Auth之一,就允许通过

</Location>;

访问过程:如果在allow列表里则允许通过,否则要求Auth,Auth没有通过则拒绝


14
十二 07

Apache的几个重要模块

mod_expires

说 明: 允许通过配置文件控制HTTP的”Expires”和”Cache-Control”头内容
状 态:扩展(E)
模块名:expires_module
源文件:mod_expires.c

概述

这个模块控制服务器应答时的Expires头内容和Cache-Control头的max-age指令。有效期(expiration date)可以设置为相对于源文件的最后修改时刻或者客户端的访问时刻。

这些HTTP头向客户端表明了文档的有效性和持久性。如果有缓存,文档就可以从缓存(除已经过期)而不是从服务器读取。接着,客户端考察缓存中的副本,看看是否过期或者失效,以决定是否必须从服务器获得更新。

要修改Cache-Control头中max-age(参见RFC 2616 section 14.9)项之外的内容,你还可以使用Header指令。


18
十一 07

Apache URL重定向指南

Apache URL重定向指南

mod_rewrite入门
Apache mod_rewrite模块是一个处理URL而又极为复杂的模块,使用mod_rewrite你可处理所有和URL有关的问题,你所付出的就是花时间去了解mod_rewrite的复杂架构,一般初学者都很难实时理解mod_rewrite的用法,有时Apache专家也要mod_rewrite来发展Apache的新功能。

换句话说,当你成功使用mod_rewrite做到你期望的东西,就不要试图再接触mod_rewrite了,因为mod_rewrite的功能实在过于强大。本章的例子会介绍几个成功的例子给你摸索,不像FAQ形式般把你的问题解答。

实用解决方法
这里还有很多未被发掘的解决方法,请大家耐心地学习如何使用mod_rewrite。

注意: 由于各人的服务器的配置都有所不同,你可能要更改设定来测试以下例子,例如使用mod_alias和mod_userdir时要加上[PT],或者使用.htaccess来重定向而非主设定文件等,请尽量理解各例子如何运作,不要生吞活剥地背诵。
 

URL规划
正规URL
描述:

在某些网页服务器中,一项资源可能会有数个URL,通常都会公布一正规URL(即真正发放的URL),其它URL都会被视为快捷方式或只供内部使用等,无论用户在使用快捷方式或正规URL,用户最后所重定向到的URL必需为正规。

方法:

我们可将所有非正规的URL重定向至正规的URL中,以下例子把非正规的「/~user」换成正规的「/u/user」,并且加上「/」号结尾。.

RewriteRule   ^/~([^/]+)/?(.*)    /u/$1/$2  [R]
RewriteRule   ^/([uge])/([^/]+)$  /$1/$2/   [R]
 

正规主机名称
描述:

(省略)

方法:

RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^/(.*)         http://fully.qualified.domain.name:%{SERVER_PORT}/$1 [L,R]
RewriteCond %{HTTP_HOST}   !^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://fully.qualified.domain.name/$1 [L,R]
 

DocumentRoot被移动
描述:

URL的「/」通常都会映像到DocumentRoot上,但DocumentRoot有时并非重始就限定在某个目录上,它可能只是一个或多个目录的对照而矣。例如我们的内联网址为/e/www/ (WWW的主目录)和/e/sww/ (内联网的主目录)等等,因为所有的网页资料都放在/e/www/目录内,我们要确定所有内嵌的图像都能正确显示。

方法:

我们只要把「/」重定向至「/e/www/」,用mod_rewrite来解决比用mod_alias来解决更为简洁,因为URL别名只会比较URL的前部分,但重定向因可能涉及另一台服务器而需要不同的前缀部分(前缀部分已受DocumentRoot限制),所以mod_rewrite是最好的解决方法::

RewriteEngine on
RewriteRule   ^/$  /e/www/  [R]
 

结尾斜线问题
描述:

每个网主都曾受到结尾斜线问题的折磨,若在URL中没有结尾斜线,服务器就会认为URL无效并返回错误,因为服务器会根据/~quux/foo去寻找foo这个档案,而非显示这个目录。其实很多时候,这问题应留待用户自己加「/」去解决,但有时你也可以完成步骤。例如你做了多次URL重定向,而目的地为一个CGI程序。

方法:

最直观的方法就是令Apache自动加上「/」,使用外部重定向令浏览器能正确找到档案,若我们只做内部重定向,就只能正确显示目录页,在这目录页的图像文件会因相对URL的问题而找不到。例如我们请求/~quux/foo/index.html的image.gif时,重定向后会变成/~quux/image.gif。

所以我们应使用以下方法:

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo$  foo/  [R]
 

这方法也适用于.htaccess文件在各目录内设定,但这设定会覆盖原先主配置文件。

RewriteEngine  on
RewriteBase    /~quux/
RewriteCond    %{REQUEST_FILENAME}  -d
RewriteRule    ^(.+[^/])$           $1/  [R]
 

利用均一的URL版面规划网络群组
描述:

所有的网页服务器都有相同的URL版面,即无论用户向哪个主机发出请求URL,用户都会接收到相同的网页,使URL独立于服务器本身。我们的目的在于如何在Apache服务器不能响应时,都能有一个常规(而又独立于服务器运作)的网页传送给用户,设立网络群组可将这网页送至远程。

方法:

首先,服务器需要一外部文件把网站的用户、用户组及其它资料存储,这文件的格式如下

user1  server_of_user1
user2  server_of_user2
:      :
把以上资料存入map.xxx-to-host。然后指示服务器把URL重定向,由

/u/user/anypath
/g/group/anypath
/e/entity/anypath

http://physical-host/u/user/anypath
http://physical-host/g/group/anypath
http://physical-host/e/entity/anypath
当服务器接收到不正确的URL时,服务器会跟随以下指示把URL映像到特定的档案(若URL并没有相对应的记录,就会重定向至 server0 上):

RewriteEngine on
 
RewriteMap      user-to-host   txt:/path/to/map.user-to-host
RewriteMap     group-to-host   txt:/path/to/map.group-to-host
RewriteMap    entity-to-host   txt:/path/to/map.entity-to-host
 
RewriteRule   ^/u/([^/]+)/?(.*)   http://${user-to-host:$1|server0}/u/$1/$2
RewriteRule   ^/g/([^/]+)/?(.*)  http://${group-to-host:$1|server0}/g/$1/$2
RewriteRule   ^/e/([^/]+)/?(.*) http://${entity-to-host:$1|server0}/e/$1/$2
 
RewriteRule   ^/([uge])/([^/]+)/?$          /$1/$2/.www/
RewriteRule   ^/([uge])/([^/]+)/([^.]+.+)   /$1/$2/.www/$3\
 

把主目录移到新的网页服务器
描述:

有很多网主都有以下问题:在升级时把所有用户主目录由旧的服务器移到新的服务器上。

方法:

使用mod_rewrite可以简单地解决这问题,把所有/~user/anypathURL重定向至http://newserver/~user/anypath

RewriteEngine on
RewriteRule   ^/~(.+)  http://newserver/~$1  [R,L]
 

结构化用户主目录
描述:

拥有大量用户的主机通常都会把用户目录规划好,将这些目录归入一个父目录中,然后再将用户的第一个字母作该用户的父目录,例如/~foo/anypath将会是/home/f/foo/.www/anypath,而/~bar/anypath就是/home/b/bar/.www/anypath。

方法:

按以下指令将URL直接对映到档案系统中。

RewriteEngine on
RewriteRule   ^/~(([a-z])[a-z0-9]+)(.*)  /home/$2/$1/.www$3
 

重新组织档案系统
描述:

这是一个麻烦的例子:在不用更动现有目录结构下,使用RewriteRules来显示整个目录结构。背景:net.sw是一个装满Unix免费软件的资料夹,并以下列结构存储:

drwxrwxr-x   2 netsw  users    512 Aug  3 18:39 Audio/
drwxrwxr-x   2 netsw  users    512 Jul  9 14:37 Benchmark/
drwxrwxr-x  12 netsw  users    512 Jul  9 00:34 Crypto/
drwxrwxr-x   5 netsw  users    512 Jul  9 00:41 Database/
drwxrwxr-x   4 netsw  users    512 Jul 30 19:25 Dicts/
drwxrwxr-x  10 netsw  users    512 Jul  9 01:54 Graphic/
drwxrwxr-x   5 netsw  users    512 Jul  9 01:58 Hackers/
drwxrwxr-x   8 netsw  users    512 Jul  9 03:19 InfoSys/
drwxrwxr-x   3 netsw  users    512 Jul  9 03:21 Math/
drwxrwxr-x   3 netsw  users    512 Jul  9 03:24 Misc/
drwxrwxr-x   9 netsw  users    512 Aug  1 16:33 Network/
drwxrwxr-x   2 netsw  users    512 Jul  9 05:53 Office/
drwxrwxr-x   7 netsw  users    512 Jul  9 09:24 SoftEng/
drwxrwxr-x   7 netsw  users    512 Jul  9 12:17 System/
drwxrwxr-x  12 netsw  users    512 Aug  3 20:15 Typesetting/
drwxrwxr-x  10 netsw  users    512 Jul  9 14:08 X11/
我们打算把这个资料夹公开,而且希望直接地显示这资料夹的目录结构,但是我们又不想更改现有目录架构来迁就,加上我们打算开放给FTP,所以不想加入任何网页或CGI程序到这个资料夹中。

方法:

本方法分为两部分:第一部份是编写一系列的CGI程序来显示目录结构,这例子会把CGI和刚才的资料夹放进/e/netsw/.www/:

-rw-r–r–   1 netsw  users    1318 Aug  1 18:10 .wwwacl
drwxr-xr-x  18 netsw  users     512 Aug  5 15:51 DATA/
-rw-rw-rw-   1 netsw  users  372982 Aug  5 16:35 LOGFILE
-rw-r–r–   1 netsw  users     659 Aug  4 09:27 TODO
-rw-r–r–   1 netsw  users    5697 Aug  1 18:01 netsw-about.html
-rwxr-xr-x   1 netsw  users     579 Aug  2 10:33 netsw-access.pl
-rwxr-xr-x   1 netsw  users    1532 Aug  1 17:35 netsw-changes.cgi
-rwxr-xr-x   1 netsw  users    2866 Aug  5 14:49 netsw-home.cgi
drwxr-xr-x   2 netsw  users     512 Jul  8 23:47 netsw-img/
-rwxr-xr-x   1 netsw  users   24050 Aug  5 15:49 netsw-lsdir.cgi
-rwxr-xr-x   1 netsw  users    1589 Aug  3 18:43 netsw-search.cgi
-rwxr-xr-x   1 netsw  users    1885 Aug  1 17:41 netsw-tree.cgi
-rw-r–r–   1 netsw  users     234 Jul 30 16:35 netsw-unlimit.lst
DATA/子目录就是刚才的资料夹,net.sw内的软件会经rdist程序来自动更新。第二部份将这资料夹和新建立的CGI、网页配合,我们想将DATA/稳藏起来,而在用户请求不同URL时执行正确的CGI程序来显示。先将/net.sw/这URL重定向至/e/netsw:

RewriteRule  ^net.sw$       net.sw/        [R]
RewriteRule  ^net.sw/(.*)$  e/netsw/$1
 

第一条规则纯粹补加URL结尾的「/」号,而第二条规则就是把URL重定向。之后将下列配置存入/e/netsw/.www/.wwwacl:

Options       ExecCGI FollowSymLinks Includes MultiViews
 
RewriteEngine on
 
#  we are reached via /net.sw/ prefix
RewriteBase   /net.sw/
 
#  first we rewrite the root dir to
#  the handling cgi script
RewriteRule   ^$                       netsw-home.cgi     [L]
RewriteRule   ^index\.html$            netsw-home.cgi     [L]
 
#  strip out the subdirs when
#  the browser requests us from perdir pages
RewriteRule   ^.+/(netsw-[^/]+/.+)$    $1                 [L]
 
#  and now break the rewriting for local files
RewriteRule   ^netsw-home\.cgi.*       -                  [L]
RewriteRule   ^netsw-changes\.cgi.*    -                  [L]
RewriteRule   ^netsw-search\.cgi.*     -                  [L]
RewriteRule   ^netsw-tree\.cgi$        -                  [L]
RewriteRule   ^netsw-about\.html$      -                  [L]
RewriteRule   ^netsw-img/.*$           -                  [L]
 
#  anything else is a subdir which gets handled
#  by another cgi script
RewriteRule   !^netsw-lsdir\.cgi.*     -                  [C]
RewriteRule   (.*)                     netsw-lsdir.cgi/$1
 

提示:

1.        留意第四部份的L(last)旗标及代表不用更改的(‘-’)符号

2.        留意最后部份第一条规则的 ! (not)字符,及 C (chain) 链接符

3.        留意最后一条规则代表全部更新的语法

以Apache的mod_imap取代NCSA的imagemap
描述:

很多人都想顺利地把旧的NCSA服务器迁至新的Apache服务器,所以我们都想将旧的NCSA imagemap顺利转换到Apache的mod_imap,问题是imagemap已被很多超级链接连系着,但旧的imagemap是存储在/cgi-bin/imagemap/path/to/page.map,而在Apache却是放在/path/to/page.map。

方法:

我们只要将「/cgi-bin/」移除便可:

RewriteEngine  on
RewriteRule    ^/cgi-bin/imagemap(.*)  $1  [PT]
 

在多个目录下搜寻网页
描述:

MultiViews亦不能指示Apache在多个目录里搜寻网页。

方法:

请参看以下指令。

RewriteEngine on
 
#   first try to find it in custom/…
#   …and if found stop and be happy:
RewriteCond         /your/docroot/dir1/%{REQUEST_FILENAME}  -f
RewriteRule  ^(.+)  /your/docroot/dir1/$1  [L]
 
#   second try to find it in pub/…
#   …and if found stop and be happy:
RewriteCond         /your/docroot/dir2/%{REQUEST_FILENAME}  -f
RewriteRule  ^(.+)  /your/docroot/dir2/$1  [L]
 
#   else go on for other Alias or ScriptAlias directives,
#   etc.
RewriteRule   ^(.+)  -  [PT]
 

跟据URL设定环境变量
描述:

在页面间传递讯息可以用CGI程序完成,但你却不想用CGI而用URL来传递。

方法:

以下指令将变量及其值抽出URL外,然后记入自设的环境变量中,该变量可由XSSI或CGI存取。例如把/foo/S=java/bar/转换为/foo/bar/,然后把「java」写入环境变量「STATUS」。

RewriteEngine on
RewriteRule   ^(.*)/S=([^/]+)/(.*)    $1/$3 [E=STATUS:$2]
 

虚拟用户主机
描述:

你只想根据DNS记录将www.username.host.domain.com的请求直接对映到档案系统,放弃使用Apache的虚拟主机功能。

方法:

只有HTTP/1.1请求才可用以下方法做到,我们可根据HTTP Header把http://www.username.host.com/anypath重定向到/home/username/anypath:

RewriteEngine on
RewriteCond   %{HTTP_HOST}                 ^www\.[^.]+\.host\.com$
RewriteRule   ^(.+)                        %{HTTP_HOST}$1          [C]
RewriteRule   ^www\.([^.]+)\.host\.com(.*) /home/$1$2
 

将远程请求重定向至另一个用户主目录
描述:

当用者的主机不属于自己的网域ourdomain.com时,就将请求重定向至www.somewhere.com</CODE。< dd>

方法:

请参看以下指令:

RewriteEngine on
RewriteCond   %{REMOTE_HOST}  !^.+\.ourdomain\.com$
RewriteRule   ^(/~.+)         http://www.somewhere.com/$1 [R,L]
 

将失败的网页请求重定向至另一部网页服务器
描述:

这是一般常见的疑问,最直观的方法就是用ErrorDocument加上CGI-scripts更改目标URL,但我们亦可使用mod_rewrite来实行(这方法的效率却比CGI程序更低)。

方法:

再一次留意CGI会是更有效率的解决方法,而mod_rewrite的好处在于更安全及易设置:

RewriteEngine on
RewriteCond   /your/docroot/%{REQUEST_FILENAME} !-f
RewriteRule   ^(.+)                             http://webserverB.dom/$1
 

以上例子会限制所有网页在DocumentRoot才能成功,我们可加多一点指令来改善:

RewriteEngine on
RewriteCond   %{REQUEST_URI} !-U
RewriteRule   ^(.+)          http://webserverB.dom/$1
 

这例子使用了mod_rewrite预计URL改动的功能,所有URL都可以安全地重定向至新的目录,但在速度慢的主机上不宜使用这方法,因为采用本例会拖慢服务器工作,当然你可以在高速CPU主机上使用。

更广泛的URL重定向
描述:

我们想重定向有控制字符的URL,例如”url#anchor”等,通常Apache会用uri_escape()函数来隔除这些控制字符,因此你不可以直接用mod_rewrite来重定向这类URL。

方法:

我们要使用一NPH-CGI(NPH = non-parseable headers)程序处理重定向工作,因为NPH-CGI不会隔除控制字符。首先,我们先利用xredirect:

RewriteRule ^xredirect:(.+) /path/to/nph-xredirect.cgi/$1 \
            [T=application/x-httpd-cgi,L]
 

强制性将所有URL加上xredirect,然后将URL导入nph-xredirect.cgi中,程序代码如下:

#!/path/to/perl
##
##  nph-xredirect.cgi — NPH/CGI script for extended redirects
##  Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved.
##
 
$| = 1;
$url = $ENV{‘PATH_INFO’};
 
print “HTTP/1.0 302 Moved Temporarily\n”;
print “Server: $ENV{‘SERVER_SOFTWARE’}\n”;
print “Location: $url\n”;
print “Content-type: text/html\n”;
print “\n”;
print “<html>\n”;
print “<head>\n”;
print “<title>302 Moved Temporarily (EXTENDED)</title>\n”;
print “</head>\n”;
print “<body>\n”;
print “<h1>Moved Temporarily (EXTENDED)</h1>\n”;
print “The document has moved <a HREF=\”$url\”>here</a>.<p>\n”;
print “</body>\n”;
print “</html>\n”;
 
##EOF##
 

这样可将所有能或不能直接用mod_rewrite来重定向的URL,经CGI来完成了。例如你可将某URL重定向至新闻服务器

RewriteRule ^anyurl  xredirect:news:newsgroup
 

注意:你不需在每条规则后加上[R]或[R,L]。

多样化资料夹存取
描述:

若你曾浏览http://www.perl.com/CPAN (CPAN = Comprehensive Perl Archive Network),它会把你重定向至其中一个最近你主机地区的FTP服务器,事实上这应该叫多样化FTP存取。CPAN用CGI来实行这服务,这次我们用mod_rewrite。

<STRONG方法:< strong>

由mod_rewrite 3.0.0开始可使用「ftp:」重定向至FTP服务器,用户主机的地区可依URL的顶层域名来决定,而顶层域名及FTP服务器位置的对照就存入某档案中。

RewriteEngine on
RewriteMap    multiplex                txt:/path/to/map.cxan
RewriteRule   ^/CxAN/(.*)              %{REMOTE_HOST}::$1                 [C]
RewriteRule   ^.+\.([a-zA-Z]+)::(.*)$  ${multiplex:$1|ftp.default.dom}$2  [R,L]
 

##
##  map.cxan — Multiplexing Map for CxAN
##
 
de        ftp://ftp.cxan.de/CxAN/
uk        ftp://ftp.cxan.uk/CxAN/
com       ftp://ftp.cxan.com/CxAN/
 :
##EOF##
 

在某段时间执行不同的重定向
描述n:

很多网主仍用CGI随着不同时间将URL重定向至不同的网页。

方法:

mod_rewrite设有很多以TIME_xxx开始的环境变量,将这些时间环境变量进行字符串比较可决定重定向至哪个网页:

RewriteEngine on
RewriteCond   %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond   %{TIME_HOUR}%{TIME_MIN} <1900
RewriteRule   ^foo\.html$             foo.day.html
RewriteRule   ^foo\.html$             foo.night.html
 

在07:00-19:00就显示foo.day.html,其余时间则显示foo.html

保留旧有文件的URL
描述:

更改文件的扩展名后,如何让旧的URL能对映到这新的文件。

方法:

把旧的URL用mod_rewrite重定向至新的文件,若有正确的新文件就对映到这文件,没有的话便对映到原有文件。

#   backward compatibility ruleset for
#   rewriting document.html to document.phtml
#   when and only when document.phtml exists
#   but no longer document.html
RewriteEngine on
RewriteBase   /~quux/
#   parse out basename, but remember the fact
RewriteRule   ^(.*)\.html$              $1      [C,E=WasHTML:yes]
#   rewrite to document.phtml if exists
RewriteCond   %{REQUEST_FILENAME}.phtml -f
RewriteRule   ^(.*)$ $1.phtml                   [S=1]
#   else reverse the previous basename cutout
RewriteCond   %{ENV:WasHTML}            ^yes$
RewriteRule   ^(.*)$ $1.html
 

内容控制
由旧的档名转到新的文件名 (档案系统)
描述:

假设我们将bar.html改名为foo.html,而我们又想保留旧有的URL,甚至不想给用户新的URL去连至这新档案。

方法:

将旧的档案对映到新的档案:

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo\.html$  bar.html
 

由旧的档名转到新的档名 (URL)
描述:

和刚才的例子一样,我们把bar.html改名为foo.html,但这次我们想直接将用户的网页重定向至新的文件,即浏览器的URL位置有所改变。

方法:

强制性将URL对映到新的URL:

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo\.html$  bar.html  [R]
 

由浏览器种类控制内容
描述:

一个出色的网页应能支持各种浏览器,例如我们要把完整版网页传送至Netscape,但就要传送文字版至Lynx。

方法:

由于浏览器没有提供Apache格式的浏览器种类资料,所以我们不可使用内文转换(mod_negotiation),我们必需用「User-Agent」决定浏览器种类。例如User-Agent为「Mozilla/3」就把「foo.html」重定向至「foo.NS.html」;若浏览器为「Lynx」或「Mozilla」就重定向至foo.20.html,其它种类的浏览器则导向至foo.32.html:

RewriteCond %{HTTP_USER_AGENT}  ^Mozilla/3.*
RewriteRule ^foo\.html$         foo.NS.html          [L]
 
RewriteCond %{HTTP_USER_AGENT}  ^Lynx/.*         [OR]
RewriteCond %{HTTP_USER_AGENT}  ^Mozilla/[12].*
RewriteRule ^foo\.html$         foo.20.html          [L]
 
RewriteRule ^foo\.html$         foo.32.html          [L]
 

动态本地档案更新(经镜像网站)
描述:

你想将某个主机的网页连结到你的网页目录,若被连结的是FTP服务器,你可用mirror程序将最新的档案移到自己的主机上,我们可用webcopy经网页服务器HTTP把档案下载,但这方法有一坏处:只有在执行webcopy时才能更新档案。更好的办法就是在发出请求时立刻找寻最新的档案来源,然后实时下载到自己主机中。

方法:

利用Proxy Throughput(flag [P])把远程网页甚至整个网站建立一直接对照。

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^hotsheet/(.*)$  http://www.tstimpreso.com/hotsheet/$1  [P]
 

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^usa-news\.html$   http://www.quux-corp.com/news/index.html  [P]
 

动态镜像档案更新(经本主机)
描述:

(省略)

方法:

RewriteEngine on
RewriteCond   /mirror/of/remotesite/$1           -U
RewriteRule   ^http://www\.remotesite\.com/(.*)$ /mirror/of/remotesite/$1
 

由内部网络更新档案
描述:

为了安全起见,我们建立了两个网页服务器,第一个是公开的(www.quux-corp.dom),第二个则是内部使用,受防火墙所保护,一切资料及网站维护都经这个服务器进行,现在我们想令外部服务器能存取穿过防火墙,获取内部服务器已最新的档案。

方法:

我们只容许外部服务器从内部获取资料,一切直接获取的请求都受防火墙拒绝,先在防火墙设定:

ALLOW Host www.quux-corp.dom Port >1024 –> Host www2.quux-corp.dom Port 80 
DENY  Host *                 Port *     –> Host www2.quux-corp.dom Port 80
 

把以上的字句译成设置防火墙的语法,然后在mod_rewrite透过proxy throughput获取最新资料:

RewriteRule ^/~([^/]+)/?(.*)          /home/$1/.www/$2
RewriteCond %{REQUEST_FILENAME}       !-f
RewriteCond %{REQUEST_FILENAME}       !-d
RewriteRule ^/home/([^/]+)/.www/?(.*) http://www2.quux-corp.dom/~$1/pub/$2 [P]
 

平衡服务器负荷
描述:

我们想将www[0-5].foo.com这六部服务器的工作量平均分配。

方法:

当然你会有很多方法达成,一般都会使用DNS,介绍完DNS后再会讨论mod_rewrite如何实行。

1.     DNS循环机制

最简单的方法就是使用BIND的循环机制,e.g.

www0   IN  A       1.2.3.1
www1   IN  A       1.2.3.2
www2   IN  A       1.2.3.3
www3   IN  A       1.2.3.4
www4   IN  A       1.2.3.5
www5   IN  A       1.2.3.6
 

然后加上以下记录:

www    IN  CNAME   www0.foo.com.
       IN  CNAME   www1.foo.com.
       IN  CNAME   www2.foo.com.
       IN  CNAME   www3.foo.com.
       IN  CNAME   www4.foo.com.
       IN  CNAME   www5.foo.com.
       IN  CNAME   www6.foo.com.
 

在DNS层面上这种设定当然是错的,但我们正好使用了BIND的循环机制,BIND接收到www.foo.com的解析请求,然后BIND就会循环地解析作www0-www6,这样就能将用户分配到不同的服务器上,但请记得这不是一个完美的方案,因为其它的域名服务器会快取你服务器的域名解析结果,所以每一次解析到wwwX.foo.com时,都会有很多用户同时被派往同一部服务器,但整体来说已能平衡各服务器的负荷。

2.     DNS平衡负荷

http://www.stanford.edu/~schemers/docs/lbnamed/lbnamed.html有一个lbnamed程序专责利用域名服务器把用户请求分发到不同的服务器上,这是一个用Perl 5及其它附助工具写的复杂DNS工作量分配程序。

3.     代理服务器循环建立机制

我们使用mod_rewrite及其代理服务器网页记录(proxy throughput)功能,先在DNS加入www0.foo.com即是www.foo.com的记录。

www    IN  CNAME   www0.foo.com.
 

然后将www0.foo.com变为一独立代理服务器,即是建立一专责代理服务器,然后把请求分流至五部不同的服务器(www1-www5),我们用lb.pl及以下mod_rewrite规则:

RewriteEngine on
RewriteMap    lb      prg:/path/to/lb.pl
RewriteRule   ^/(.+)$ ${lb:$1}           [P,L]
 

lb.pl的程序代码:

#!/path/to/perl
##
##  lb.pl — load balancing script
##
 
$| = 1;
 
$name   = “www”;     # the hostname base
$first  = 1;         # the first server (not 0 here, because 0 is myself)
$last   = 5;         # the last server in the round-robin
$domain = “foo.dom”; # the domainname
 
$cnt = 0;
while (<STDIN>) {
    $cnt = (($cnt+1) % ($last+1-$first));
    $server = sprintf(“%s%d.%s”, $name, $cnt+$first, $domain);
    print “http://$server/$_”;
}
 
##EOF##
 

注意,www0.foo.com这服务器的工作量仍然和以前一样高,但这服务器的工作就只是负责分流,所有SSI、CGI、ePerl请求都由其它服务器执行,所以整体的工作量已经减少了许多。

4.     硬件/TCP循环机制

可Cisco的LocalDirector在TCP/IP网络层上把用户请求分流,事实上这种分流程序已刻烙在是电路板上。与硬件有关的解决方法通常都需要大量的金钱,但执行效率就会是最高。

将请求重定向至代理服务器
描述:

(省略)

方法:

##
##  apache-rproxy.conf — Apache configuration for Reverse Proxy Usage
##
 
#   server type
ServerType           standalone
Port                 8000
MinSpareServers      16
StartServers         16
MaxSpareServers      16
MaxClients           16
MaxRequestsPerChild  100
 
#   server operation parameters
KeepAlive            on
MaxKeepAliveRequests 100
KeepAliveTimeout     15
Timeout              400
IdentityCheck        off
HostnameLookups      off
 
#   paths to runtime files
PidFile              /path/to/apache-rproxy.pid
LockFile             /path/to/apache-rproxy.lock
ErrorLog             /path/to/apache-rproxy.elog
CustomLog            /path/to/apache-rproxy.dlog “%{%v/%T}t %h -> %{SERVER}e URL: %U”
 
#   unused paths
ServerRoot           /tmp
DocumentRoot         /tmp
CacheRoot            /tmp
RewriteLog           /dev/null
TransferLog          /dev/null
TypesConfig          /dev/null
AccessConfig         /dev/null
ResourceConfig       /dev/null
 
#   speed up and secure processing
<Directory />
Options -FollowSymLinks -SymLinksIfOwnerMatch
AllowOverride None
</Directory>
 
#   the status page for monitoring the reverse proxy
<Location /apache-rproxy-status>
SetHandler server-status
</Location>
 
#   enable the URL rewriting engine
RewriteEngine        on
RewriteLogLevel      0
 
#   define a rewriting map with value-lists where
#   mod_rewrite randomly chooses a particular value
RewriteMap     server  rnd:/path/to/apache-rproxy.conf-servers
 
#   make sure the status page is handled locally
#   and make sure no one uses our proxy except ourself
RewriteRule    ^/apache-rproxy-status.*  -  [L]
RewriteRule    ^(http|ftp)://.*          -  [F]
 
#   now choose the possible servers for particular URL types
RewriteRule    ^/(.*\.(cgi|shtml))$  to://${server:dynamic}/$1  [S=1]
RewriteRule    ^/(.*)$               to://${server:static}/$1 
 
#   and delegate the generated URL by passing it
#   through the proxy module
RewriteRule    ^to://([^/]+)/(.*)    http://$1/$2   [E=SERVER:$1,P,L]
 
#   and make really sure all other stuff is forbidden
#   when it should survive the above rules…
RewriteRule    .*                    -              [F]
 
#   enable the Proxy module without caching
ProxyRequests        on
NoCache              *
 
#   setup URL reverse mapping for redirect reponses
ProxyPassReverse  /  http://www1.foo.dom/
ProxyPassReverse  /  http://www2.foo.dom/
ProxyPassReverse  /  http://www3.foo.dom/
ProxyPassReverse  /  http://www4.foo.dom/
ProxyPassReverse  /  http://www5.foo.dom/
ProxyPassReverse  /  http://www6.foo.dom/
 

##
##  apache-rproxy.conf-servers — Apache/mod_rewrite selection table
##
 
#   list of backend servers which serve static
#   pages (HTML files and Images, etc.)
static    www1.foo.dom|www2.foo.dom|www3.foo.dom|www4.foo.dom
 
#   list of backend servers which serve dynamically
#   generated page (CGI programs or mod_perl scripts)
dynamic   www5.foo.dom|www6.foo.dom
 

建立新的档案型态及服务
描述:

你可在网上找到大量华丽的CGI程序,但又因这些CGI的艰难用法,很多人都不愿意使用,甚至Apache Action Handler的MIME类型亦On the net there are a lot of nifty CGI programs. But their usage is usually boring, so a lot of webmaster don’t use them. Even Apache’s Action handler feature for MIME-types is only appropriate when the CGI programs don’t need special URLs (actually PATH_INFO and QUERY_STRINGS) as their input. First, let us configure a new file type with extension .scgi (for secure CGI) which will be processed by the popular cgiwrap program. The problem here is that for instance we use a Homogeneous URL Layout (see above) a file inside the user homedirs has the URL /u/user/foo/bar.scgi. But cgiwrap needs the URL in the form /~user/foo/bar.scgi/. The following rule solves the problem:

RewriteRule ^/[uge]/([^/]+)/\.www/(.+)\.scgi(.*) …
… /internal/cgi/user/cgiwrap/~$1/$2.scgi$3  [NS,T=application/x-http-cgi]
 

Or assume we have some more nifty programs: wwwlog (which displays the access.log for a URL subtree and wwwidx (which runs Glimpse on a URL subtree). We have to provide the URL area to these programs so they know on which area they have to act on. But usually this ugly, because they are all the times still requested from that areas, i.e. typically we would run the swwidx program from within /u/user/foo/ via hyperlink to

/internal/cgi/user/swwidx?i=/u/user/foo/
which is ugly. Because we have to hard-code both the location of the area and the location of the CGI inside the hyperlink. When we have to reorganise or area, we spend a lot of time changing the various hyperlinks.

Solution:

The solution here is to provide a special new URL format which automatically leads to the proper CGI invocation. We configure the following:

RewriteRule   ^/([uge])/([^/]+)(/?.*)/\*  /internal/cgi/user/wwwidx?i=/$1/$2$3/
RewriteRule   ^/([uge])/([^/]+)(/?.*):log /internal/cgi/user/wwwlog?f=/$1/$2$3
 

Now the hyperlink to search at /u/user/foo/ reads only

HREF=”*”
which internally gets automatically transformed to

/internal/cgi/user/wwwidx?i=/u/user/foo/
The same approach leads to an invocation for the access log CGI program when the hyperlink :log gets used.

From Static to Dynamic
Description:

How can we transform a static page foo.html into a dynamic variant foo.cgi in a seemless way, i.e. without notice by the browser/user.

Solution:

We just rewrite the URL to the CGI-script and force the correct MIME-type so it gets really run as a CGI-script. This way a request to /~quux/foo.html internally leads to the invokation of /~quux/foo.cgi.

RewriteEngine  on
RewriteBase    /~quux/
RewriteRule    ^foo\.html$  foo.cgi  [T=application/x-httpd-cgi]
 

On-the-fly Content-Regeneration
Description:

Here comes a really esoteric feature: Dynamically generated but statically served pages, i.e. pages should be delivered as pure static pages (read from the filesystem and just passed through), but they have to be generated dynamically by the webserver if missing. This way you can have CGI-generated pages which are statically served unless one (or a cronjob) removes the static contents. Then the contents gets refreshed.

Solution:

This is done via the following ruleset:

RewriteCond %{REQUEST_FILENAME}   !-s
RewriteRule ^page\.html$          page.cgi   [T=application/x-httpd-cgi,L]
 

Here a request to page.html leads to a internal run of a corresponding page.cgi if page.html is still missing or has filesize null. The trick here is that page.cgi is a usual CGI script which (additionally to its STDOUT) writes its output to the file page.html. Once it was run, the server sends out the data of page.html. When the webmaster wants to force a refresh the contents, he just removes page.html (usually done by a cronjob).

Document With Autorefresh
Description:

Wouldn’t it be nice while creating a complex webpage if the webbrowser would automatically refresh the page every time we write a new version from within our editor? Impossible?

Solution:

No! We just combine the MIME multipart feature, the webserver NPH feature and the URL manipulation power of mod_rewrite. First, we establish a new URL feature: Adding just :refresh to any URL causes this to be refreshed every time it gets updated on the filesystem.

RewriteRule   ^(/[uge]/[^/]+/?.*):refresh  /internal/cgi/apache/nph-refresh?f=$1
 

Now when we reference the URL

/u/foo/bar/page.html:refresh
this leads to the internal invocation of the URL

/internal/cgi/apache/nph-refresh?f=/u/foo/bar/page.html
The only missing part is the NPH-CGI script. Although one would usually say “left as an exercise to the reader” ;-) I will provide this, too.

#!/sw/bin/perl
##
##  nph-refresh — NPH/CGI script for auto refreshing pages
##  Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved.
##
$| = 1;
 
#   split the QUERY_STRING variable
@pairs = split(/&/, $ENV{‘QUERY_STRING’});
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $name =~ tr/A-Z/a-z/;
    $name = ‘QS_’. $name;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(“C”, hex($1))/eg;
    eval “\$$name = \”$value\”";
}
$QS_s = 1 if ($QS_s eq ”);
$QS_n = 3600 if ($QS_n eq ”);
if ($QS_f eq ”) {
    print “HTTP/1.0 200 OK\n”;
    print “Content-type: text/html\n\n”;
    print “&lt;b&gt;ERROR&lt;/b&gt;: No file given\n”;
    exit(0);
}
if (! -f $QS_f) {
    print “HTTP/1.0 200 OK\n”;
    print “Content-type: text/html\n\n”;
    print “&lt;b&gt;ERROR&lt;/b&gt;: File $QS_f not found\n”;
    exit(0);
}
 
sub print_http_headers_multipart_begin {
    print “HTTP/1.0 200 OK\n”;
    $bound = “ThisRandomString12345″;
    print “Content-type: multipart/x-mixed-replace;boundary=$bound\n”;
    &print_http_headers_multipart_next;
}
 
sub print_http_headers_multipart_next {
    print “\n–$bound\n”;
}
 
sub print_http_headers_multipart_end {
    print “\n–$bound–\n”;
}
 
sub displayhtml {
    local($buffer) = @_;
    $len = length($buffer);
    print “Content-type: text/html\n”;
    print “Content-length: $len\n\n”;
    print $buffer;
}
 
sub readfile {
    local($file) = @_;
    local(*FP, $size, $buffer, $bytes);
    ($x, $x, $x, $x, $x, $x, $x, $size) = stat($file);
    $size = sprintf(“%d”, $size);
    open(FP, “&lt;$file”);
    $bytes = sysread(FP, $buffer, $size);
    close(FP);
    return $buffer;
}
 
$buffer = &readfile($QS_f);
&print_http_headers_multipart_begin;
&displayhtml($buffer);
 
sub mystat {
    local($file) = $_[0];
    local($time);
 
    ($x, $x, $x, $x, $x, $x, $x, $x, $x, $mtime) = stat($file);
    return $mtime;
}
 
$mtimeL = &mystat($QS_f);
$mtime = $mtime;
for ($n = 0; $n &lt; $QS_n; $n++) {
    while (1) {
        $mtime = &mystat($QS_f);
        if ($mtime ne $mtimeL) {
            $mtimeL = $mtime;
            sleep(2);
            $buffer = &readfile($QS_f);
            &print_http_headers_multipart_next;
            &displayhtml($buffer);
            sleep(5);
            $mtimeL = &mystat($QS_f);
            last;
        }
        sleep($QS_s);
    }
}
 
&print_http_headers_multipart_end;
 
exit(0);
 
##EOF##
Mass Virtual Hosting
Description:

The <VirtualHost> feature of Apache is nice and works great when you just have a few dozens virtual hosts. But when you are an ISP and have hundreds of virtual hosts to provide this feature is not the best choice.

Solution:

To provide this feature we map the remote webpage or even the complete remote webarea to our namespace by the use of the Proxy Throughput feature (flag [P]):

##
##  vhost.map
##
www.vhost1.dom:80  /path/to/docroot/vhost1
www.vhost2.dom:80  /path/to/docroot/vhost2
     :
www.vhostN.dom:80  /path/to/docroot/vhostN
 

##
##  httpd.conf
##
    :
#   use the canonical hostname on redirects, etc.
UseCanonicalName on
 
    :
#   add the virtual host in front of the CLF-format
CustomLog  /path/to/access_log  “%{VHOST}e %h %l %u %t \”%r\” %>s %b”
    :
 
#   enable the rewriting engine in the main server
RewriteEngine on
 
#   define two maps: one for fixing the URL and one which defines
#   the available virtual hosts with their corresponding
#   DocumentRoot.
RewriteMap    lowercase    int:tolower
RewriteMap    vhost        txt:/path/to/vhost.map
 
#   Now do the actual virtual host mapping
#   via a huge and complicated single rule:
#
#   1. make sure we don’t map for common locations
RewriteCond   %{REQUEST_URI}  !^/commonurl1/.*
RewriteCond   %{REQUEST_URI}  !^/commonurl2/.*
    :
RewriteCond   %{REQUEST_URI}  !^/commonurlN/.*
#
#   2. make sure we have a Host header, because
#      currently our approach only supports
#      virtual hosting through this header
RewriteCond   %{HTTP_HOST}  !^$
#
#   3. lowercase the hostname
RewriteCond   ${lowercase:%{HTTP_HOST}|NONE}  ^(.+)$
#
#   4. lookup this hostname in vhost.map and
#      remember it only when it is a path
#      (and not “NONE” from above)
RewriteCond   ${vhost:%1}  ^(/.*)$
#
#   5. finally we can map the URL to its docroot location
#      and remember the virtual host for logging puposes
RewriteRule   ^/(.*)$   %1/$1  [E=VHOST:${lowercase:%{HTTP_HOST}}]
    :
 

Access Restriction
Blocking of Robots
Description:

How can we block a really annoying robot from retrieving pages of a specific webarea? A /robots.txt file containing entries of the “Robot Exclusion Protocol” is typically not enough to get rid of such a robot.

Solution:

We use a ruleset which forbids the URLs of the webarea /~quux/foo/arc/ (perhaps a very deep directory indexed area where the robot traversal would create big server load). We have to make sure that we forbid access only to the particular robot, i.e. just forbidding the host where the robot runs is not enough. This would block users from this host, too. We accomplish this by also matching the User-Agent HTTP header information.

RewriteCond %{HTTP_USER_AGENT}   ^NameOfBadRobot.*     
RewriteCond %{REMOTE_ADDR}       ^123\.45\.67\.[8-9]$
RewriteRule ^/~quux/foo/arc/.+   -   [F]
 

Blocked Inline-Images
Description:

Assume we have under http://www.quux-corp.de/~quux/ some pages with inlined GIF graphics. These graphics are nice, so others directly incorporate them via hyperlinks to their pages. We don’t like this practice because it adds useless traffic to our server.

Solution:

While we cannot 100% protect the images from inclusion, we can at least restrict the cases where the browser sends a HTTP Referer header.

RewriteCond %{HTTP_REFERER} !^$                                 
RewriteCond %{HTTP_REFERER} !^http://www.quux-corp.de/~quux/.*$ [NC]
RewriteRule .*\.gif$        -                                    [F]
 

RewriteCond %{HTTP_REFERER}         !^$                                 
RewriteCond %{HTTP_REFERER}         !.*/foo-with-gif\.html$
RewriteRule ^inlined-in-foo\.gif$   -                        [F]
 

Host Deny
Description:

How can we forbid a list of externally configured hosts from using our server?

Solution:

For Apache >= 1.3b6:

RewriteEngine on
RewriteMap    hosts-deny  txt:/path/to/hosts.deny
RewriteCond   ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND} !=NOT-FOUND [OR]
RewriteCond   ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND} !=NOT-FOUND
RewriteRule   ^/.*  -  [F]
 

For Apache <= 1.3b6:

RewriteEngine on
RewriteMap    hosts-deny  txt:/path/to/hosts.deny
RewriteRule   ^/(.*)$ ${hosts-deny:%{REMOTE_HOST}|NOT-FOUND}/$1
RewriteRule   !^NOT-FOUND/.* – [F]
RewriteRule   ^NOT-FOUND/(.*)$ ${hosts-deny:%{REMOTE_ADDR}|NOT-FOUND}/$1
RewriteRule   !^NOT-FOUND/.* – [F]
RewriteRule   ^NOT-FOUND/(.*)$ /$1
 

##
##  hosts.deny
##
##  ATTENTION! This is a map, not a list, even when we treat it as such.
##             mod_rewrite parses it for key/value pairs, so at least a
##             dummy value “-” must be present for each entry.
##
 
193.102.180.41 -
bsdti1.sdm.de  -
192.76.162.40  -
 

URL-Restricted Proxy
Description:

How can we restrict the proxy to allow access to a configurable set of internet sites only? The site list is extracted from a prepared bookmarks file.

Solution:

We first have to make sure mod_rewrite is below(!) mod_proxy in the Configuration file when compiling the Apache webserver (or in the AddModule list of httpd.conf in the case of dynamically loaded modules), as it must get called _before_ mod_proxy.

For simplicity, we generate the site list as a textfile map (but see the mod_rewrite documentation for a conversion script to DBM format). A typical Netscape bookmarks file can be converted to a list of sites with a shell script like this:

#!/bin/sh
cat ${1:-~/.netscape/bookmarks.html} |
tr -d ‘\015′ | tr ‘[A-Z]‘ ‘[a-z]‘ | grep href=\” |
sed -e ‘/href=”file:/d;’-e ‘/href=”news:/d;’ \
    -e ‘s|^.*href=”[^:]*://\([^:/"]*\).*$|\1 OK|;’\
    -e ‘/href=”/s|^.*href=”\([^:/"]*\).*$|\1 OK|;’|
sort -u
 

We redirect the resulting output into a text file called goodsites.txt. It now looks similar to this:

www.apache.org OK
xml.apache.org OK
jakarta.apache.org OK
perl.apache.org OK

 

We reference this site file within the configuration for the VirtualHost which is responsible for serving as a proxy (often not port 80, but 81, 8080 or 8008).

<VirtualHost *:8008>
  …
  RewriteEngine   On
  # Either use the (plaintext) allow list from goodsites.txt
  RewriteMap      ProxyAllow   txt:/usr/local/apache/conf/goodsites.txt
  # Or, for faster access, convert it to a DBM database:
  #RewriteMap     ProxyAllow   dbm:/usr/local/apache/conf/goodsites
  # Match lowercased hostnames
  RewriteMap      lowercase    int:tolower
  # Here we go:
  # 1) first lowercase the site name and strip off a :port suffix
  RewriteCond  ${lowercase:%{HTTP_HOST}}    ^([^:]*).*$
  # 2) next look it up in the map file.
  #    “%1″ refers to the previous regex.
  #    If the result is “OK”, proxy access is granted.
  RewriteCond  ${ProxyAllow:%1|DENY}        !^OK$          [NC]
  # 3) Disallow proxy requests if the site was _not_ tagged “OK”:
  RewriteRule  ^proxy:                      -              [F]
  …
</VirtualHost>
 

Proxy Deny
Description:

How can we forbid a certain host or even a user of a special host from using the Apache proxy?

Solution:

We first have to make sure mod_rewrite is below(!) mod_proxy in the Configuration file when compiling the Apache webserver. This way it gets called _before_ mod_proxy. Then we configure the following for a host-dependend deny…

RewriteCond %{REMOTE_HOST} ^badhost\.mydomain\.com$
RewriteRule !^http://[^/.]\.mydomain.com.*  – [F]
 

…and this one for a user@host-dependend deny:

RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST^badguy@badhost\.mydomain\.com$
RewriteRule !^http://[^/.]\.mydomain.com.*  – [F]
 

Special Authentication Variant
Description:

Sometimes a very special authentication is needed, for instance a authentication which checks for a set of explicitly configured users. Only these should receive access and without explicit prompting (which would occur when using the Basic Auth via mod_access).

Solution:

We use a list of rewrite conditions to exclude all except our friends:

RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend1@client1.quux-corp\.com$
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend2@client2.quux-corp\.com$
RewriteCond %{REMOTE_IDENT}@%{REMOTE_HOST} !^friend3@client3.quux-corp\.com$
RewriteRule ^/~quux/only-for-friends/      -                                 [F]
 

Referer-based Deflector
Description:

How can we program a flexible URL Deflector which acts on the “Referer” HTTP header and can be configured with as many referring pages as we like?

Solution:

Use the following really tricky ruleset…

RewriteMap  deflector txt:/path/to/deflector.map
 
RewriteCond %{HTTP_REFERER} !=”"
RewriteCond ${deflector:%{HTTP_REFERER}} ^-$
RewriteRule ^.* %{HTTP_REFERER} [R,L]
 
RewriteCond %{HTTP_REFERER} !=”"
RewriteCond ${deflector:%{HTTP_REFERER}|NOT-FOUND} !=NOT-FOUND
RewriteRule ^.* ${deflector:%{HTTP_REFERER}} [R,L]
 

… in conjunction with a corresponding rewrite map:

##
##  deflector.map
##
 
http://www.badguys.com/bad/index.html    -
http://www.badguys.com/bad/index2.html   -
http://www.badguys.com/bad/index3.html   http://somewhere.com/
 

This automatically redirects the request back to the referring page (when “-” is used as the value in the map) or to a specific URL (when an URL is specified in the map as the second argument).

Other
External Rewriting Engine
Description:

A FAQ: How can we solve the FOO/BAR/QUUX/etc. problem? There seems no solution by the use of mod_rewrite…

Solution:

Use an external rewrite map, i.e. a program which acts like a rewrite map. It is run once on startup of Apache receives the requested URLs on STDIN and has to put the resulting (usually rewritten) URL on STDOUT (same order!).

RewriteEngine on
RewriteMap    quux-map       prg:/path/to/map.quux.pl
RewriteRule   ^/~quux/(.*)$  /~quux/${quux-map:$1}
 

#!/path/to/perl
 
#   disable buffered I/O which would lead
#   to deadloops for the Apache server
$| = 1;
 
#   read URLs one per line from stdin and
#   generate substitution URL on stdout
while (<>) {
    s|^foo/|bar/|;
    print $_;
}
 

This is a demonstration-only example and just rewrites all URLs /~quux/foo/… to /~quux/bar/…. Actually you can program whatever you like. But notice that while such maps can be used also by an average user, only the system administrator can define it.