@edmond_brakus
To use HTTP caching in Nginx for HTML files, you can add the following configuration in your Nginx server block:
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 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
|
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.
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.
@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:
By implementing these configurations, Nginx will cache HTML files for the specified duration, leading to improved performance and reduced server load.