配置文件

  1. 配置文件是由一堆指令组成的,任何指令都可以放在配置文件的任何位置。
  2. 一行只能有一条指令。行末尾的反斜杠”"标志当前行的指令会跨行设置,即多行文本组成一条指令。
  3. 指令不区分大小写。但是指令的参数通常是区分大小写的。
  4. #号开后的行是备注。备注不能和指令在一行。
  5. 每一行的前置空格会被忽略。可以使用空白字符缩进。
  6. 可以使用${ENVVAR}引用环境变量。
  7. 每一行最多8192个字符,如果有环境变量,8192包含环境变量的值的长度。
  8. 通过modules扩展功能,如果是动态链接库模块,需要使用LoadModule指令引用dll。加载模块时可以使用IfModule指令设置条件加载功能。
  9. 指令默认全局有效,但是可以通过 , , , , , and 设置作用域范围。

Listen指令

Listen 80
Listen 8000

Listen 192.0.2.1:80
Listen 192.0.2.5:8000

Listen [2001:db8::a00:20ff:fea7:ccea]:80

容器指令

IfDefine: httpd -D ClosedForNow

IfDefine类似于宏,当命令行参数中定义了该宏,就会运行内部指令,该指令只会在系统启动时运行。

<IfDefine ClosedForNow>
    Redirect / http://otherserver.example.com/
</IfDefine>

IfModule

只有模块被静态编译或通过LoadModule指令引用对应动态链接库的模块后,才会执行。如果是动态引用,必须先Load Module,才能判断模块是否存在。

<IfModule mod_mime_magic.c>
    MimeMagicFile conf/magic
</IfModule>

IfVersion

<IfVersion >= 2.1>
    # this happens only in versions greater or
    # equal 2.1.0.
</IfVersion>

Filesystem & Webspace

文件目录和网址支持正则表达式。当使用正则表达式时,使用, , and 。当不使用正则表达式时,可以使用通配符。The character "*" matches any sequence of characters, "?" matches any single character, and "[seq]" matches any character in seq. The "/" character will not be matched by any wildcard; it must be specified explicitly.

Directory & File

目录和文件可以组合使用,目录分段符只能是/,即使是Windows,也要用/。

<Directory /var/web/dir1>
    Options +Indexes
</Directory>

<Files private.html>
    Order allow,deny
    Deny from all
</Files>

<Directory /var/web/dir1>
    <Files private.html>
        Order allow,deny
        Deny from all
    </Files>
</Directory>

通配符
<Directory /home/*/public_html>
    Options Indexes
</Directory>

正则表达式
<FilesMatch \.(?i:gif|jpe?g|png)$>
    Order allow,deny
    Deny from all
</FilesMatch>

WebSpace

禁止类似下面三个网址访问。
http://yoursite.example.com/private
http://yoursite.example.com/private123
http://yoursite.example.com/private/dir/file.html
<LocationMatch ^/private>
    Order Allow,Deny
    Deny from all
</LocationMatch>

<Location /server-status>
    SetHandler server-status
</Location>