浅谈Apache服务(二)

定义Main server的文档页面路径

  • 设置格式DocumentRoot "/path"
    DocumentRoot指向的路径为URL路径的起始位置
DocumentRoot “/path”
<directory /path>
Require all granted
</directory>
  • 注意
    • SELinuxiptables的状态影响设置是否生效
    • DocumentRoot指向的路径为URL路径的起始位置
    • /path必须显式授权后才可以访问
  • 范例
vim /etc/httpd/conf.d/test.conf             / 主配置文件下面的子配置文件
DocumentRoot "/data/html"
<directory /data/html>
   Require all granted
</directory>                               / 显示授权

mkdir /data/html
echo "/data/html/index.html" > /data/html/index.html    / 生成页面文件
systemctl reload httpd

[root@Centos7 ~]# curl 172.20.54.1
 /data/html/index.html 

定义站点主页面

  • 设置格式DirectoryIndex index.html index.html.var

  • 当URL并未明确目录内的文件时,根据设置中的文件依次查询

  • 范例:当访问bbs目录,设置主页面为index文件

mkdir /data/html/bbs           / 此时的httpd根目录是/data/html
echo "/data/html/bbs/index" > /data/html/bbs/index

/ 此时在配置文件不设置DirectoryIndex的值
[root@Centos7 ~]# curl 172.20.54.1/bbs/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>          / 403错误 请求被拒绝
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /bbs/
on this server.</p>
</body></html>

/ 设置DirectoryIndex的值包含index
vim /etc/httpd/conf.d/test.conf
DirectoryIndex index.html index

systemctl reload httpd
[root@Centos7 ~]# curl 172.20.54.1/bbs/
/data/html/bbs/index

站点访问控制常见机制

访问控制机制有两种:客户端来源地址,用户账号

1️⃣被访问控制的资源描述方式:

  • 文件系统路径:
// 基于目录
<Directory “/path">
...
</Directory>

// 基于文件
<File “/path/file”>
...
</File>

