Configure expiration time for static elements

Keywords: Javascript vim PHP curl

Configure static element expiration time

The browser will cache static files (pictures, CSS, JS) in the computer by default when visiting the website. So the next time you visit, you don't have to download remotely. How long is the cache? The browser will have its own mechanism to clear the cache. Or it can be set on the remote server side.

The server can define the expiration time through the expires module.

Server defined static element expiration time configuration:

<IfModule mod_expires.c>
    ExpiresActive on  //Switch on the function
    ExpiresByType image/gif  "access plus 1 days"
    ExpiresByType image/jpeg "access plus 24 hours"
    ExpiresByType image/png "access plus 24 hours"
    ExpiresByType text/css "now plus 2 hour"
    ExpiresByType application/x-javascript "now plus 2 hours"
    ExpiresByType application/javascript "now plus 2 hours"
    ExpiresByType application/x-shockwave-flash "now plus 2 hours"
    ExpiresDefault "now plus 0 min"
</IfModule>

This is the expires module, switch (expires active on), configure expiration time for some types of static elements, gif time is 1 day (expires bytype image / gif "access plus 1 days"), png day (expires bytype image / png "access plus 24 hours), css two hours (expires bytype text / css" now plus 2 hour s ")

Open the website (111.com/logo.png), which will display 304

Configure element expiration time in server segment:

To edit a virtual host profile:

[root@shuai-01 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.111.com www.example.com
  #  <Directory /data/wwwroot/111.com> 
  #  <FilesMatch 123.php>
  #     AllowOverride AuthConfig 
  #      AuthName "111.com user auth" 
  #      AuthType Basic 
  #      AuthUserFile /data/.htpasswd  
  #      require valid-user 
  #  </FilesMatch>
   # </Directory> 
       <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^111.com$
        RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L]
        </IfModule>
<IfModule mod_expires.c>
    ExpiresActive on  
    ExpiresByType image/gif  "access plus 1 days"
    ExpiresByType image/jpeg "access plus 24 hours"
    ExpiresByType image/png "access plus 24 hours"
    ExpiresByType text/css "now plus 2 hour"
    ExpiresByType application/x-javascript "now plus 2 hours"
    ExpiresByType application/javascript "now plus 2 hours"
    ExpiresByType application/x-shockwave-flash "now plus 2 hours"
    ExpiresDefault "now plus 0 min"
</IfModule>
        SetEnvIf Request_URI ".*\.gif$" img
        SetEnvIf Request_URI ".*\.jpg$" img
        SetEnvIf Request_URI ".*\.png$" img
        SetEnvIf Request_URI ".*\.bmp$" img
        SetEnvIf Request_URI ".*\.swf$" img
        SetEnvIf Request_URI ".*\.js$" img
        SetEnvIf Request_URI ".*\.css$" img
    ErrorLog "logs/111.com-error_log"
    CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img
</VirtualHost>

This is a module. First, check whether the module file is opened

[root@shuai-01 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expires

Load module in main profile

[root@shuai-01 111.com]# vim /usr/local/apache2.4/conf/httpd.conf

LoadModule env_module modules/mod_env.so
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so

Check configuration file syntax for errors and reload

[root@shuai-01 111.com]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@shuai-01 111.com]# /usr/local/apache2.4/bin/apachectl graceful

This is on the website (111.com/logo.png)

Before and after the expiration of the contract:
No overdue

Expired

It's also reflected in curl

[root@shuai-01 111.com]# curl -x127.0.0.1:80 111.com/logo.png -I
HTTP/1.1 200 OK
Date: Thu, 21 Dec 2017 14:46:37 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.30
Last-Modified: Thu, 21 Dec 2017 14:12:26 GMT
ETag: "1914-560da4a1b6680"
Accept-Ranges: bytes
Content-Length: 6420
Cache-Control: max-age=86400
Expires: Fri, 22 Dec 2017 14:46:37 GMT
Content-Type: image/png

Posted by jamiel on Mon, 30 Mar 2020 23:46:01 -0700