apahce 配置指令可以分为两大块,核心指令和第三方提供的指令。在apache中,每一个指令都对应着一个模块,而在所有模块中,最重要的就是core_module,so_module,http_module,以及mpm模块,他们除了so_moudle以外的其他模块都不可以关掉或者禁止.
一:httpd.conf
(1) 主服务器部分
1 、servername: 定义apache默认主机名,可以是域名或者ip地址
2 、serverroot: 用于定义服务器所在的目录,这个路径通常是在配置时候由--prefix来指定的
3 、documentroot: 用于指定apache提供页面服务的根目录,这个路径必须是绝对路径而不是相对路径,如果有空格还需要用引号括起来
4 、serveradmin: 服务器出错后给管理员发邮件的地址
5 、serveralias 和 alias: 都用于映射目录,只是serveralias将映射的目录识别为cgi脚本目录,并将此目录所有文件都作为cgi脚本对待。但是alias只是映射为一个普通的目录
6 、user 和 group: 用于定义用于运行apache 子进程的用户和用户组
7 、listen: 用来定义监听apache的端口号
8 、loadmodule 指令:用于加载模块或者目标文件 loadmodule cgi_module modules/mod_cgi.so_module
9、 errordocument: 自义错误页面信息 errordocument 500 unknown error errordocument 404 /var/server/www/cgi-bin/missing_404.pl errordocument 402 http://www.nicky.com/error_402.html
10、 options : 决定在哪些目录中使用那些特性,这些特性如下:
none: option 指令将不会起作用
execcgi: 允许当前目录下执行cgi脚本
includes: 允许使用ssi功能
includesnoexec: 允许使用ssi功能,但是exec cgi and exec cmd 功能禁用
indexes: 开起索引功能,比如一个请求到目录urlz中没有有directoryindex 指令指定的索引文件,那么服务器会自动返回一个请求目录内容列表
followsymlinks: 允许在当前环境使用符号连接,但是在location 容器中会被忽略
all: 使用除multiviews之外的所有特性,也是options的默认参数
multiviews: 用于启动mod_negotiations模块提供的多重视图功能
11 、servertokens: os/major/full 影响报错页脚信息的详细程度,一般不建议使用
(2) 容器部分
1 、<ifmodule> 容器:容器作用于模块,他会首先判断模块是否载入,然后再决定是否进行处理,即只有当判断结果为真时,才会执行容器内的指令,相反如果为假,则会全部忽略,可以使用<ifmodule 模块名>或者<ifmodule !模块名> 来判断模块是否载入
<ifmodule mpm_netware_module>
dirctoryindex index.html
</ifmodule>
如果载入则执行
<ifmodule !mpm_netware_module>
dirctoryindex index.html
</ifmodule>
如果不载入则执行
2 <ifdefine> 容器:封装一组条件为真时才生效的指令,作用于 server config, virtual host, directory, .htaccess ,和ifmodule区别在于,他是以模块是否加载作为判断,但是ifdefine是以条件为判断
依据
<ifdefine proxy>
loadmodule proxy_module modules/libproxy.so
</ifdefine>
3 <directory> <directorymatch> 容器:
directory: 让它封装的指令在它的指定的目录或者他的子目录起作用,这个目录必须是一个完整的路径,当然你也可以使用通配符* ?匹配目录,也考虑利用使用[]来确定字符范围,不过不论是哪一种都不能匹配/
<directory /var/apache/html>
order deny,allow
deny from all
</directory>
上述例子禁止了对/var/apache/html目录的访问权限。任何请求到/var/apache/html都会被拒绝
如果希望目录使用正则表达式,那么需要在前面加一个~
<directory ~ "^/var/apache[0-9]{2}/html">
order deny,allow
allow from all
</directory>
directorymatch: 和directory作用类似,只不过他可以直接接受正则匹配 而不需要加一个~符号
<directorymatch "^/var/apache[0-9]{2}/html">
order deny,allow
allow from all
</directorymatch>
4 <files> 和 <filesmatch>
files: 只作用于文件,也可以使用通配符和[]以及在正则表达式前面~来使用正则表达式
<files "^\.css">
order deny,allow
allow from all
</files>
filesmatch: files 而不需要加一个~符号
<filesmatch "\.(gif|jpe?g|png)$">
order deny,allow
allow from all
</filesmatch>
5 <location> 和 <locationmatch>: 只是对url进行访问控制
<location /cgi>order allow,denydeny from all</location>
如果以cgi开头url则会被拒绝
另外还可以将url 请求映射到apache模块处理器上,例如使用mod_status模块:
<location /server-status>
sethandler server-status
</location>
如果使用上面的配置。那么访问/server-status,apache会将连接交给mod_status模块处理,并返回一个apache服务器运行状态页面
容器的处理顺序问题:
apache 会优先处理directory 容器(但是不会处理带有正则表达式的directory和.htaccess,)接着处理files 和 filesmatch 容器,再接着就是处理location 和locationmatch容器
<location /var/apache/html>
order deny,allow
allow from all
</location>
<direcotry /var/apache/html>
order allow,deny
allow from all
deny from www.jons.com
</direcotry>
上述例子,由于apache会先处理<directory>容器,最后处理的<location>容器会覆盖之前directory配置,因此对于www.json.com将是允许被用户访问的,如果容器相同则按照字典顺序由短到长来处理。
更多相关问题请访问:linux视频教程
以上就是apache服务配置详细讲解的详细内容。