Web service(二)

一.httpd-2.2

1.常用配置文件

1.主配置文件:/etc/httpd/conf/httpd.conf

1.### Section 1: Global Environment(全局环境配置)
2.### Section 2: 'Main' server configuration(主服务配置)
3.### Section 3: Virtual Hosts(虚拟主机)
[root@grub6 ~]# grep "^###" /etc/httpd/conf/httpd.conf 
### Section 1: Global Environment
### Section 2: 'Main' server configuration
### Section 3: Virtual Hosts
ps:需要注意在centos6,httpd-2.2中,若需要启用虚拟主机,需要将主服务器关闭,反之亦然

2.配置格式:drective value

1.directive:不区分大小写
2.value:其值为路径时,是否区分字符大小写,取决于文件系统

3.安装帮助文件

安装完成后即可查看本机自带的帮助文件
[root@grub6 ~]# yum -y install httpd-manual

在这里插入图片描述

2.常用配置

1.修改监听的IP和PORT: Listen [IP-address:]portnumber [protocol]

1.省略IP即表示为0.0.0.0(监听本在本机的所有地址上)
2.LISTEN指令可以重复出现多次
	1)LISTEN 80
	2)LISTEN 8080
3.修改监听的socket,重启服务进程后方可生效
4.若限制其必须通过ssl通信时,protocol需要定义为https
ps:需要注意http是一个文本协议
"直接与80端口进行通信"
[root@grub6 ~]# yum -y install telnet
[root@grub6 ~]# telnet 192.168.3.20 80
Trying 192.168.3.20...
Connected to 192.168.3.20.
Escape character is '^]'.

2.持久连续(persistent connection):tcp连续建立后,每个资源获取完成之后不会断开连接,而是继续等待其他资源请求的进行
1)持久连续断开限制

1.数量限制:限定其获取网页资源的数量
2.时间限制:限定其获取网页资源的时长
3.副作用:对于并发访问量较大的服务器,长连接机制会使得后续某些请求无法得到正常响应
4.折中方法:使用较短的持久连接时长,以及较少的请求数量
ps:两者满足其一则断开其tcp连接

2)启用保持连接功能

1.KeepAlive On|Off (启动或关闭)
2.KeepAliveTimeout 15 (超时时长)
3.MaxKeepAliveRequests (获取的最大请求资源量)
请求报文格式
telnet WEB_SERVER_IP PORT
GET /URL HTTP/1.1
Host:WEB_SERVER_IP
测试关闭长连接功能
[root@grub6 ~]# vim /etc/httpd/conf.d/KeepAlive.conf
	KeepAlive Off
[root@grub6 conf]# httpd -t
Syntax OK "检查配置文件语是否存在错误"
[root@grub6 conf]# service httpd restart
[root@grub6 conf]# telnet 192.168.3.20 80
Trying 192.168.3.20...
Connected to 192.168.3.20.
Escape character is '^]'.
"GET /index.html HTTP/1.1
Host:192.168.3.20   请求报文格式"

HTTP/1.1 200 OK
Date: Sat, 22 Jun 2019 13:22:28 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Mon, 27 May 2019 05:31:08 GMT
ETag: "221dca-6-589d7db01b07b"
Accept-Ranges: bytes
Content-Length: 6
Connection: close  "响应报文守护"
Content-Type: text/html; charset=UTF-8

hello
"Connection closed by foreign host.  响应完成直接关闭tcp连接"
测试打开长连接功能
[root@grub6 ~]# vim /etc/httpd/conf.d/KeepAlive.conf 
KeepAlive On
KeepAliveTimeout 30
MaxKeepAliveRequests 100
[root@grub6 ~]# httpd -t
httpd: apr_sockaddr_info_get() failed for grub6
Syntax OK
[root@grub6 ~]# service httpd restart
[root@grub6 ~]# telnet 192.168.3.20 80
Trying 192.168.3.20...
Connected to 192.168.3.20.
Escape character is '^]'.
GET /index.html HTTP/1.1
Host:192.168.3.20

HTTP/1.1 200 OK
Date: Sat, 22 Jun 2019 13:28:57 GMT
Server: Apache/2.2.15 (CentOS)
Last-Modified: Mon, 27 May 2019 05:31:08 GMT
ETag: "221dca-6-589d7db01b07b"
Accept-Ranges: bytes
Content-Length: 6
Content-Type: text/html; charset=UTF-8

