Moss doesn't make any assumption regarding the caching policy for the resources of your website, since that's completely dependent on the type of application you're building. Luckily, thanks to Nginx extra config and Apache's .htaccess, Moss provides you with a mechanism to establish the resources that will be cached, where they can be cached, and for how long 😊. If you came here looking for a solution for WordPress, you'd better check how to set up the browser cache using an external plugin.

To show you how to do this, we'll apply the config of an example taken from Servers for Hackers to a site named site.com and run by user user. The following policy will be set up:

  • Don't cache HTML nor other data of your website
  • Cache feed (RSS/ATOM) for an hour (both in browsers and proxies)
  • Cache images for a month (both in browsers and proxies)
  • Cache Javascript and CSS for a year (both in browsers and proxies)

Nginx configuration

Do this if your website uses Nginx as an standalone web server. If you're using Nginx as a reverse proxy in front of Apache, then please jump into the section below.

Edit your config

You can edit your config file using your favorite SFTP client or by connecting to your server via SSH. Assuming the latter:

  1. Log into your server as user moss: ssh moss@<server-ip> 
  2. Using sudo and your favorite text editor, add the following lines to file /etc/openresty/server_params.site.com:
# Don't cache app html/data
location ~* \.(?:manifest|appcache|html?|xml|json)$ {
    expires -1;
}

# Cache feed
location ~* \.(?:rss|atom)$ {
    expires 1h;
    add_header Cache-Control "public";
}

# Cache images, audio, video
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
    expires 1M;
    access_log off;
    add_header Cache-Control "public";
}

# Cache css/javascript
location ~* \.(?:css|js)$ {
    expires 1y;
    access_log off;
    add_header Cache-Control "public";
}

You may find some information about these directives in the Nginx documentation.

Provision your website

  1. Log into Moss
  2. Head to the details of site.com 
  3. Click the 'Provision' button and wait until Moss finishes the process

Apache configuration

Do this if your website uses Nginx as a reverse proxy in front of Apache. Otherwise - Nginx is the only web server for this site - take a look to the section above.

Edit your config

We're going to create/update a .htaccess file at the root dir of your site. Most likely you're using git and you only need to add or edit such file in your repo. If you're uploading your site onto your server by any other means (e.g. via SCP or SFTP), then use the same method to upload this .htaccess fragment.

# Add mime-types for icons
<IfModule mod_mime.c>
    AddType image/x-icon .ico
    AddType image/x-icon .cur
</IfModule>

# Set "Expires" header
<IfModule mod_expires.c>
    ExpiresActive On

    ExpiresByType audio/ogg        "access plus 1 months"
    ExpiresByType audio/webm       "access plus 1 months"
    ExpiresByType image/jpg        "access plus 1 months"
    ExpiresByType image/jpeg       "access plus 1 months"
    ExpiresByType image/gif        "access plus 1 months"
    ExpiresByType image/png        "access plus 1 months"
    ExpiresByType image/x-icon     "access plus 1 months"
    ExpiresByType image/svg+xml    "access plus 1 months"
    ExpiresByType text/xml         "access plus 1 hours"
    ExpiresByType text/x-component "access plus 1 months"
    ExpiresByType video/mp4        "access plus 1 months"
    ExpiresByType video/ogv        "access plus 1 months"
    ExpiresByType video/webm       "access plus 1 months"

    ExpiresByType application/javascript  "access plus 1 years"
    ExpiresByType text/css                "access plus 1 years"
</IfModule>

# Set "Cache-Control" header to prevent caching
<FilesMatch "\.(manifest|appcache|html?|xml|json)$">
    <IfModule mod_headers.c>
        Header set Cache-Control "no-cache"
    </IfModule>
</FilesMatch>

# Set "Cache-Control" header to allow public caching
<FilesMatch "\.(rss|atom|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc|css|js)$">
    <IfModule mod_headers.c>
        Header set Cache-Control "public"
    </IfModule>
</FilesMatch>

You may find more information about these directives in the Apache documentation:

Provision or deploy your website

If you're using git and Push to deploy is enabled for this website:

  1. Commit your changes to the .htaccess at the root dir of your website
  2. Run git push

If you're using git but Push to deploy is disabled for this website:

  1. Commit your changes to the .htaccess at the root dir of your website
  2. Run git push
  3. Log into Moss
  4. Head to the details of site.com 
  5. Click the Deploy button and wait until Moss finishes the process

If you're not using git at all:

  1. Upload the .htaccess to the root dir of your website by any means (SFTP, SCP, etc). Remember to do this as user user, otherwise you'll have permission issues.
  2. Log into Moss
  3. Head to the details of site.com 
  4. Click the 'Provision' button and wait until Moss finishes the process
Did this answer your question?