关于MPM机制(多路处理模块)
1)prefork:多进程模型,一个进程响应一个请求
2)worker:多线程模型(多进程生成,一个进程生成多个线程),一个线程响应一个请求
3)event:事件驱动模型,一个线程响应多个请求
并发服务器响应请求,包括:单进程IO模型,多进程IO模型,复用的IO模型,复用的多进程IO模型。
关于centos 6的知识
主配置文件
/etc/httpd/conf/httpd.conf
/etc/httpd/一般是web服务的根目录
服务脚本
/etc/rc.d/init.d/httpd是(centos 6)
/etc/sysconfig/httpd是脚本的配置文件
[root@lab1 ~]# rpm -qc httpd
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
/etc/httpd/conf.modules.d/00-base.conf
/etc/httpd/conf.modules.d/00-dav.conf
/etc/httpd/conf.modules.d/00-lua.conf
/etc/httpd/conf.modules.d/00-mpm.conf
/etc/httpd/conf.modules.d/00-proxy.conf
/etc/httpd/conf.modules.d/00-systemd.conf
/etc/httpd/conf.modules.d/01-cgi.conf
/etc/httpd/conf/httpd.conf
/etc/httpd/conf/magic
/etc/logrotate.d/httpd
/etc/sysconfig/htcacheclean
/etc/sysconfig/httpd
主程序文件,包括:
1) /usr/sbin/httpd (prefork模式)
2) /usr/sbin/httpd.event (event 模式)
3) /usr/sbin/httpd.worker (worker 模式)
日志文件目录:
/var/log/httpd
access_log:访问日志
error_log:错误日志
站点文档目录
/var/www/html
模块文件路径
/usr/lib64/httpd/modules
配置文件的组成
grep "Section" /etc/httpd/conf/httpd.conf
### Section 1: Global Environment
### Section 2: 'Main' server configuration
### Section 3: Virtual Hosts
常用配置:
1)修改监听的IP和端口
省略IP表示监听本机所有IP,Listen可重复多次出现
添加端口的实例:
[root@lab1 ~]# ss -tnl | grep 80
LISTEN 0 128 :::80 :::*
[root@lab1 ~]# grep ^Listen /etc/httpd/conf/httpd.conf
Listen 80
Listen 172.20.0.131:8080
[root@lab1 ~]# ss -tnl | grep 80
LISTEN 0 128 172.20.0.131:8080 *:*
LISTEN 0 128 :::80 :::*
2)持久连接
连接建立,每个资源获取完成后不会断开连接,而是连续等待其他的请求完成
实现断开的机制
数量限制
时间限制
断开机制的影响:对并发访问量很大的服务器,持久连接功能会使得有些请求得不到响应
改进机制:使用较短的持久连接时间,httpd2.4支持毫秒级持久时间
非持久连接
KeepAlive On|Off
MaxKeepAliveRequests #
KeepAliveTimeout #
使用telnet测试的实例:
[root@lab2 ~]# telnet 172.20.0.131 80
Trying 172.20.0.131...
Connected to 172.20.0.131.
Escape character is '^]'.
GET / HTTP/1.1
Host: 172.20.0.131
HTTP/1.1 403 Forbidden
Date: Fri, 03 May 2019 14:21:57 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
ETag: "1321-5058a1e728280"
Accept-Ranges: bytes
Content-Length: 4897
Content-Type: text/html; charset=UTF-8
...
Connection closed by foreign host. (注意:此处可以看出是短链接模式)
3)MPM 多路处理模块
httpd2.2不支持同时编译多个模块,只能编译时选择一个:rpm安装的包提供了三个二进制程序文件,分别用于实现对不同MPM机制的支持,确认方法
[root@lab1 conf]# ps aux | grep httpd
root 1404 0.0 0.2 222008 5048 ? Ss 10:01 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1432 0.0 0.1 222008 3184 ? S 10:13 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1433 0.0 0.1 222008 2948 ? S 10:13 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1434 0.0 0.1 222144 3692 ? S 10:13 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1435 0.0 0.1 222008 2948 ? S 10:13 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1436 0.0 0.1 222008 2948 ? S 10:13 0:00 /usr/sbin/httpd -DFOREGROUND
apache 1500 0.0 0.1 222008 2944 ? S 10:20 0:00 /usr/sbin/httpd -DFOREGROUND
root 1529 0.0 0.0 112708 980 pts/0 S+ 10:29 0:00 grep --color=auto httpd
默认为/usr/sbin/httpd,其使用prefork
查看模块列表
静态编译的模块
[root@lab1 conf]# httpd -l
Compiled in modules:
core.c
mod_so.c
http_core.c
静态编译及动态装载的模块
[root@lab1 conf]# httpd -M
Loaded Modules:
更新使用的httpd程序
修改/etc/sysconfig/httpd,重启服务
prefork的配置
<IfModule prefork.c>
StartServers
MinSpareServers
MaxSpareServers
ServerLimit
MaxClients
MaxRequestsPerChild
</IfModule>
worker的配置
<IfModule worker.c>
StartServers
MinSpareThreads
MaxSpareThreads
ThreadsPeChild
MaxClients
MaxRequestsPerChild
</IfModule>
4)DSO 动态共享对象
配置指令实现模块加载
模块路径可以使用相对路径, 相对于ServerRoot(/etc/httpd)指向的路径而言
5)定义'Main' server主服务器的文档页面路径
文档路径映射:DocumentRoot指向的路径为URL路径的起始位置
[root@lab1 conf]# mkdir /www/htdocs -pv
mkdir: created directory ‘/www/htdocs’
[root@lab1 conf]# vim /www/htdocs/index.html
[root@lab1 conf]# cat /www/htdocs/index.html
Hello World!
[root@lab1 conf]# vim httpd.conf
[root@lab1 conf]# grep ^DocumentRoot httpd.conf
DocumentRoot "/www/htdocs"
6)站点访问控制
可基于两种类型的路径指明对资源进行访问控制
文件系统路径
<Directory ></Directory>
<File ></File>
<FileMatch ></FileMatch>
URL路径
<Location ></Location>
访问控制机制
基于来源地址
基于用户账号密码
7)基于来源地址的访问控制
Options选项:
Indexes 索引(比较危险的功能),如果索引不到,默认配置是跳转到欢迎页面(/etc/httpd/conf.d/welcome.conf)
FollowSymlinks: 允许跟踪符号链接文件
基于来源地址的访问控制机制
Order:检查次序(排在后面的是默认机制)
Order allow,deny
Order deny,allow
Allow from
Deny from
8)定义默认主页面
DirectoryIndex index.html index.html.var