"hello  已经获取相关数据,并且此时tcp连接不会断开,需要等待设置的30秒后才会断开连接"
需要注意:httpd-2.4的KeepAliveTimeout可以是毫秒级,即KeepAliveTimeout num[ms]

3.MPM:httpd-2.2不支持同时编译多个MPM模块,所以只能编译选定要使用的那个
1)centos 6的rpm包为此专门提供了三个应用程序文件,httpd(prefork),httpd.worker,httpd.event,分别用于实现对不同MPM机制的支持,可使用ps aux | grep httpd进行查看

[root@grub6 ~]# ps aux | grep httpd
root       2124  0.0  0.3 186152  4004 ?        Ss   08:28   0:00 /usr/sbin/httpd
apache     2134  0.0  0.2 186152  2568 ?        S    08:28   0:00 /usr/sbin/httpd
apache     2135  0.0  0.2 186152  2568 ?        S    08:28   0:00 /usr/sbin/httpd
apache     2136  0.0  0.2 186152  2568 ?        S    08:28   0:00 /usr/sbin/httpd
apache     2137  0.0  0.2 186152  2588 ?        S    08:28   0:00 /usr/sbin/httpd
apache     2138  0.0  0.2 186152  2568 ?        S    08:28   0:00 /usr/sbin/httpd
apache     2139  0.0  0.2 186152  2568 ?        S    08:28   0:00 /usr/sbin/httpd
apache     2141  0.0  0.2 186152  2568 ?        S    08:28   0:00 /usr/sbin/httpd
apache     2142  0.0  0.2 186152  2568 ?        S    08:28   0:00 /usr/sbin/httpd
root       2506  0.0  0.0 103348   860 pts/0    S+   08:49   0:00 grep httpd

2)默认使用的为/usr/sbin/httpd,其为prefork的MPM模块

查看httpd程序的模块列表
1.查看静态编译的模块
[root@grub6 ~]# httpd -l
Compiled in modules:
  core.c
  prefork.c
  http_core.c
  mod_so.c
[root@grub6 ~]# httpd.worker -l
Compiled in modules:
  core.c
  worker.c
  http_core.c
  mod_so.c
2.查看静态编译及动态编译的模块
[root@grub6 ~]# httpd -M
httpd: apr_sockaddr_info_get() failed for grub6
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 auth_basic_module (shared)
 auth_digest_module (shared)
.
.

3)更换使用httpd-2.2程序,以实现支持其他MPM机制:/etc/sysconfig/httpd

[root@grub6 ~]# vim /etc/sysconfig/httpd 
	HTTPD=/usr/sbin/httpd.worker
[root@grub6 ~]# service httpd restart
[root@grub6 ~]# ps aux | grep "httpd"
root       2553  0.0  0.4 186360  4192 ?        Ss   08:56   0:00 /usr/sbin/httpd.worker
apache     2668  0.0  0.5 530620  5452 ?        Sl   08:56   0:00 /usr/sbin/httpd.worker
root       2697  0.0  0.0 103344   860 pts/0    S+   08:56   0:00 grep httpd
ps:即在脚本配置文件中设定即可HTTPD=/usr/sbin/httpd.{worker,event},重启服务后即可以生效

4)MPM配置:centos7在/etc/httpd/conf.modules.d/00-mpm.conf下进行配置,centos6可在/etc/httpd/conf/httpd.conf下配置

"prefork的配置"
<IfModule prefork.c>
StartServers	8 (进程创建时,启动多少个子进程)
MinSpareServers	5 (最小空闲进程数)
MaxSpareServers	20 (最大空闲进程数)
ServerLimit	20	(对MaxClients,允许其在生命周期内,处于活跃的服务器进程有多少)
MaxClients	256	(最多所允许创建的子进程数量)
MaxRequestsPerChild	4000 (每个子进程最多处理多少个请求后就退出)
</IfModule>
ps:若需要对httpd-2.4进行自定义配置,即可将其配置至/etc/httpd/conf.modules.d/00-mpm.conf中,centos7一般为默认配置,没有MPM的配置模板
"worker的配置"
<IfModule worker.c>
StartServers         4  (程创建时,启动多少个子进程)
MaxClients         300	(最大创建的线程数)
MinSpareThreads     25	(最小空闲的线程数)
MaxSpareThreads     75	(最大空闲的线程数)
ThreadsPerChild     25	(每个子进程生成多少的线程)
MaxRequestsPerChild  0	(每个进程最多处理多少个请求后就退出,0表示永不退出)
</IfModule>
ps:由于worker模型中一开始会创建4个子进程,每个子进程生成25个线程,即共100个线程,但由于其最大空闲线程数为75,即会再关闭一个子进程,可使用[root@grub6 ~]# service httpd restart; watch -n0.5 'ps aux | grep httpd'观察其过程
"event配置"
一般于httpd-2.4中配置,其配置模型相似于worker

