apache2 中 I/O 速度的疑問

apache2 中 I/O 速度的疑問

這是我的故事:

我正在致力於改進 develutils.com,我在安裝了 Ubuntu、4Gb RAM 和 2 個 4 核心的 Virtualbox 中進行開發。

我有一個指向 VBox 的本機虛擬主機,並啟動測試以使用 apache test 檢查速度:

ab -n 2700 -c 90 -k -H “接受編碼:gzip、deflate”http://develutils.ben/

這給了我 38 Req/sec

好吧...這是低Req/s,在測試過程中,VBox HDD 指示燈始終亮起,並且 2 個核心未達到 100%...

首先,我嘗試將機器移至另一個磁碟和 SSD,檢查 ab 測試並獲得 360 Req/s,好吧,這個數字就像我一樣,但這給了我更多問題...

我嘗試確定 I/O 過剩的問題是什麼,並使用奇妙的命令:

inotifywait -mr /var/www/Develutils/

這吐了我:

/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/system/temp/cache/ OPEN LANGUAGE_DICTIONARY_es.tmp
/var/www/WEBS/Develutils/system/temp/cache/ ACCESS LANGUAGE_DICTIONARY_es.tmp
/var/www/WEBS/Develutils/system/temp/cache/ CLOSE_NOWRITE,CLOSE LANGUAGE_DICTIONARY_es.tmp
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/templates/bts/css/ OPEN bootstrap-theme.min.css
/var/www/WEBS/Develutils/webroot/js/ OPEN jquery.min.js
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/templates/bts/css/ OPEN bootstrap.min.css
/var/www/WEBS/Develutils/webroot/templates/bts/css/ OPEN bootstrap-datetimepicker.css
/var/www/WEBS/Develutils/webroot/templates/bts/css/ CLOSE_NOWRITE,CLOSE bootstrap-theme.min.css
/var/www/WEBS/Develutils/webroot/templates/bts/css/ OPEN styles.css
/var/www/WEBS/Develutils/webroot/js/ CLOSE_NOWRITE,CLOSE jquery.min.js
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/templates/bts/css/ CLOSE_NOWRITE,CLOSE bootstrap.min.css
/var/www/WEBS/Develutils/webroot/templates/bts/css/ CLOSE_NOWRITE,CLOSE styles.css
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/templates/bts/css/ CLOSE_NOWRITE,CLOSE bootstrap-datetimepicker.css
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/js/ OPEN moment.js
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/js/ CLOSE_NOWRITE,CLOSE moment.js
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/js/ OPEN bootstrap.min.js
/var/www/WEBS/Develutils/webroot/js/ CLOSE_NOWRITE,CLOSE bootstrap.min.js
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/js/ OPEN handlebars.js
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/js/ CLOSE_NOWRITE,CLOSE handlebars.js
/var/www/WEBS/Develutils/webroot/js/ OPEN bootstrap-datetimepicker.js
/var/www/WEBS/Develutils/webroot/js/ OPEN web.js
/var/www/WEBS/Develutils/webroot/js/ CLOSE_NOWRITE,CLOSE web.js
/var/www/WEBS/Develutils/webroot/js/ CLOSE_NOWRITE,CLOSE bootstrap-datetimepicker.js
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/ OPEN .htaccess
/var/www/WEBS/Develutils/ ACCESS .htaccess
/var/www/WEBS/Develutils/ CLOSE_NOWRITE,CLOSE .htaccess
/var/www/WEBS/Develutils/webroot/ OPEN .htaccess
/var/www/WEBS/Develutils/webroot/ ACCESS .htaccess
/var/www/WEBS/Develutils/webroot/ CLOSE_NOWRITE,CLOSE .htaccess

我看到了這一點,不明白為什麼系統會多次讀取 .httaccess ,如果該檔案是現有檔案(.css、.js、映像等),則需要避免 .htaccess,但是apache 不斷調用

我有一個帶有 .htaccess 的基本目錄:

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteRule    ^$    webroot/    [L]
    RewriteRule    (.*) webroot/$1    [L]
</IfModule>

在 webroot 目錄中,我有這個:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>

我有很多問題:

首先,這個.htaccess存取正常還是我有什麼設定錯誤?

