初识apache服务(3)---httpd的常见配置

包含其它配置文件

  • Include file-path|directory-path|wildcard
  • IncludeOptional file-path|directory-path|wildcard

区别:
当无匹配文件时,include会报错,IncludeOptional会忽略错误
建议改配置在包含文件上操作

Include conf.modules.d/*.conf
IncludeOptional conf.d/*.conf                                                                                                                                             

监听端口

  • LISTEN

Listen指令至少一个,可重复出现多次。默认监听本机所有IP的80端口,也可监听本机指定IP端口

#Listen 12.34.56.78:80
Listen 80

定义Main server的文档页面路径

  1. 指定URL的起始位置
  2. 访问授权
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "/var/www/html"   
<Directory "/var/www/html">
    AllowOverride None
    Require all granted
</Directory>

定义站点主页面

# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>
    DirectoryIndex index.html 
</IfModule>

服务器会依次查找主页面

[root@centos8 ~]#ll /var/www/html/
total 8
-rw-r--r-- 1 root root 10 Jan 22 15:46 index.htm
-rw-r--r-- 1 root root 10 Jan 22 15:47 index.html
[root@centos8 ~]#curl 10.0.0.8
html file
[root@centos8 ~]#rm -rf /var/www/html/index.html
[root@centos8 ~]#curl 10.0.0.8
 htm file

针对目录和URL实现访问控制

** Options指令:**
后跟1个或多个以空白字符分隔的选项列表, 在选项前的+,- 表示增加或删除指定选项。需同时+或-。默认禁止访问索引列表
常见选项:

  • Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户

  • FollowSymLinks:允许访问符号链接文件所指向的源文件

  • None:全部禁用

  • All: 全部允许

[root@centos8 ~]#cat /etc/httpd/conf.d/test.conf 
DocumentRoot "/data/html"  
<Directory "/data/html">
	options Indexes FollowSymLinks
	Require all granted
</Directory>

[root@centos8 ~]#ll /data/html/
total 4
drwxr-xr-x 2 root root  6 Jan 22 16:11 hahah
drwxr-xr-x 2 root root  6 Jan 22 16:11 hehehe
-rw-r--r-- 1 root root 11 Jan 22 16:10 index.html
lrwxrwxrwx 1 root root  5 Jan 22 16:03 linketc -> /etc/

模拟URL路径不存在

[root@centos8 ~]#rm -f /data/html/index.html 
[root@centos8 ~]#curl 10.0.0.8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /</title>
 </head>
 <body>
<h1>Index of /</h1>
  <table>
   <tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr>
   <tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="hahah/">hahah/</a>                 </td><td align="right">2021-01-22 16:11  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="hehehe/">hehehe/</a>                </td><td align="right">2021-01-22 16:11  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="linketc/">linketc/</a>               </td><td align="right">2021-01-22 15:48  </td><td align="right">  - </td><td>&nbsp;</td></tr>
   <tr><th colspan="5"><hr></th></tr>
</table>
</body></html>

模拟未开启允许访问符号链接文件

[root@centos8 ~]#cat /etc/httpd/conf.d/test.conf 
DocumentRoot "/data/html"  
<Directory "/data/html">
	options Indexes
	Require all granted
</Directory>
[root@centos8 ~]#curl 10.0.0.8
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /</title>
 </head>
 <body>
<h1>Index of /</h1>
  <table>
   <tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr>
   <tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="hahah/">hahah/</a>                 </td><td align="right">2021-01-22 16:11  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="hehehe/">hehehe/</a>                </td><td align="right">2021-01-22 16:11  </td><td align="right">  - </td><td>&nbsp;</td></tr>
   <tr><th colspan="5"><hr></th></tr>
</table>

AllowOverride指令
与访问控制相关的哪些指令可以放在指定目录下的.htaccess(由AccessFileName 指令指定,AccessFileName .htaccess 为默认值)文件中,覆盖之前的配置指令,只对语句有效
常见用法:

  • AllowOverride All: .htaccess中所有指令都有效
  • AllowOverride None: .htaccess 文件无效,此为httpd 2.3.9以后版的默认值
  • AllowOverride AuthConfig .htaccess 文件中,除了AuthConfig 其它指令都无法生效

基于客户端的IP地址的访问控制

  • 无明确授权的目录,默认拒绝
  • 允许所有主机访问:Require all granted
  • 拒绝所有主机访问:Require all denied

控制特定的IP访问:

  • Require ip IPADDR:授权指定来源的IP访问
  • Require not ip IPADDR:拒绝特定的IP访问

控制特定的主机访问:

  • Require host HOSTNAME:授权特定主机访问
  • Require not host HOSTNAME:拒绝

HOSTNAME:

  • FQDN:特定主机
  • domin.tld:指定域名下的所有主机

基于用户的访问控制

htpasswd
-c 自动创建文件,仅应该在文件不存在时使用
-p 明文密码
-d CRYPT格式加密,默认
-m md5格式加密
式加密
-D 删除指定用户

配置:

AuthType Basic
AuthName "Administator private"
AuthUserFile "/etc/httpd/conf.d/.htpasswd"
AuthGroupFile "/etc/httpd/conf.d/.htgroup"

猜你喜欢

转载自blog.csdn.net/weixin_50904580/article/details/112986207