5)更换httpd-2.4的MPM

[root@sakura ~]# vim /etc/httpd/conf.modules.d/00-mpm.conf 
	LoadModule mpm_worker_module modules/mod_mpm_worker.so "启用对应模块即可"
[root@sakura ~]# httpd -t
	Syntax OK
[root@sakura ~]# systemctl restart httpd
[root@sakura ~]# httpd -M | grep mpm
 mpm_worker_module (shared)  "确认当前模块"

4.PV,PU

1.PV:Page View;一个页面可能有多个资源,获取一个页面上的所有资源叫做一次页面访问
2.UV:User View;独立用户浏览量
3.IP:基于IP进行访问,其值一般应小于UV(一般为同一IP对应多个用户)

5.DSO(Dynamic Shared object 动态共享对象)

"配置指定实现模块加载"
LoadModule  <mod_name>  <mod_path>
[root@sakura ~]# httpd -M | grep proxy
proxy_module (shared)
proxy_ajp_module (shared)
proxy_balancer_module (shared)
proxy_connect_module (shared)
proxy_express_module (shared)
proxy_fcgi_module (shared)
proxy_fdpass_module (shared)
proxy_ftp_module (shared)
proxy_http_module (shared)
proxy_scgi_module (shared)
proxy_wstunnel_module (shared)
[root@sakura ~]# vim /etc/httpd//conf.modules.d/00-proxy.conf
   #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so  "将不需要的模块进行注释即可"
[root@sakura ~]# systemctl restart httpd
[root@sakura ~]# httpd -M | grep proxy  "对应模块已经消失"
"模块文件路径可使用相对路径"
相对于ServerRoot(默认为/etc/httpd)
[root@sakura ~]# ls /etc/httpd/
conf  conf.d  conf.modules.d  logs  modules  run
[root@sakura ~]# ls -l /etc/httpd/modules  "其模块文件为一个链接文件"
lrwxrwxrwx. 1 root root 29 5月   7 00:42 /etc/httpd/modules -> ../../usr/lib64/httpd/modules
[root@sakura ~]# ls /usr/lib64/httpd/modules/
mod_access_compat.so    mod_autoindex.so      mod_ext_filter.so           mod_mpm_event.so       mod_rewrite.so
.
.
ps:httpd所有模块都在 /usr/lib64/httpd/modules/下,可自行进行定义;加载第三方模块,编译后也会存放于此目录下,进行自定义加载

6.定义Main server的文档页面路径
1)servername

"servername:	语法ServerName [scheme://]fully-qualified-domain-name[:port]"
http服务器名称,和真正服务器主机名无关;若没有定义http服务启动时,
会自动反解本机Ip地址为主机名,并将其作为servernam;若失败则会有警告
"1.警告内容"
[root@grub6 ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:httpd: apr_sockaddr_info_get() failed for grub6
httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName 
                                                           [确定]
"2.设定servername"
[root@grub6 ~]# vim /etc/httpd/conf/httpd.conf
	使用/ServerName进行查找,并进行设定,此时即不会有错误信息
	ServerName 192.168.3.20
[root@grub6 ~]# service httpd restart
停止 httpd:                                               [确定]
正在启动 httpd:                                           [确定]

2)DocumentRoot “”:站点的URL会映射到本地的哪一个文件系统路径下

"文档路径映射"
1.DoucmentRoot指向的路径为URL路径的起始位置,其相当于站点URL的根路径;
2.URL PATH与FileSystem PATH不是等同的,而是存在一种映射关系
	1)URL /  --> FileSystem /var/www/html/
	2)/images/logo.jpg --> /var/www/html/images/logo.jpg
"测试"
[root@sakura ~]# mkdir -p /date/www/html
[root@sakura ~]# vim /date/www/html/index.html
	<h1> Main Server test </h1>
[root@sakura ~]# vim /etc/httpd/conf/httpd.conf  "更改其配置文件"
	DocumentRoot "/date/www/html"
[root@sakura ~]# systemctl restart httpd
ps:需要注意,更改配置文件完成后,还需要使用Directory进行配置才可访问自定义的页面

