Linux Apache setting compression and caching

User 1685462 2022-06-24 06:15:03 阅读数:840

linuxapachesettingcompressioncaching

Compressed files :

edit apache modular

vim /etc/httpd/conf.modules.d/00-base.conf

Make sure that the following three modules are not annotated

LoadModule headers_module modules/mod_headers.so
LoadModule deflate_module modules/mod_deflate.so
LoadModule filter_module modules/mod_filter.so

Be careful : You can also put three modules into /etc/httpd/conf/httpd.conf In the document , Modules can only be loaded in one file

Set compression rules

vim /etc/httpd/conf/httpd.conf

Add... At the end

<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 # The suffix is not set gif,jpg,jpeg,png The image file is compressed
Header append Vary User-Agent
</IfModule>

The compression ratio is generally greater than 70%

Compression ratio detection (http://tool.chinaz.com/Gzips/) Only in pc End detection

Set the cache :

vim /etc/httpd/conf.modules.d/00-base.conf

Make sure that 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 cache method 

Be careful : You can also put this module into /etc/httpd/conf/httpd.conf In the document , Modules can only be loaded in one file

Set cache rules

vim /var/www/html/.htaccess

Add... At the end

The first caching method

FileEtag INode Mtime Size

The second cache method

# Cache effective time 1 month
<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>

Be careful :max-age For cache time , Unit second , Modify as needed

The third caching method

vim /etc/httpd/conf/httpd.conf

Add the following

<IfModule expires_module>
ExpiresActive On
#css File cache 2592000/3600/24=1 month
ExpiresByType text/css A2592000
#js File cache 2592000/3600/24=1 month
ExpiresByType application/x-javascript A2592000
ExpiresByType application/javascript A2592000
#html File cache 2592000/3600/24=1 month
ExpiresByType text/html A2592000
# Picture file cache 2592000/3600/24=1 month
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 1 month
ExpiresDefault "access plus 30 days"
</IfModule>

Be careful : Above way , Choose one of three

restart apache service

service httpd restart

Be careful :apache Setting compression and caching will increase the memory pressure of the server , But after setting up compression and caching , The access speed of the website will be improved

copyright:author[User 1685462],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/20210723184704802T.html