Dino Geek, try to help you

How to configure cache expiration for static files?


Certainly! Configuring cache expiration for static files is crucial for optimizing website performance by reducing the load on web servers and speeding up the delivery of content to users. Here’s how you can do it using different web servers like Apache, Nginx, and even through Content Delivery Networks (CDNs) such as Cloudflare.

  1. Configuring Cache Expiration in Apache

For Apache, you can use the `.htaccess` file to set cache expiration directives. Here’s an example of how you could configure it:

```
ExpiresActive On

# Set default expiry to 1 month for images ExpiresByType image/jpg “access plus 1 month“ ExpiresByType image/jpeg “access plus 1 month“ ExpiresByType image/gif “access plus 1 month“ ExpiresByType image/png “access plus 1 month” # Set 1 week for CSS and JavaScript ExpiresByType text/css “access plus 1 week“ ExpiresByType text/javascript “access plus 1 week“ ExpiresByType application/javascript “access plus 1 week” # HTML – No cache ExpiresByType text/html “access plus 0 seconds” ```

Place this configuration in your `.htaccess` file located in the root of your web directory. Make sure you have `mod_expires` enabled. You can enable it via:

```
sudo a2enmod expires
sudo systemctl restart apache2
```

  1. Configuring Cache Expiration in Nginx

In Nginx, cache expiration can be set in your server block configuration. Here’s an example:

```
http { include mime.types; default_type application/octet-stream;

sendfile on; keepalive_timeout 65; server { listen 80; location / { root /usr/share/nginx/html; index index.html index.htm; } location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ { expires 1w; add_header Cache-Control “public, no-transform”; } location /no-cache { expires -1; } } } ```

In this configuration:
- Images and CSS/JS files will be cached for 1 week.
- Content under `/no-cache` will not be cached.

  1. Configuring Cache Expiration with Cloudflare

If you’re using a CDN like Cloudflare, you can configure cache expiration through its dashboard:

1. Navigate to the Caching tab on the Cloudflare dashboard.
2. Set the cache expiration for various types of content.

For example, you may set:
- `1 month` for images.
- `1 week` for CSS and JavaScript.
- `No cache` for HTML content.

You can also use Page Rules to fine-tune caching policies for specific URLs.

  1. Why Configure Cache Expiration?

Caching is pivotal for performance. Without proper cache expiration:
- Users repeatedly load the same static resources.
- Server load increases.
- User experience may degrade during peak times.

For example, if you set an image’s cache expiration to 1 month, browsers will store the image locally for a month before requiring a new copy, unless the user manually clears the cache.

  1. Sources

1. [Apache Module `mod_expires`](https://httpd.apache.org/docs/current/mod/mod_expires.html)
2. [Nginx Caching Guide](https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/)
3. [Cloudflare Caching](https://support.cloudflare.com/hc/en-us/articles/202775670-Configuring-Cloudflare-page-rules)

By following these steps and examples, you’ll improve the delivery speed and efficiency of your website, ultimately offering a better user experience while reducing server strain.


Simply generate articles to optimize your SEO
Simply generate articles to optimize your SEO





DinoGeek offers simple articles on complex technologies

Would you like to be quoted in this article? It's very simple, contact us at dino@eiki.fr

CSS | NodeJS | DNS | DMARC | MAPI | NNTP | htaccess | PHP | HTTPS | Drupal | WEB3 | LLM | Wordpress | TLD | Domain name | IMAP | TCP | NFT | MariaDB | FTP | Zigbee | NMAP | SNMP | SEO | E-Mail | LXC | HTTP | MangoDB | SFTP | RAG | SSH | HTML | ChatGPT API | OSPF | JavaScript | Docker | OpenVZ | ChatGPT | VPS | ZIMBRA | SPF | UDP | Joomla | IPV6 | BGP | Django | Reactjs | DKIM | VMWare | RSYNC | Python | TFTP | Webdav | FAAS | Apache | IPV4 | LDAP | POP3 | SMTP

| Whispers of love (API) | Déclaration d'Amour |






Legal Notice / General Conditions of Use