7.站点访问控制常见机制:可基于两种机制指明对哪些资源进行何种访问控制

"文件系统路径"
<Directory  "">目录下的所有资源
...
</Directory>
						
<File  ""> 一组文件,可以使用通配符
...
</File>
						
<FileMatch  "PATTERN"> 可以使用正则表达式
						...
</FileMatch>
"URL路径"
<Location  "">
...
</Location>
						
<LocationMatch "PATTERN">
...
</LocationMatch>

1)允许所有来源可访问相关设置

"控制页面资源允许所有来源的主机可访问"
1.httpd-2.2
	<Directory " ">
		...
		Order allow,deny
		Allow from all
	</Directory>
2.httpd-2.4
	<Directory " ">
		...
		Require all granted
	</Directory>
"2.2测试"
httpd-2.2一般更改DocumentRoot即可生效
[root@grub6 ~]# mkdir -pv /data/www
mkdir: 已创建目录 "/data/www"
[root@grub6 ~]# vim /data/www/index.html
	<h1> Centos 6 test </h1>
[root@grub6 ~]# vim /etc/httpd/conf/httpd.conf "更改其DocumentRoot为/data/www即可"
[root@grub6 ~]# service httpd restart
"2.4测试"
httpd-2.4更改DocumentRoot后需要添加Directory才可进行访问
[root@sakura ~]# mkdir -p /data/www/html
[root@sakura ~]# vim /data/www/html/index.html
	<h1> Main Server Centos7 </h1>

[root@sakura ~]# vim /etc/httpd/conf/httpd.conf
	<Directory "/data/www/html">
		Options Indexes FollowSymLinks
		AllowOverride None
	 	Require all granted
	</Directory>
[root@sakura ~]# systemctl restart httpd

2)拒绝所有来源可访问相关设置

"控制页面资源拒绝所有来源的主机可访问"
1.httpd-2.2
	<Directory " ">
		...
		Order allow,deny
		Deny from all
	</Directory>
2.httpd-2.4
	<Directory " ">
		...
		Require all denied
	</Directory>
"2.2测试,此时/data/www下的资源所有主机均无法访问"
[root@grub6 ~]# vim /etc/httpd/conf/httpd.conf
	<Directory "/data/www">
        	Order allow,deny
        	Deny from all
	</Directory>
[root@grub6 ~]# service httpd restart
"2.4测试,此时/data/www/html下的资源所有主机均无法访问"
[root@sakura ~]# vim /etc/httpd/conf/httpd.conf 
	<Directory "/data/www/html">
		Options Indexes FollowSymLinks
		AllowOverride None
	 	Require all denied
	</Directory>
[root@sakura ~]# httpd -t
Syntax OK
[root@sakura ~]# systemctl restart httpd

3)httpd-2.2自定义可访问的主机

1.使用Allow from, Deny from即可定义
2.来源地址:以下四种方法均可实现
	1)172.16
	2)172.16.0.0
	3)172.16.0.0/16
	4)172.16.0.0/255.255.0.0
"测试,此时192.168.3.19主机可进行访问,其余主机无法访问"
[root@grub6 ~]# vim /etc/httpd/conf/httpd.conf
	<Directory "/data/www">
        Order allow,deny
        Allow from 192.168.3.19
	</Directory>
[root@grub6 ~]# httpd -t
Syntax OK
[root@grub6 ~]# service httpd reload
重新载入 httpd:

[root@sakura ~]# curl http://192.168.3.20/index.html  "192.168.3.100主机"
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>   "此时192.168.3.100主机访问会显示403报错"
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /index.html
on this server.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 192.168.3.20 Port 80</address>

ps:需要注意这里的Order allow,deny,此时若只指明allow,不指明deny,表示指明的主机运行访问,其余主机均不能访问;反之亦然

"指明网段中的某个主机无法访问,其余均可访问"
[root@grub6 ~]# vim /etc/httpd/conf/httpd.conf
	<Directory "/data/www">
        Order allow,deny
        Allow from 192.168.3
        Deny from 192.168.3.19
	</Directory>
[root@grub6 ~]# service httpd reload

[root@sakura ~]# curl http://192.168.3.20/index.html  "192.168.3.100主机"
<h1> Centos 6 test </h1>

4)httpd-2.4自定义可访问的主机

1.基于IP控制
	1)Require ip IP地址或网络地址
	2)Require not ip IP地址或网络地址
2.基于主机名控制
	1)Require host 主机名或域名
	2)Require not host 主机名或域名