// 基于文件通配符
<File “/path/*file*”>
...
</File>

//基于正则表达式
<FileMatch “regex”>
...
</FileMatch>
  • 范例:
<FilesMatch ".+\.(gif|jpe?g|png)$">
# ...
</FilesMatch>
<FilesMatch "\.(gif|jpe?g|png)$">
<Files “?at.*”> 通配符
<Files ".ht*">
Require all denied
</Files>
  • URL路径:
// URL匹配
<Location "URL">
...
</Location>

// 正则表达式匹配
<LocationMatch "regex">
...
</LocationMatch>
  • 范例:
#/private1, /private1//private1/file.txt 匹配
#/private1other 不匹配
<Location "/private1">
# ...
</Location>
#/private2//private2/file.txt 匹配
#/private2,/private2other 不匹配
<Location "/private2/">
# ...
</Location>

2️⃣针对目录实现访问控制

1️⃣Options:后跟1个或多个以空白字符分隔的选项列表

  • 选项前的+-表示增加或删除指定选项

  • 常见选项

    • Indexes:指明的URL路径下不存在与定义的主页面资源相符的资源文件时,返回索引列表给用户
    • FollowSymLinks:允许访问符号链接文件所指向的源文件
    • None:全部禁用
    • All:全部允许
  • 范例

cd /etc/httpd/conf.d/
mv welcom.conf{,.bak}                     / 取消默认欢迎页面
echo /data/data.html > /data/data.html
ln -s /data /var/www/html/datalink
echo "test page" > /var/www/html/test.html
/ 注意 此时如果/var/www/html下有index.html 文件请删除掉
systemctl reload httpd

打开浏览器,访问 http://httpd主机IP/ 可看到下面所示
在这里插入图片描述

/ 在配置文件中把followsymlinks选项去掉
vim /etc/httpd/conf.d/test.conf
<directory /var/www/html>
    options -followsymlinks
</directory>

systemctl reload httpd     / 生效配置文件

打开浏览器,访问 http://httpd主机IP/ 可看到下面所示
在这里插入图片描述

2️⃣AllowOverride指令

  • 指定目录下的.htaccess(由AccessFileName指定)文件中哪些访问控制相关的指令可以生效

    • 只对<directory>语句有效
    • AllowOverride All:所有指令都有效
    • AllowOverride None:所有指令都无效
    • AllowOverride AuthConfig Indexes:只有AuthConfigIndexes指令可以生效
  • 范例

vim /etc/httpd/conf.d/test.conf           / 子配置文件
<directory /var/www/html>                 / 必须是directory语句
Options indexes
Allowoverride options=Followsymlinks,indexes
</directory>
mkdir /var/www/html/dir1
ln -s /etc/ /var/www/html/dir1/etclink    / 软链接

vim /var/www/html/dir1/.htaccess
options Followsymlinks

systemctl restart httpd

打开浏览器,访问http://httpd主机IP/dir1,可以看到etclink的软链接
在这里插入图片描述
打开浏览器,访问http://httpd主机IP/ 可看到下面所示,无法看软链接目录datalink
在这里插入图片描述

3️⃣基于客户端 IP 地址实现访问控制

针对各种资源,可以基于以下两种方式的访问控制

  • 客户端来源地址
  • 用户账号

基于客户端的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:指定域名下的所有主机

不能有失败,至少有一个成功匹配才成功,即失败优先

  • 示例
vim /etc/httpd/conf.d/test.conf
<directory /var/www/html>            / 权限必须是基于directory目录进行限制的
<requireall>
require all granted
require not ip 172.20.54.3         / 除了172.20.54.3 其他IP都放行
</requireall>
</directory>

httpd -t
systemctl restart httpd

[root@test002 ~]# curl -I 172.20.54.1   / 172.20.54.3被拒绝
HTTP/1.1 403 Forbidden
Date: Wed, 11 Dec 2019 01:50:07 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

[root@Centos7 ~]# curl -I 172.20.54.1   / 172.20.54.2通过
HTTP/1.1 200 OK
Date: Wed, 11 Dec 2019 01:49:59 GMT
Server: Apache
Content-Type: text/html;charset=ISO-8859-1

多个语句有一个成功,则成功,即成功优先

  • 示例
vim /etc/httpd/conf.d/test.conf
<directory /var/www/html>
<requireany>
require all denied                   / 全部拒绝
require ip 172.20.54.3               / 仅允许172.20.54.3访问
</requireany>
</diretory>

httpd -t
systemctl restart httpd

[root@test002 ~]# curl -I 172.20.54.1  / 172.20.54.3访问通过
HTTP/1.1 200 OK
Date: Wed, 11 Dec 2019 02:00:10 GMT
Server: Apache
Content-Type: text/html;charset=ISO-8859-1

[root@Centos7 ~]# curl -I 172.20.54.1  / 172.20.54.2访问拒绝
HTTP/1.1 403 Forbidden
Date: Wed, 11 Dec 2019 02:00:41 GMT
Server: Apache
Content-Type: text/html; charset=iso-8859-1

日志设定

  • 日志类型:
    • 访问日志/etc/httpd/logs/access_log
    • 错误日志/etc/httpd/logs/error_log
  • 错误日志
    ErrorLog logs/error_log LogLevel warn LogLevel
    可选值: debug, info, notice, warn,error, crit, alert,emerg
  • 访问日志
    • 定义日志格式
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    • 使用日志格式
      CustomLog logs/access_log combined
    • 日志格式标识符含义
      %h:客户端IP地址
      %l:远程用户,启用mod_ident才有效,通常为减号“-”
      %u:验证(basic,digest)远程用户,非登录访问时,为一个减号“-”
      %t:服务器收到请求时的时间
      %r:First line of request,即表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本
      %>s:响应状态码
      %b:响应报文的大小,单位是字节;不包括响应报文http首部
      %{Referer}i:请求报文中首部“referer”的值;即从哪个页面中的超链接跳转至当前页面的
      %{User-Agent}i:请求报文中首部“User-Agent”的值;即发出请求的应用程序
      全部日志标识符http://httpd.apache.org/docs/2.4/mod/mod_log_config.html
  • 示例
vim /etc/httpd/conf/httpd.conf
<IfModule log_config_module>
    LogFormat "%h %f %t %U" hello   / 访问IP 文件名 请求时间 URL路径
    CustomLog "logs/access_log" hello
</IfModule>

httpd -t
systemctl reload httpd
[root@Centos7 ~]# tail -F /etc/httpd/logs/access_log 
172.20.54.3 /var/www/html/ [11/Dec/2019:10:13:25 +0800] /
172.20.54.3 /var/www/html/ [11/Dec/2019:10:13:43 +0800] /
172.20.54.3 /var/www/html/ [11/Dec/2019:10:13:47 +0800] /
172.20.3.69 /var/www/html/ [11/Dec/2019:10:14:02 +0800] /
172.20.3.69 /var/www/html/favicon.ico [11/Dec/2019:10:14:02 +0800] /favicon.ico
172.20.3.69 /var/www/html/ [11/Dec/2019:10:14:49 +0800] /
172.20.3.69 /var/www/html/favicon.ico [11/Dec/2019:10:14:49 +0800] /favicon.ico
172.20.3.69 /var/www/html/ [11/Dec/2019:10:14:51 +0800] /
172.20.3.69 /var/www/html/dir1/ [11/Dec/2019:10:14:55 +0800] /dir1/
172.20.3.69 /var/www/html/dir1/etclink/ [11/Dec/2019:10:14:56 +0800] /dir1/etclink/
172.20.3.69 /usr/share/httpd/icons/unknown.gif [11/Dec/2019:10:14:56 +0800] /icons/unknown.gif
172.20.3.69 /usr/share/httpd/icons/image2.gif [11/Dec/2019:10:14:56 +0800] /icons/image2.gif

设定默认字符集

  • 设置格式:AddDefaultCharset UTF-8 #此为默认值

  • 中文字符集:GBK, GB2312, GB18030

  • 示例

vim /etc/httpd/conf/httpd.conf

adddefalutcharset gbk2312

httpd -t
systemctl restart httpd
[root@test002 ~]# curl -I 172.20.54.1
HTTP/1.1 200 OK
Date: Wed, 11 Dec 2019 03:09:23 GMT
Server: Apache
Last-Modified: Wed, 11 Dec 2019 03:07:07 GMT
ETag: "4-59964ec8bc6e0"
Accept-Ranges: bytes
Content-Length: 4
Content-Type: text/html; charset=gbk2312

定义路径别名

  • 设置格式:Alias /URL/ “/PATH/”
  • 示例
vim /etc/httpd/conf.d/test.conf
alias /dir2 /data/html     / 别名
<directory /data/html>     
require all granted        / 授权
</directory>
mkdir /data/html
echo "/data/html/index.html " > /data/html/index.html

httpd -t
systemctl restart httpd
[root@test002 ~]# curl 172.20.54.1/dir2/
 /data/html/index.html 

基于用户的访问控制

1️⃣认证的相关概念

  • 认证质询:WWW-Authenticate,响应码为401,拒绝客户端请求,并说明要求客户端需要提供账号和密码
  • 认证:Authorization,客户端用户填入账号和密码后再次发送请求报文;认证通过时,则服务器发送响应的资源
  • 认证方式两种:
    • basic:明文
    • digest:消息摘要认证,兼容性差
  • 安全域:需要用户认证后方能访问的路径;应该通过名称对其进行标识,以便于告知用户认证的原因
  • 用户的账号和密码
    • 虚拟账号:仅用于访问某服务时用到的认证标识
    • 存储:文本文件,SQL数据库,ldap目录存储,nis等

2️⃣basic认证

  • basic认证配置格式:
    1.定义安全域
<Directory "/path">
AuthType Basic                                  //认证方式
AuthName "String"                               //认证提示字符串
AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE"     //认证文件路径
Require user username1 username2 ...     //允许访问安全域的用户
</Directory>

允许账号文件中的所有用户登录访问:Require valid-user

2.使用htpasswd命令生成存储账号密码的文件

htpasswd [options] /PATH/HTTPD_PASSWD_FILE username
-c:自动创建文件,仅应该在文件不存在时使用
-m:md5格式加密
-s:sha格式加密
-D:删除指定用户
  • 实验:只允许用户xiaoming, xiaohong登录172.20.54.1/secret目录
/ 启用登录验证模块
vim /etc/httpd/conf.modules.d/00-base.conf
LoadModule auth_basic_module modules/mod_auth_basic.so
mkdir -p /var/www/html/secret
echo /var/www/html/secret > /var/www/html/secret   
/ 定义安全域
vim /etc/httpd/conf.d/test.conf
<directory /var/www/html/secret>
   Authtype  basic
   AuthName "FBI warning"
   AuthUserfile "/etc/httpd/conf.d/.httpuser"
   require user xiaoming xiaohong
</directory>

/ 生成登录验证文件 虚拟账户
htpasswd -c /etc/httpd/conf.d/.httpuser xiaoming  / 第一次生成验证文件
htpasswd -s /etc/httpd/conf.d/.httpuser xiaohong  / 第二次追加账户文件 -s 加密
cat conf.d/.httpuser 
xiaoming:$apr1$tgv.jp7p$g6DprZ1E0ci/z4t2WP.zh.
xiaohong:{SHA}fEqNCco3Yq9h5ZUglD3CZJT4lBs=

httpd -t
systemctl restart httpd

在这里插入图片描述
在这里插入图片描述

3️⃣基于组账号认证

  • 组账号认证配置格式
定义安全域
<Directory "/path">
AuthType Basic                                 
AuthName "String"                             
AuthUserFile "/PATH/HTTPD_USER_PASSWD_FILE"     
AuthGroupFile "/PATH/HTTPD_GROUP_FILE"
Require group grpname1 grpname2 ...     //允许访问安全域的组
</Directory>
  • 创建用户账号和组账号文件
    • 组文件:每一行定义一个组
      GRP_NAME: username1 username2 ...
  • 范例
/ 方法一
vim /var/www/html/secret/.htacess
Authtype Basic
AuthName "FBI warning"
AuthUserfile "/etc/httpd/conf.d/.httpuser"
AuthGroupfile "/etc/httpd/conf.d/.httpgroup"
require group webadmins

vim /etc/httpd/conf.d/.httpgroup
webadmins: xiaoming xiaobai

httpd -t
systemctl restart httpd

/ 方法二
vim /etc/httpd/conf.d/test.conf
<directroy /var/www/html/secret>
   Authtype basic
   AuthName "FBI warning"
   AuthUserfile "/etc/httpd/conf.d/.httpuser"
   Authgroupfile "/etc/httpd/conf.d/.httpgroup"
	require  group webadmins
</directory>

vim /etc/httpd/conf.d/.httpgroup
webadmins xiaoming xiaobai

httpd -t
systemctl restart httpd

在这里插入图片描述
在这里插入图片描述

远程客户端和用户验证的控制

  • Satisfy ALL|Any
  • ALL:验证条件需要都通过才可以
  • Any:验证条件有一个满足即可
  • 示例
[root@Centos7 ~]# vim /etc/httpd/conf.d/test.conf
<directory /var/www/html/test>
	require valid-user
	allow from 172.20.54
	satisfy any
</directory>
/172.20.54.2 机器访问
[root@test002 ~]# curl -I 172.20.54.1/test/
HTTP/1.1 200 OK
Date: Wed, 11 Dec 2019 08:27:08 GMT
Server: Apache/2.4.6 (CentOS)
Content-Type: text/html;charset=ISO-8859-1

禁止错误网页版本泄露

  • ServerSignature On | Off | EMail
  • 默认值Off,当客户请求的网页并不存在时,服务器将产生错误文档,
  • 如果ServerSignature选项为on,错误文档的最后一行将包含服务器名字、Apache版本等信息,如果不对外显示这些信息,就可将这个参数设置为Off,
  • 设置为Email,将显示ServerAdmin 的Email提示

禁止trace方法

  • TraceEnable [on|off|extended]
  • 是否支持trace方法,默认on,基于安全风险,建议关闭
  • 范例:关闭trace方法
[root@Centos7 ~]# curl -IX OPTIONS http://127.0.0.1
HTTP/1.1 200 OK
Date: Wed, 11 Dec 2019 08:34:20 GMT
Server: Apache/2.4.6 (CentOS)
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Length: 0
Content-Type: httpd/unix-directory

[root@Centos7 ~]# vim /etc/httpd/conf.d/test.conf 
traceenable off

httpd -t 
systemctl reload httpd
[root@Centos7 ~]# curl -IX OPTIONS http://127.0.0.1
HTTP/1.1 200 OK
Date: Wed, 11 Dec 2019 08:35:16 GMT
Server: Apache/2.4.6 (CentOS)
Allow: POST,OPTIONS,GET,HEAD      / trace 已经关闭
Content-Length: 0
Content-Type: httpd/unix-directory

status 状态页

httpd提供了状态页,可以用来观察httpd的运行情况。

  • 此功能需要加载mod_status.so模块才能实现
  • 配置格式
LoadModule status_module modules/mod_status.so
<Location /server-status>
SetHandler server-status
Order allow,deny
Allow from 172.20     //只允许172.20.0.0网段主机访问status页面
</Location>
ExtendedStatus On 显示扩展信息
  • 示例:只允许172.20.0.0/16网段的主机访问172.20.54.1的status页面
/ 确认模块加载
[root@Centos7 ~]# httpd -M| grep status
 status_module (shared)
/ 第一种方法
 [root@Centos7 ~]# vim /etc/httpd/conf.d/status.conf
<location /status>
	sethandler server-status
	<requireany>
	require all denied
	require ip 172.20.
	</requireany>
</location>
extendedstatus on

/ 第二种方法 
 [root@Centos7 ~]# vim /etc/httpd/conf.d/status.conf
 <Location /status>
        SetHandler server-status
        Order allow,deny
        Allow from 172.20.
</Location>
ExtendedStatus On

httpd -t
systemctl restart httpd

在这里插入图片描述


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值