Linux Apache設(shè)置壓縮及緩存
壓縮文件:
編輯apache模塊
vim /etc/httpd/conf.modules.d/00-base.conf
確保以下三個(gè)模塊沒(méi)有被注釋
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
注意:也可以把三個(gè)模塊放到/etc/httpd/conf/httpd.conf文件里,模塊只能在一個(gè)文件中加載
設(shè)置壓縮規(guī)則
vim /etc/httpd/conf/httpd.conf
末尾添加
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
#SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary #設(shè)置不對(duì)后綴gif,jpg,jpeg,png的圖片文件進(jìn)行壓縮
Header append Vary User-Agent
</IfModule>
壓縮率一般都大于70%
壓縮率檢測(cè)(http://tool.chinaz.com/Gzips/)只能在pc端檢測(cè)
設(shè)置緩存:
vim /etc/httpd/conf.modules.d/00-base.conf
確保以下模塊沒(méi)有被注釋
LoadModule headers_module modules/mod_headers.so #第一種緩存方式
LoadModule expires_module modules/mod_expires.so #第二種緩存方式
注意:也可以把這個(gè)模塊放到/etc/httpd/conf/httpd.conf文件里,模塊只能在一個(gè)文件中加載
設(shè)置緩存規(guī)則
vim /var/www/html/.htaccess
末尾添加
第一種緩存方式
FileEtag INode Mtime Size
第二種緩存方式
# 緩存有效時(shí)間 1 月
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css)$">
Header set Cache-Control "max-age=2592000, public"
</FilesMatch>
<FilesMatch "\.(html|txt|htm|php)$">
Header set Cache-Control "max-age=2592000, public, must-revalidate"
</FilesMatch>
注意:max-age為緩存時(shí)間,單位秒,按需修改
第三種緩存方式
vim /etc/httpd/conf/httpd.conf
添加以下內(nèi)容
<IfModule expires_module>
ExpiresActive On
#css文件緩存2592000/3600/24=1月
ExpiresByType text/css A2592000
#js文件緩存2592000/3600/24=1月
ExpiresByType application/x-javascript A2592000
ExpiresByType application/javascript A2592000
#html文件緩存2592000/3600/24=1月
ExpiresByType text/html A2592000
#圖片文件緩存2592000/3600/24=1月
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/ico A2592000
ExpiresByType image/x-icon A2592000
#文件默認(rèn)緩存1月
ExpiresDefault "access plus 30 days"
</IfModule>
注意:以上方式,三選一
重啟apache服務(wù)
service httpd restart
注意:apache設(shè)置壓縮和緩存都會(huì)增加服務(wù)器的內(nèi)存壓力,但設(shè)置壓縮和緩存之后,網(wǎng)站的訪(fǎng)問(wèn)速度會(huì)有所提高
相關(guān)文章
CentOS 6.5平臺(tái)實(shí)現(xiàn)快速部署FTP的方法
這篇文章主要介紹了CentOS 6.5平臺(tái)實(shí)現(xiàn)快速部署FTP的方法,結(jié)合實(shí)例形式分析了CentOS6.5平臺(tái)配置與部署FTP的具體步驟、相關(guān)操作命令與注意事項(xiàng),需要的朋友可以參考下2018-04-04expect實(shí)現(xiàn)Linux自動(dòng)登陸遠(yuǎn)程機(jī)器腳本實(shí)例
expect?是由Don Libes基于Tcl(Tool Command Language )語(yǔ)言開(kāi)發(fā)的,主要應(yīng)用于自動(dòng)化交互式操作的場(chǎng)景,借助Expect處理交互的命令,可以將交互過(guò)程如:ssh登錄,ftp登錄等交互過(guò)程,寫(xiě)到Shell腳本里以實(shí)現(xiàn)一些自動(dòng)化操作。2022-12-12linux命令行報(bào)錯(cuò)bash command not found問(wèn)題及解決
這篇文章主要介紹了linux命令行報(bào)錯(cuò)bash command not found問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Ubuntu 20.04 apt 更換國(guó)內(nèi)源的實(shí)現(xiàn)方法
這篇文章主要介紹了Ubuntu 20.04 apt 更換國(guó)內(nèi)源的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05apache禁止搜索引擎收錄、網(wǎng)絡(luò)爬蟲(chóng)采集的配置方法
這篇文章主要介紹了apache禁止搜索引擎收錄、網(wǎng)絡(luò)爬蟲(chóng)采集的配置方法,注意一定要寫(xiě)到Location節(jié)點(diǎn),否則不起作用,可以精確匹配,也可以IP匹配,需要的朋友可以參考下2014-06-06