3.配置内容需要放置于<RequireAll><RequireAny>配置快中

"测试,此时192.168.3.19主机无法进行访问,192.168.3.20主机访问正常"
[root@sakura ~]# vim /etc/httpd/conf/httpd.conf 
<Directory "/data/www/html">
     Options Indexes FollowSymLinks
     AllowOverride None
     <RequireAll>
         Require not ip 192.168.3.19
         Require ip 192.168.3
    </RequireAll>
 </Directory>
 [root@sakura ~]# httpd -t
	Syntax OK
[root@grub6 ~]# curl http://192.168.3.100  "192.168.3.20主机"
<h1> Main Server Centos7 </h1>

8.站点访问控制中的常用选项option:定义目录下的资源基于哪种特征被访问
1)Indexes:指明的URL路径下不存在index.html时,即不存在主页文件时,返回索引列表给用户;

[root@sakura ~]# mkdir /data/www/html/images
[root@sakura ~]# find /usr/share/ -iname "*.jpg" -exec cp {} /data/www/html/images/ \;
[root@sakura ~]# ls /data/www/html/images/
2560x1600.jpg   chess.jpg    dice.jpg                 flake.jpg   launch.jpg     night.jpg    sand-light.jpg  sunset.jpg
astronaut.jpg   coffee.jpg   energy-arc.jpg           flower.jpg  leaf.jpg       penguin.jpg  sky.jpg         yellow-rose.jpg
background.jpg  day.jpg      featured-builder-bg.jpg  grapes.jpg  lightning.jpg  profile.jpg  smiley.jpg
cat-eye.jpg     default.jpg  fish.jpg                 guitar.jpg  morning.jpg    puppy.jpg    sunflower.jpg
"此时若访问192.168.3.100/images则会显示images目录下图片的所有索引"

"进行如下设置后,访问images则不会显示所有图片索引"
[root@sakura ~]# vim /etc/httpd/conf/httpd.conf 
 <Directory "/data/www/html">
     Options  FollowSymLinks  "将Indexes删除"
     AllowOverride None
     Require all granted
 </Directory>
[root@sakura ~]# httpd -t
Syntax OK
[root@sakura ~]# systemctl reload httpd

ps:需要注意,若需要将一台主机的http服务当做下载站,则可以开启Indexes选项,否则一般则需要关闭此选项

索引效果图
在这里插入图片描述
2)FollowSymLinks:允许跟踪符号链接文件所指向的源文件

[root@sakura ~]# ln -sv /etc/fstab  /data/www/html/fstab.html
"/data/www/html/fstab.html" -> "/etc/fstab"
[root@sakura ~]# ll -h /data/www/html/
总用量 8.0K
lrwxrwxrwx. 1 root root   10 6月  24 20:48 fstab.html -> /etc/fstab
drwxr-xr-x. 2 root root 4.0K 6月  24 20:22 images
-rw-r--r--. 1 root root   31 6月  24 17:52 index.html
"此时可直接访问fstab.html文件"

"禁用FollowSymLinks,此时则无法使用符号链接文件"
[root@sakura ~]# vim /etc/httpd/conf/httpd.conf 
 <Directory "/data/www/html">
     Options  None
     AllowOverride None
     Require all granted
 </Directory>

效果图
在这里插入图片描述
3)None:不启用任何一个功能

4)ALL:All options except for MultiViews.(除了 MultiViews其余功能都启用)

9.定义站点主页面:DirectoryIndex;其种类为index.html,index.html.var

10.定义路径别名:Alias /URL/ “/PATH/TO/SOMEDIR/”

[root@sakura ~]# vim /etc/httpd/conf/httpd.conf 
Alias /images1/ "/usr/share/backgrounds/"   "定义别名"
 <Directory "/usr/share/backgrounds/">
     Options Indexes FollowSymLinks
     AllowOverride None
     Require all granted
 </Directory>
 ps:需要注意,httpd-2.4定义别名后,需要为别名设置Directory,否则别名目录无法访问

效果图
在这里插入图片描述
10.设定默认字符集(字符映射库):AddDefaultCharset UTF-8
ps:中文字符集,GBK, GB2312, GB18030

11.日志设定
1)日志类型:访问日志和错误日志

