Home >Operation and Maintenance >Linux Operation and Maintenance >Example of how to set up compression and caching for Apache in Linux
This article mainly shares with you examples of how to set up compression and caching for Apache in Linux. I hope it can help you.
Compressed file:
Edit apache module
vim /etc/httpd/conf.modules.d/00-base.conf
Make sure the following three modules are not commented
LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so
Note: You can also put the three modules into the /etc/httpd/conf/httpd.conf file. The module can only be loaded in one file.
Set compression rules
vim /etc/httpd/conf/httpd.conf
Add
at the end
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 #Set not to compress image files with suffixes gif, jpg, jpeg, png
Header append Vary User-Agent
Compression The rate is generally greater than 70%
Compression rate detection (http://tool.chinaz.com/Gzips/) can only be detected on the PC side
Set cache:
vim /etc/httpd/conf.modules.d/00-base.conf
Make sure the following modules are not commented
LoadModule headers_module modules/mod_headers. so #The first caching method
LoadModule expires_module modules/mod_expires.so #The second caching method
Note: You can also put this module in /etc/httpd/conf/httpd. In the conf file, the module can only be loaded in one file
Set cache rules
vim /var/www/html/.htaccess
End Add
The first caching method
FileEtag INode Mtime Size
The second caching method
# Cache Valid for 1 month
Header set Cache-Control "max-age=2592000, public"
Header set Cache-Control "max-age=2592000, public, must-revalidate"
Note: max-age is the cache time, in seconds, and can be modified as needed
The third caching method
vim /etc/httpd/conf/httpd.conf
Add the following content
##Note: Choose one of the above methodsRestart apache serviceExpiresActive On
#css file Cache 2592000/3600/24=January
ExpiresByType text/css A2592000
#js file cache 2592000/3600/24=January
ExpiresByType application/x-javascript A2592000
ExpiresByType application/javascript A2592000
#html file cache 2592000/3600/24=January
ExpiresByType text/html A2592000
#Image file cache 2592000/3600/24=January
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
#File default cache January
ExpiresDefault "access plus 30 days"
service httpd restartNote: Apache setting compression and caching will increase the memory pressure of the server, but after setting compression and caching, the access speed of the website will be improvedRelated recommendations:
Detailed explanation of the configuration of Apache and PHP
The above is the detailed content of Example of how to set up compression and caching for Apache in Linux. For more information, please follow other related articles on the PHP Chinese website!