其次,如果這是「正常」的,apache沒有任何方法來快取它嗎?如果這是「正常」行為,我認為在使用 ramdisk 時,在系統啟動時複製 CMS 檔案(只有 CMS 檔案和臨時目錄,而不是映像或任何大檔案),然後使用以下命令啟動 apache這個目錄將Req/ s 加速到瘋狂的數字,但我不知道這是否瘋狂。

歡迎任何有關此 Req/s 速度和 I/O 數字的幫助和評論。

答案1

這就是為什麼我把所有內容都放在我的 apache site/mod/port conf 中,它在 apache 啟動時加載然後保存在記憶體中,所以沒有額外的磁碟存取。

透過 apache 存取的每個檔案都會讀取 .htacces,甚至是您網站的圖像和額外內容。

順便說一句,.htaccess 主要用於共享伺服器,以防止濫用使用者配置,但讓他們比預設配置有更多的可能性。

因此,如果您想將 .htaccess 新增至您的網站配置中,您可以使用您的/etc/apache2/site-enabled/{yoursiteconfig}

<Directory /var/www/WEBS/Develutils/>
RewriteEngine on
RewriteRule    ^$    webroot/    [L]
RewriteRule    (.*) webroot/$1    [L]
</Directory>

<Directory /var/www/WEBS/Develutils/webrrot/>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</Directory>

你必須為每個 .htaccess 執行此操作

如果您想減少更多的硬碟訪問,您可以在配置中添加一些快取並刪除一些無用的日誌

例如從日誌中刪除圖像、css 和 js

SetEnvIf Request_URI "\.jpg$|\.jpeg$|\.gif$|\.png$|\.ico|\.icon|\.css$|\.js$" dontlog

<virtualhost {IP}:{PORT}>
...
CustomLog ${APACHE_LOG_DIR}/access.log combined env=!dontlog
...
</virtualhost>

快取文件以防止存取文件

#Remove ETags
Header unset ETag
FileETag none

#MOD_DEFLATE COMPRESSION
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml application/x-javascript application/x-httpd-php

#DEFLATE NOT COMPATIBLE BROWERS
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

#NOT CHACHING IF ALREADY CACHED
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip

#PROXY CHECK CONTENT
Header append Vary User-Agent env=!dont-vary

#CACHE-CONTROL HEADERS
<IfModule mod_headers.c>
<FilesMatch "\.(ico|jpe?g|png|gif|swf|gz|ttf)$">
Header set Cache-Control "max-age=2797200, public"
</FilesMatch>
<FilesMatch "\.(css)$">
Header set Cache-Control "max-age=2797200, public"
</FilesMatch>
<FilesMatch "\.(js)$">
Header set Cache-Control "max-age=2797200, private"
</FilesMatch>
<filesMatch "\.(html|htm)$">
Header set Cache-Control "max-age=86400, public"
</filesMatch>
#Disable caching for scripts and other dynamic files
<FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
Header unset Cache-Control
</FilesMatch>
</IfModule>

#EXPIRES HEADERS
<IfModule mod_expires.c>
ExpiresActive On
#Images
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
AddType image/x-icon .ico
ExpiresByType image/ico "access plus 1 year"
ExpiresByType image/icon "access plus 1 year"
ExpiresByType image/x-icon "access plus 1 year"
#Elements
ExpiresByType application/xhtml+xml "access plus 1 week"
ExpiresByType text/html "access plus 1 week"
ExpiresByType text/css "access plus 1 week"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType text/x-javascript "access plus 1 week"
#Others
ExpiresDefault "access plus 1 month"
</IfModule>

答案2

除非您絕對必須這樣做(例如您位於共享主機上,而這不適合您),那麼您應該將所有內容從 .htaccess 中移出並將其放入伺服器設定檔中。

如果您使用AllowOverride 啟用了.htaccess 文件,那麼Apache httpd 必須針對每個請求檢查資源路徑中的每個目錄是否有.htaccess 檔案。開啟並讀取找到的 .htaccess 文件每一個要求。

這句話再怎麼說也不為過——不要使用 .htaccess 文件,除非沒有其他方法可以完成您想要的操作。通常,您可以在伺服器設定檔中執行所需的所有操作。

相關內容