错误日志:仅记录错误及警告级别以上的信息,服务器程序启动和关闭过程的信息
1.ErrorLog  logs/error_log;其为相对路径,即相对ServerRoot "/etc/httpd"的路径
[root@sakura ~]# ll  /etc/httpd/
总用量 0
drwxr-xr-x. 2 root root  37 6月  24 22:03 conf
drwxr-xr-x. 2 root root 118 6月  24 21:05 conf.d
drwxr-xr-x. 2 root root 146 6月  23 13:46 conf.modules.d
lrwxrwxrwx. 1 root root  19 5月   7 00:42 logs -> ../../var/log/httpd
lrwxrwxrwx. 1 root root  29 5月   7 00:42 modules -> ../../usr/lib64/httpd/modules
lrwxrwxrwx. 1 root root  10 5月   7 00:42 run -> /run/httpd
访问日志:内建宏(httpd通常指字符串)
"日志相关格式,需要指定日志格式的名称,如combined,common,combinedio"
1.LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
2.LogFormat "%h %l %u %t \"%r\" %>s %b" common
3.LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
ps:还需使用CustomLog "logs/access_log" combined 指定日志路径及使用哪个名称的日志

"名称解析:例如192.168.3.19 - - [24/Jun/2019:22:21:58 +0800] "GET /images1/ HTTP/1.1" 200 2390 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36""
1.%h:客户端IP(若设置 HostnameLookups is set to On则会将IP解析为主机名,一般不使用)
2.%l:Remote logname (from identd, if supplied),基于identd登录,通常为一个减号("-")
3.%u:Remote user if the request was authenticated(远程认证登录),不为登录访问时,为一个减号
4.%t:服务器收到请求时的时间(+8000为东八区)
5.%r:First line of request,表示请求报文的首行;记录了此次请求的“方法”,“URL”以及协议版本
6.%>s:响应状态码(>表示记录最后一次响应状态码),一般2开头表示成功,4开头表示客户端错误,5开头表示服务端错误
7.%b:响应报文的大小,单位为字节;但不包括响应报文的http首部;若其大小为0记录为“-”,使用%B记录为0
8.%{Referer}i:请求报文中首部“referer”的值;即从哪个页面中的超链接跳转至当前页面的
9.%{User-Agent}i:请求报文中首部“User-Agent”的值;即为发出请求的应用程序(客户端浏览器类型)

Referer值
在这里插入图片描述
User-Agent类型
在这里插入图片描述
12.基于用户的访问控制

1.认证质询:
	WWW.Authenticate:响应码为401,拒绝客户端请求,并说明要求客户端提供账号和密码
2.认证:
	1)Authorization:客户端用户填入账号和密码后再次发送请求报文;认证通过时,则服务器发送相应的资源
	2)认证方式:basic(明文);digest(消息摘要认证,一般使用协议认证或表单认证)
3.安全域:需要用户认证后方能访问的路径;应该通过名称对其进行标识,以便于告知用户认证的原因
4.用户的账号和密码存放位置
	1)虚拟账号:此账号仅用于访问某服务时用到的认证标识,与系统用户无关
	2)存储:文本文件(一般用户量较小时使用);SQL数据库(一般用户量较大时使用,但需要使用特定的程序httpd才可以进行访问数据库);ldap目录存储

1)basic认证配置测试

"提供账号和密码存储(文本文件):使用专用命令完成此类文件的创建及用户管理"
1.htpasswd命令
	1)语法:htpasswd  [options]   /PATH/TO/HTTPD_PASSWD_FILE  username
	2)-c:自动创建此处指定的文件,一般仅在此文件不存在时使用
	3)-m:MD5格式加密
	4)-s:sha格式加密
	5)-D:删除指定用户
	6)-b:批模式添加用户
[root@sakura ~]# htpasswd -c /etc/httpd/conf.d/test.users kasumi
New password: 
Re-type new password: 
Adding password for user kasumi
[root@sakura ~]# htpasswd  /etc/httpd/conf.d/test.users yuki
New password: 
Re-type new password: 
Adding password for user yuki
[root@sakura ~]# htpasswd  -b /etc/httpd/conf.d/test.users sakura 516221    "使用-b选项添加账户"
Adding password for user sakura
[root@sakura ~]# cat /etc/httpd/conf.d/test.users 
kasumi:$apr1$U7DBCSSR$nfJvwKXT3xHyKPwUi803s1
yuki:$apr1$TkWF/1C3$0VrTvqJ7VgrKvkgwZD4Hx1
sakura:$apr1$WFdNfhbf$pPHwwbGht9sYmm96K1foA0
"定义安全域"
<Directory "">
	Options None
	AllowOverride None
	AuthType Basic  认证类型
	AuthName "String" 提示信息
	AuthUserFile  "/PATH/TO/HTTPD_USER_PASSWD_FILE" "用户文件位置"
	Require  user  username1  username2 ...
