How to use http caching in nginx for html files?

by edmond_brakus , in category: Third Party Scripts , a month ago

How to use http caching in nginx for html files?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by jerad , a month ago

@edmond_brakus 

To use HTTP caching in Nginx for HTML files, you can add the following configuration in your Nginx server block:

  1. Set the caching headers for HTML files:
1
2
3
4
location ~* .html$ {
    expires 1d;
    add_header Cache-Control "public";
}


This configuration sets the caching headers for HTML files to expire after 1 day and allows caching by public proxies and browsers.

  1. Enable caching for HTML files:
1
2
3
4
5
6
7
8
9
location ~* .html$ {
    expires 1d;
    add_header Cache-Control "public";
    proxy_cache my_cache;
    proxy_cache_valid 200 302 1d;
    proxy_cache_valid 301 30m;
    proxy_cache_valid any 1m;
    proxy_cache_key $host$uri$is_args$args;
}


In this configuration, we are enabling caching for HTML files using the Nginx proxy cache module. We are setting different cache lifetimes for different response codes (200, 302, 301) and also setting a default cache lifetime for any other response codes. The proxy_cache_key directive specifies the cache key based on the host, URI, and query parameters.

  1. Define the proxy_cache_path in the Nginx configuration:
1
proxy_cache_path /path_to_cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=1d;


This configuration sets the path to the cache directory, defines the cache size and retention policies. Adjust the path_to_cache to the actual path where you want to store the cache files.

  1. Restart Nginx to apply the changes:


Once you have added the above configurations in your Nginx server block, save the changes and restart Nginx to apply the new caching settings for HTML files:

1
sudo systemctl restart nginx


With these configurations in place, Nginx will now cache HTML files for the specified duration and serve them from the cache instead of fetching the content from the origin server, thus improving the performance and reducing the load on the server.

by aniya.jaskolski , a month ago

@edmond_brakus 

The provided instructions are clear and detailed. By following these steps, you should be able to enable HTTP caching for HTML files in Nginx efficiently.


Here is a summary of the steps for using HTTP caching in Nginx for HTML files:

  1. Set caching headers to expire after a specified duration and allow caching by public proxies and browsers for HTML files.
  2. Enable caching for HTML files by configuring Nginx proxy cache module with different cache lifetimes for different response codes.
  3. Define the proxy_cache_path in the Nginx configuration to specify cache directory, size, and retention policies.
  4. Restart Nginx to apply the new caching settings.


By implementing these configurations, Nginx will cache HTML files for the specified duration, leading to improved performance and reduced server load.