引子
先來(lái)分析一個(gè)Yslow 測(cè)試的一個(gè)頁(yè)面的前端性能。
這里所有的請(qǐng)求是指http請(qǐng)求,對(duì)于一個(gè)請(qǐng)求各個(gè)階段的劃分,阻擋->域名解析->建立連接->發(fā)送請(qǐng)求->等待響應(yīng)->接收數(shù)據(jù)。當(dāng)然不是每個(gè)請(qǐng)求都要包含所有過(guò)程。
在以上測(cè)試中,沒(méi)有涉及到請(qǐng)求下載資源過(guò)程中還有一個(gè)部分:TCP請(qǐng)求的鏈接與斷開(kāi),而這篇文章正式說(shuō)這個(gè)請(qǐng)求的。
那么http請(qǐng)求和tcp請(qǐng)求是什么關(guān)系呢?簡(jiǎn)單點(diǎn)說(shuō)就是一個(gè)tcp請(qǐng)求是比較靠近底層的,在它上面是http之類的應(yīng)用請(qǐng)求,所以可以認(rèn)為一個(gè)tcp請(qǐng)求包括很多個(gè)http請(qǐng)求(至于包括多少,apache中可以設(shè)定),同時(shí)tcp的鏈接與斷開(kāi)比http請(qǐng)求的鏈接和斷開(kāi)更需要消耗掉更多的內(nèi)存資源和時(shí)間。
KeepAlive的含義
KeepAlive配置的含義:對(duì)于HTTP/1.1的客戶端來(lái)說(shuō),將會(huì)盡量的保持客戶的HTTP連接,通過(guò)一個(gè)連接傳送多份HTTP請(qǐng)求響應(yīng)。這樣對(duì)于客戶端來(lái)說(shuō),可以提高50%左右的響應(yīng)時(shí)間,而于服務(wù)器端來(lái)說(shuō)則降低了更多個(gè)連接的開(kāi)銷。不過(guò)這個(gè)依賴于客戶端是否想保持連接。IE默認(rèn)是保持連接的,當(dāng)你打開(kāi)100個(gè)圖片的網(wǎng)站時(shí),IE有可能只打開(kāi)2個(gè)連接,通過(guò)這兩個(gè)連接傳送數(shù)據(jù),而不是開(kāi)100個(gè)連接。
在 Apache 服務(wù)器中,KeepAlive 是一個(gè)布爾值,On 代表打開(kāi),Off 代表關(guān)閉,這個(gè)指令在其他眾多的 HTTPD 服務(wù)器中都是存在的。
KeepAliveTimeout 為持久連接保持的時(shí)間,也就是說(shuō),在這此連接結(jié)束后開(kāi)始計(jì)時(shí),多長(zhǎng)時(shí)間內(nèi)沒(méi)有重新發(fā)送HTTP請(qǐng)求,就斷掉連接。默認(rèn)設(shè)置為5秒,這個(gè)值可以大點(diǎn),但不能太大,否則會(huì)出現(xiàn)同時(shí)等候過(guò)多連接,導(dǎo)致多的內(nèi)存被占用。
KeepAlive的作用
如何謀求網(wǎng)絡(luò)帶寬與服務(wù)器資源之間的平衡。這個(gè)要根據(jù)具體情況,具體分析。
那么我們考慮3種情況:
1。用戶瀏覽一個(gè)網(wǎng)頁(yè)時(shí),除了網(wǎng)頁(yè)本身外,還引用了多個(gè) javascript 文件,多個(gè) css 文件,多個(gè)圖片文件,并且這些文件都在同一個(gè) HTTP 服務(wù)器上。
2。用戶瀏覽一個(gè)網(wǎng)頁(yè)時(shí),除了網(wǎng)頁(yè)本身外,還引用一個(gè) javascript 文件,一個(gè)圖片文件。
3。用戶瀏覽的是一個(gè)動(dòng)態(tài)網(wǎng)頁(yè),由程序即時(shí)生成內(nèi)容,并且不引用其他內(nèi)容。
對(duì)于上面3中情況,我認(rèn)為:1 最適合打開(kāi) KeepAlive ,2 隨意,3 最適合關(guān)閉 KeepAlive
在 Apache 中,打開(kāi)和關(guān)閉 KeepAlive 功能,服務(wù)器端會(huì)有什么異同呢? 下面看理論分析。
打開(kāi) KeepAlive 后,意味著每次用戶完成全部訪問(wèn)后,都要保持一定時(shí)間后才關(guān)閉會(huì)關(guān)閉 TCP 連接,那么在關(guān)閉連接之前,必然會(huì)有一個(gè)Apache 進(jìn)程對(duì)應(yīng)于該用戶而不能處理其他用戶,假設(shè) KeepAlive 的超時(shí)時(shí)間為 10 秒種,服務(wù)器每秒處理 50個(gè)獨(dú)立用戶訪問(wèn),那么系統(tǒng)中 Apache 的總進(jìn)程數(shù)就是 10 * 50 = 500 個(gè),如果一個(gè)進(jìn)程占用 4M 內(nèi)存,那么總共會(huì)消耗 2G內(nèi)存,所以可以看出,在這種配置中,相當(dāng)消耗內(nèi)存,但好處是系統(tǒng)只處理了 50次 TCP 的握手和關(guān)閉操作。
如果關(guān)閉 KeepAlive,如果還是每秒50個(gè)用戶訪問(wèn),如果用戶每次連續(xù)的請(qǐng)求數(shù)為3個(gè),那么 Apache 的總進(jìn)程數(shù)就是 50 * 3= 150 個(gè),如果還是每個(gè)進(jìn)程占用 4M 內(nèi)存,那么總的內(nèi)存消耗為 600M,這種配置能節(jié)省大量?jī)?nèi)存,但是,系統(tǒng)處理了 150 次 TCP的握手和關(guān)閉的操作,因此又會(huì)多消耗一些 CPU 資源。
在看看實(shí)踐的觀察。
在一組大量處理動(dòng)態(tài)網(wǎng)頁(yè)內(nèi)容的服務(wù)器中,起初打開(kāi) KeepAlive功能,經(jīng)常觀察到用戶訪問(wèn)量大時(shí)Apache進(jìn)程數(shù)也非常多,系統(tǒng)頻繁使用交換內(nèi)存,系統(tǒng)不穩(wěn)定,有時(shí)負(fù)載會(huì)出現(xiàn)較大波動(dòng)。關(guān)閉了 KeepAlive功能后,看到明顯的變化是: Apache 的進(jìn)程數(shù)減少了,空閑內(nèi)存增加了,用于文件系統(tǒng)Cache的內(nèi)存也增加了,CPU的開(kāi)銷增加了,但是服務(wù)更穩(wěn)定了,系統(tǒng)負(fù)載也比較穩(wěn)定,很少有負(fù)載大范圍波動(dòng)的情況,負(fù)載有一定程度的降低;變化不明顯的是:訪問(wèn)量較少的時(shí)候,系統(tǒng)平均負(fù)載沒(méi)有明顯變化。
總結(jié)一下:
在內(nèi)存非常充足的服務(wù)器上,不管是否關(guān)閉 KeepAlive 功能,服務(wù)器性能不會(huì)有明顯變化;
如果服務(wù)器內(nèi)存較少,或者服務(wù)器有非常大量的文件系統(tǒng)訪問(wèn)時(shí),或者主要處理動(dòng)態(tài)網(wǎng)頁(yè)服務(wù),關(guān)閉 KeepAlive 后可以節(jié)省很多內(nèi)存,而節(jié)省出來(lái)的內(nèi)存用于文件系統(tǒng)Cache,可以提高文件系統(tǒng)訪問(wèn)的性能,并且系統(tǒng)會(huì)更加穩(wěn)定。
KeepAlive配置文件
如果設(shè)置KeepAlive ,找到這個(gè)設(shè)置的文件頗為費(fèi)時(shí),以前版本的大多配置都在httpd.conf文件,現(xiàn)在版本(2.4.2)的apache把不少配置都分離到不同的文件中了,于是,我只好一個(gè)文件一個(gè)文件的搜索。
...apache/conf/extra/httpd-default.conf
#
# This configuration file reflects default settings for Apache HTTP Server.
#
# You may change these, but chances are that you may not need to.
##
# Timeout: The number of seconds before receives and sends time out.
#
Timeout 60
#
# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.
#
KeepAlive On
#
# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.
#
MaxKeepAliveRequests 100
#
# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.
#
KeepAliveTimeout 5
## UseCanonicalName: Determines how Apache constructs self-referencing
# URLs and the SERVER_NAME and SERVER_PORT variables.
# When set "Off", Apache will use the Hostname and Port supplied
# by the client. When set "On", Apache will use the value of the
# ServerName directive.
#
UseCanonicalName Off
#
# AccessFileName: The name of the file to look for in each directory
# for additional configuration directives. See also the AllowOverride
# directive.
#
AccessFileName .htaccess
#
# ServerTokens
# This directive configures what you return as the Server HTTP response
# Header. The default is 'Full' which sends information about the OS-Type
# and compiled in modules.
# Set to one of: Full | OS | Minor | Minimal | Major | Prod
# where Full conveys the most information, and Prod the least.
#
ServerTokens Full
#
# Optionally add a line containing the server version and virtual host
# name to server-generated pages (internal error documents, FTP directory
# listings, mod_status and mod_info output etc., but not CGI generated
# documents or custom error documents).
# Set to "EMail" to also include a mailto: link to the ServerAdmin.
# Set to one of: On | Off | EMail
#
ServerSignature Off
#
# HostnameLookuPS: Log the names of clients or just their IP addresses
# e.g., www.apache.org (on) or 204.62.129.132 (off).
# The default is off because it'd be overall better for the net if people
# had to knowingly turn this feature on, since enabling it means that
# each client request will result in AT LEAST one lookup request to the
# nameserver.
#
HostnameLookups Off
#
# Set a timeout for how long the client may take to send the request header
# and body.
# The default for the headers is header=20-40,MinRate=500, which means wait
# for the first byte of headers for 20 seconds. If some data arrives,
# increase the timeout corresponding to a data rate of 500 bytes/s, but not
# above 40 seconds.
# The default for the request body is body=20,MinRate=500, which is the same
# but has no upper limit for the timeout.
# To disable, set to header=0 body=0
#
<IfModule reqtimeout_module>
RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
</IfModule>
文件的注釋部分已經(jīng)給出了每個(gè)參數(shù)的具體含義,所以這里就沒(méi)必要一一解釋了。