日期:2014-05-17  浏览次数:20751 次

Apache经典实例

优雅的重启Apache:

/path/to/apache/to/apachectl graceful


安装简单的Apache模块:


/path/to/apache/bin/apxs -cia module.c


cia三个字母分别表示编译compile,安装install,启用activate。

安装把.so文件放入apache可以找到的位置,启用表示将模块加入到httpd.conf文件中。

不记录来自本机的图片请求:

<FilesMatch \.(gif|jpg|png)>
SetEnvIfNoCase Referer "^http://www.foobar.com/" local_referrer=1
</FilesMatch>

CustomLog logs/access_log combined env=!local_referrer


或者

<directory ...>
RewriteEngine On
RewriteRule "\.(gif|jpg|png)$" "-" [ENV=local_referrer:1]
</directory>

CustomLog logs/access_log combined env=!local_referrer


记录任意环境变量,其实就是%{...}e语法:

CustomLog logs/accept_log "%{UNIQUE_ID}e"

记录任意请求头,其实就是%{...}i语法:

CustomLog logs/accept_log "%{Accept}i"

记录任意响应头,其实就是%{...}o语法:

CustomLog logs/accept_log "%{Last-Modified}o"

拒绝未被引用的请求:

RewriteEngine On
RewriteCond %{HTTP_REFERER} !=""
RewriteCond %{HTTP_REFERER} "!^http://www.foobar.com/.*$" [NC]
RewriteCond %{REQUEST_URI} "\.(gif|jpg|png)$"
RewriteRule ".*" "-" [F]


依据查询字符串来重写:

RewriteEngine on
RewriteCond "%{QUERY_STRING}" "^user=([^=]*)"
RewriteRule "/userdirs" "http://%1.foobar.com/" [R]


以Rewirte规则建立大量的虚拟主机:

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