Linux Apache設(shè)置壓縮及緩存
壓縮文件:
編輯apache模塊
vim /etc/httpd/conf.modules.d/00-base.conf
確保以下三個模塊沒有被注釋
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
注意:也可以把三個模塊放到/etc/httpd/conf/httpd.conf文件里,模塊只能在一個文件中加載
設(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è)置不對后綴gif,jpg,jpeg,png的圖片文件進(jìn)行壓縮
Header append Vary User-Agent
</IfModule>
壓縮率一般都大于70%
壓縮率檢測(http://tool.chinaz.com/Gzips/)只能在pc端檢測
設(shè)置緩存:
vim /etc/httpd/conf.modules.d/00-base.conf
確保以下模塊沒有被注釋
LoadModule headers_module modules/mod_headers.so #第一種緩存方式
LoadModule expires_module modules/mod_expires.so #第二種緩存方式
注意:也可以把這個模塊放到/etc/httpd/conf/httpd.conf文件里,模塊只能在一個文件中加載
設(shè)置緩存規(guī)則
vim /var/www/html/.htaccess
末尾添加
第一種緩存方式
FileEtag INode Mtime Size
第二種緩存方式
# 緩存有效時間 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為緩存時間,單位秒,按需修改
第三種緩存方式
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è)置壓縮和緩存都會增加服務(wù)器的內(nèi)存壓力,但設(shè)置壓縮和緩存之后,網(wǎng)站的訪問速度會有所提高
相關(guān)文章
CentOS 6.5平臺實(shí)現(xiàn)快速部署FTP的方法
這篇文章主要介紹了CentOS 6.5平臺實(shí)現(xiàn)快速部署FTP的方法,結(jié)合實(shí)例形式分析了CentOS6.5平臺配置與部署FTP的具體步驟、相關(guān)操作命令與注意事項(xiàng),需要的朋友可以參考下2018-04-04expect實(shí)現(xiàn)Linux自動登陸遠(yuǎn)程機(jī)器腳本實(shí)例
expect?是由Don Libes基于Tcl(Tool Command Language )語言開發(fā)的,主要應(yīng)用于自動化交互式操作的場景,借助Expect處理交互的命令,可以將交互過程如:ssh登錄,ftp登錄等交互過程,寫到Shell腳本里以實(shí)現(xiàn)一些自動化操作。2022-12-12linux命令行報錯bash command not found問題及解決
這篇文章主要介紹了linux命令行報錯bash command not found問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Ubuntu 20.04 apt 更換國內(nèi)源的實(shí)現(xiàn)方法
這篇文章主要介紹了Ubuntu 20.04 apt 更換國內(nèi)源的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05apache禁止搜索引擎收錄、網(wǎng)絡(luò)爬蟲采集的配置方法
這篇文章主要介紹了apache禁止搜索引擎收錄、網(wǎng)絡(luò)爬蟲采集的配置方法,注意一定要寫到Location節(jié)點(diǎn),否則不起作用,可以精確匹配,也可以IP匹配,需要的朋友可以參考下2014-06-06