** 1 概述**
HTTPD 2.4默認是用于centOS7上,該軟件相比于HTTPD2.2 有了功能的改進。本文將對httpd2.4的新功能進行介紹,并介紹相關的常見配置
由于格式問題,本文的相關代碼見文章 http://ghbsunny.blog.51cto.com/7759574/1970553
2 HTTPD 新特性
.(1) MPM支持運行為DSO機制;以模塊形式按需加載,DSO:動態的共享模塊
.(2) event MPM生產環境可用
.(3) 異步讀寫機制
.(4) 支持每模塊及每目錄的單獨日志級別定義,centos6上是每個虛擬主機的日志記錄,不支持目錄的日志記錄
.(5) 每請求相關的專用配置
.(6) 增強版的表達式分析式
.(7) 毫秒級持久連接時長定義
.(8) 基于FQDN的虛擬主機不需要NameVirutalHost指令
.(9) 新指令,AllowOverrideList
.(10) 支持用戶自定義變量
.(11) 更低的內存消耗
3 配置機制變更
HTTPD2.4不再支持使用Order, Deny, Allow來做基于IP的訪問控制
.新模塊
.(1) mod_proxy_fcgi
FastCGI Protocol backend formod_proxy
.(2) mod_remoteip
Replaces the apparent client remoteIP address and hostname for the request with the IP address list presented by aproxies or a load balancer via the request headers.
.(3) mod_ratelimit
Provides Bandwidth Rate Limiting forClients
CentOS 7 httpd程序環境
.CentOS 7:httpd-2.4
.安裝方法
rpm,編譯安裝
.Rpm安裝程序環境:
.配置文件:
/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/.conf
.模塊相關的配置文件:
/etc/httpd/conf.modules.d/.conf
.systemdunit file:
/usr/lib/systemd/system/httpd.service
.主程序文件:
/usr/sbin/httpd
httpd-2.4支持MPM的動態切換
CentOS 7 httpd 程序環境
.日志文件:
/var/log/httpd
access_log:訪問日志
error_log:錯誤日志
.站點文檔:
/var/www/html
.模塊文件路徑:
/usr/lib64/httpd/modules
.服務控制:
systemctlenable|disablehttpd.servicesystemctl{start|stop|restart|status}httpd.service
4 常見配置
.(1) 切換使用的MPM
.Centos 7:/etc/httpd/conf.modules.d/00-mpm.conf
在00-mpm.conf這個文件下啟用對應MPM相關的LoadModule指令即可
如果啟動的命令是apachectl,那么關閉的時候也要用apachectl來控制,而不能用systemctl來控制,這個是配套的
如果更換模塊為worker或者event后重啟失敗,可能是/etc/httpd/conf.d/php.conf的值設置有問題,把以下兩行注釋,再重啟即可
php_value session.save_handler "files"#php_valuesession.save_path "/var/lib/php/session"
.centos6編譯安裝:
vim /etc/httpd24/httpd.confInclude /etc/httpd24/extra/httpd-mpm.confLoadModule mpm_event_module modules/mod_mpm_event.so
(2)切換主目錄:
DocumentRoot /path
這個和centOS6不一樣,先改配置文件,把DocumentRoot更改,然后要設置directory進行授權。cent7上默認創建的目錄是不允許訪問的。所以要進行授權。
vim /etc/httpd/conf/httpd.confDocumentRoot /app
授權如下
vim /etc/httpd/conf.d/newhome.conf<directory /app>require all granted</directory>
(3) 基于IP的訪問控制
無明確授權的目錄,默認拒絕
允許所有主機訪問:Require all granted
拒絕所有主機訪問:Require all denied
控制特定的IP訪問:
Require ip IP ADDR:授權指定來源的IP訪問
Require not ip IP ADDR:拒絕特定的IP訪問
控制特定的主機訪問:
Require host HOSTNAME:授權特定主機訪問
Require not host HOSTNAME:拒絕
HOSTNAME:
FQDN:特定主機
domin.tld:指定域名下的所有主機
.不能有失敗,至少有一個成功匹配
<RequireAll>……</RequireAll>
以下配置除了172.18.50.63這個ip不能訪問,其他所有的主機都能訪問
vim /etc/httpd/conf.d/newhome.conf<directory /app><requireall>require all grantedrequire not ip 172.18.50.63</requireall></directory>
.多個語句有一個成功,即成功
<RequireAny>……</RequireAny>
以下配置除了172.18.50.63這個ip能訪問,其他所有的主機都不能訪問
vim /etc/httpd/conf.d/newhome.conf<directory /app><requireany>require all deniedrequire ip 172.18.50.63</requireany></directory>
(4) 虛擬主機
基于FQDN的虛擬主機也不再需要NameVirutalHost指令,但是centos6(2.2)要配置namevirtualhost:80,2.4不需要配置這行代碼
例子一
<VirtualHost:80>ServerName www.b.netDocumentRoot "/apps/b.net/htdocs" <Directory"/apps/b.net/htdocs">Options NoneAllowOverride NoneRequire all granted</Directory></VirtualHost>
例子二
首先要建立三個主站點a.com,b.com,c.com,虛擬主機建立完成后,默認的主站點會失效,主站點會變更為配置文件中的第一個虛擬主機的站點,注意:任意目錄下的頁面只有顯式授權才能被訪問
虛擬主機配置文件如下
vim /etc/httpd/conf.d/virtualhost.conf<directory /app>require all granted</directory><virtualhost *:80>servername www.a.comdocumentroot "/app/a.com/"</virtualhost><virtualhost *:80>servername www.b.comdocumentroot "/app/b.com/"</virtualhost><virtualhost *:80>servername www.c.comdocumentroot "/app/c.com/"</virtualhost>
測試的主機指定的dns要能解析這三個站點,或者可以直接添加在測試主機的hosts進行測試
(5) 開啟https
安裝mod_ssl,和httpd-2.2相同配置
(6) 長連接
KeepAlive on #默認支持持久連接KeepAlive Timeout #msMaxKeepAlive Requests 100
毫秒級持久連接時長定義
** (7) Sendfile機制**
提高訪問性能的設置,默認啟用,配置語句
EnableSendfile on
Sendfile機制的相關原理如下
.不用sendfile的傳統網絡傳輸過程:
.read(file, tmp_buf, len)
.write(socket, tmp_buf, len)
實現機制如下:
.硬盤>>kernel buffer >> user buffer >> kernel socket buffer >> 協議棧
資源是從硬盤復制到內核緩沖區,然后在復制到httpd的應用程序緩沖區,然后應用程序封裝響應頭部,再交給內核緩存,造成了效率低下
.一般網絡應用通過讀硬盤數據,寫數據到socket 來完成網絡傳輸,底層執行過程:
.1系統調用read()產生一個上下文切換:從user mode 切換到kernel mode,然后DMA 執行拷貝,把文件數據從硬盤讀到一個kernel buffer 里。
.2 數據從kernelbuffer 拷貝到userbuffer,然后系統調用read() 返回,這時又產生一個上下文切換:從kernel mode 切換到user mode
.3 系統調用write()產生一個上下文切換:從user mode 切換到kernel mode,然后把步驟2讀到user buffer 的數據拷貝到kernel buffer(數據第2次拷貝到kernel buffer),不過這次是個不同的kernel buffer,這個buffer和socket 相關聯。
.4 系統調用write()返回,產生一個上下文切換:從kernel mode 切換到user mode(第4次切換),然后DMA從kernel buffer 拷貝數據到協議棧(第4次拷貝)
.上面4個步驟有4次上下文切換,有4次拷貝,如果能減少切換次數和拷貝次數將會有效提升性能
.在kernel2.0+ 版本中,系統調用sendfile()就是用來簡化上面步驟提升性能的。sendfile() 不但能減少切換次數而且還能減少拷貝次數
.用sendfile()來進行網絡傳輸的過程:
.sendfile(socket, file, len);
.硬盤>>kernel buffer (快速拷貝到kernelsocket buffer) >> 協議棧
.1系統調用sendfile()通過DMA 把硬盤數據拷貝到kernel buffer,然后數據被kernel 直接拷貝到另外一個與socket 相關的kernel buffer。這里沒有user mode 和kernel mode 之間的切換,在kernel 中直接完成了從一個buffer 到另一個buffer 的拷貝。
.2DMA 把數據從kernelbuffer 直接拷貝給協議棧,沒有切換,也不需要數據從usermode 拷貝到kernelmode,因為數據就在kernel 里
5 反向代理功能
2.4默認支持反向代理,反向代理相當于是代替客戶端訪問后端服務器,httpd可以實現,但是httpd不能實現大規模的調度作用,所以一般不用來實現反向代理, 一般前端用haproxy這種專業的反向代理服務器,本身沒有web功能,就是用來做代理用的。
nginx也可以做代理服務器,郵件服務器,網頁服務器,但是,nginx的功能比haproxy弱,但是企業還是比較多用nginx來做代理服務器,因為nginx的代理服務器功能基本能滿足轉發,而且可以當網頁服務器,本身可以響應web請求。
.啟用反向代理
ProxyPass "/" "http://www.example.com/"# ProxyPass 指請求從客戶端到反向代理服務器#"/" 這里代表URL,這里是主站點的根#"http://www.example.com/" 是后端的服務器ProxyPassReverse "/" "http://www.example.com/"# ProxyPassReverse是指后端服務器"http://www.example.com/"將結果返回到反向代理的服務器上
以上兩個語句是配套出現
例子
例子一
.特定URL反向代理
在172.18.50.75這臺機器上配置,如果訪問http://172.18.50.75 會被轉發到http://172.18.50.61/,如果訪問http://172.18.50.75/bbs,就會被轉發到http://172.18.50.63/sunny.txt
vim/etc/httpd/conf.d/virtualhost.conf
<directory/app>
requireall granted
</directory>
<virtualhost:80>
servername www.a.comdocumentroot"/app/a.com/"proxypass"/" "http://172.18.50.61/"proxypassreverse"/" "http://172.18.50.61"</virtualhost><virtualhost:80>
servernamewww.b.com
documentroot"/app/b.com/"proxypass"/bbs" "http://172.18.50.63/sunny.txt"proxypassreverse"/bbs" "http://172.18.50.63/sunny.txt"</virtualhost>
測試,在能夠解析http://172.18.50.75的主機上curl http://172.18.50.75/bbs 和curl http://172.18.50.75 看到結果,如果有重定向,加-L選項,會將請求再發一次,發到重定向后的地址
例子二
實現基于端口的虛擬主機
vim /etc/httpd/conf.d/virtualhost.conf
<directory/app>requireall granted
</directory>
listen8080
<virtualhost*:8080>
servernamewww.c.com
documentroot"/app/c.com/"
</virtualhost>
測試,在其他主機上curl http://172.18.50.75:8080
6 總結
本文主要是介紹了httpd的一些新特性和常見的幾種配置,其他內容可以參見博客http://ghbsunny.blog.51cto.com/7759574/1970486。