</Directory>
						
ps:允许账号文件中的所有用户登录访问使用Require  valid-user
"测试,此时只有认证用户之后才可以访问相关资源"
[root@sakura ~]# mkdir /data/www/html/basic
[root@sakura ~]# vim /data/www/html/basic/index.html
	<h1> Basic test page </h1>
[root@sakura ~]# vim /etc/httpd/conf.d/basictest.conf
<Directory "/data/www/html/basic">
	Options None
	AllowOverride None
	AuthType Basic
	AuthName "this is private page,please input user"
	AuthUserFile "/etc/httpd/conf.d/test.users"
	Require user kasumi yuki
</Directory>
[root@sakura ~]# httpd -t
Syntax OK
[root@sakura ~]# systemctl restart httpd

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

"基于组账号进行认证"
<Directory "">
	Options None
	AllowOverride None
	AuthType Basic  认证类型
	AuthName "String" 提示信息
	AuthUserFile  "/PATH/TO/HTTPD_USER_PASSWD_FILE" 
	AuthGroupFile "/PATH/TO/HTTPD_GROUP_FILE"
	Require  group  grpname1  grpname2 ...
</Directory>
"创建用户账号和组账号文件"
1.组文件:每一行定义一个组
	GRP_NAME: username1  username2  ...
"测试,此时仅组内成员可以通过认证进行访问"
[root@sakura ~]# vim /etc/httpd/conf.d/group
	grouptest: kasumi yuki
[root@sakura ~]# vim /etc/httpd/conf.d/basictest.conf 
<Directory "/data/www/html/basic">
	Options None
	AllowOverride None
	AuthType Basic
	AuthName "this is private page,please input user"
	AuthUserFile "/etc/httpd/conf.d/test.users"
	AuthGroupFile "/etc/httpd/conf.d/group"
	Require group grouptest
</Directory>

13.虚拟主机

"站点标识:socket"
1.IP相同,但端口不同
2.IP不同,但端口均为默认端口
3.FQDN不同:使用请求报文中的首部,Host:www.kasumi.com

"三种实现方案"
1.基于IP:为每个虚拟主机准备至少一个IP地址
2.基于port:为每个虚拟主机使用至少一个独立的port
3.基于FQDN:为每个虚拟主机使用至少一个FQDN
ps:需要注意,对于httpd-2.2,一般虚拟机不能与中心主机混用;因此,要使用虚拟主机,得先禁用'main'主机;其禁用方法为,注释掉中心主机的DocumentRoot指令即可

"虚拟主机的配置方法"
<VirtualHost  IP:PORT>
	ServerName FQDN
	DocumentRoot  ""
</VirtualHost>
					
其它可用指令:
ServerAlias:虚拟主机的别名;可多次使用;
ErrorLog:错误日志
CustomLog:指定日志格式
<Directory "">
	...
</Directory>
Alias
...
"基于IP进行测试,此时不同ip即可访问不同页面"
[root@sakura ~]# mkdir /data/www/html/{ilinux,iunix}
[root@sakura ~]# ls /data/www/html/
basic  fstab.html  ilinux  images1  index.html  iunix
[root@sakura ~]# vim /data/www/html/ilinux/index.html
	<h1> This is ilinux test page </h1>
	<h2> Welcome </h1>
[root@sakura ~]# vim /data/www/html/iunix/index.html
	<h1> This is iunix test page </h1>
[root@sakura ~]# ip addr add 192.168.3.101/24 dev ens33
[root@sakura ~]# ip addr list | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.3.100/24 brd 192.168.3.255 scope global ens33
    inet 192.168.3.101/24 scope global secondary ens33
[root@sakura ~]# vim /etc/httpd/conf.d/ilinux.conf
<VirtualHost 192.168.3.100:80>
	Servername www.ilinux.io
	DocumentRoot "/data/www/html/ilinux"
	<Directory "/data/www/html/ilinux">
		Options None
		AllowOverride None
		Require all granted
	</Directory>
	CustomLog "logs/ilinux_acess_log" combined
</VirtualHost>
[root@sakura ~]# vim /etc/httpd/conf.d/iunix.conf
<VirtualHost 192.168.3.101:80>
	Servername www.iunix.io
	DocumentRoot "/data/www/html/iunix"
	<Directory "/data/www/html/iunix">
		Options None
		AllowOverride None
		Require all granted
	</Directory>
	CustomLog "logs/iunix_acess_log" combined
