關於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