</VirtualHost>
[root@sakura ~]# httpd -t
Syntax OK
[root@sakura ~]# systemctl restart httpd

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

"基于port进行测试,此时即可以使用不同端口进行访问"
[root@sakura ~]# ip addr delet 192.168.3.101/24 dev ens33
[root@sakura ~]# ip a l | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    inet 192.168.3.100/24 brd 192.168.3.255 scope global ens33
[root@sakura ~]# vim /etc/httpd/conf.d/iunix.conf  "此时仅需更改iunix配置文件"
Listen 5166
<VirtualHost 192.168.3.100:5166>
    Servername www.iunix.io
    DocumentRoot "/data/www/html/iunix"
    <Directory "/data/www/html/iunix">
        Options None
        AllowOverride None
        Require all granted
    </Directory>
    CustomLog "logs/iunix_acess_log" combined
</VirtualHost>
[root@sakura ~]# vim /etc/httpd/conf.d/iunix.conf 
[root@sakura ~]# httpd -t
Syntax OK
[root@sakura ~]# systemctl restart httpd

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

"基于FQDN进行测试,"
1.更改虚拟主机配置文件
[root@sakura ~]# vim /etc/httpd/conf.d/ilinux.conf 
<VirtualHost *:80>  "更改为*:80,表示任意主机都能通过80端口进行访问"
	Servername www.ilinux.io
	DocumentRoot "/data/www/html/ilinux"
	<Directory "/data/www/html/ilinux">
		Options None
		AllowOverride None
		Require all granted
	</Directory>
	CustomLog "logs/ilinux_acess_log" combined
</VirtualHost>
[root@sakura ~]# cat /etc/httpd/conf.d/iunix.conf 
<VirtualHost *:80>
	Servername www.iunix.io
	DocumentRoot "/data/www/html/iunix"
	<Directory "/data/www/html/iunix">
		Options None
		AllowOverride None
		Require all granted
	</Directory>
	CustomLog "logs/iunix_acess_log" combined
</VirtualHost>
ps:此时使用IP仅能访问一个页面
2.添加DNS服务
[root@sakura ~]# vim /etc/named.rfc1912.zones 
zone "ilinux.io" IN {
    type master;
    file "ilinux.io.zone";
    allow-update { none; };
};
zone "iunix.io" IN {
    type master;
    file "iunix.io.zone";
    allow-update { none; };
};
[root@sakura ~]# vim /var/named/ilinux.io.zone 
$TTL 3600
$ORIGIN ilinux.io.
@ IN SOA ns1.ilinux.io. admin.ilinux.io. (
        2019062501
        1H
        10M
        1D
        2D )
        IN NS ns1
 IN NS ns1
ns1 IN A 192.168.3.100
www IN A 192.168.3.100
[root@sakura ~]# vim /var/named/iunix.io.zone 
$TTL 3600
$ORIGIN iunix.io.
@ IN SOA ns1.iunix.io. admin.iunix.io. (
        2019062501
        1H  
        10M 
        1D  
        2D )
        IN NS ns1 
 IN NS ns1 
ns1 IN A 192.168.3.100
www IN A 192.168.3.100
[root@sakura ~]# named-checkzone ilinux.io /var/named/ilinux.io.zone 
zone ilinux.io/IN: loaded serial 2019062501
OK
[root@sakura ~]# named-checkzone iunix.io /var/named/iunix.io.zone
zone iunix.io/IN: loaded serial 2019062501
OK
[root@sakura ~]# named-checkconf 
[root@sakura ~]# systemctl restart named
ps:需要注意,配置好DNS服务器后,需要设定对应主机的DNS服务器为配置过的服务器才可进行访问;并且server-status页面也可设置认证访问
<Location /server-status>
    SetHandler server-status
    AuthType basic
    AuthName "test page"
    AuthUserFile "/etc/httpd/conf.d/test.users"
    Require user kasumi yuki
</Location>

效果图
在这里插入图片描述
在这里插入图片描述
14.status页面

首先需要确认是否已加载:status_module模块,若未加载需要加载模块;
LoadModule  status_module  modules/mod_status.so
[root@sakura ~]# httpd -M | grep status
 status_module (shared)
 [root@sakura ~]# vim /etc/httpd/conf.d/status.conf
<Location /server-status>
    SetHandler server-status
    <RequireAll>
        require ip 192.168.3
    </RequireAll>
</Location>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值