Nginx · December 19, 2023

Nginx Tip - Set expires headers for static content caching

Nginx Tip - Set expires headers for static content caching

When it comes to website performance, one of the key factors to consider is the caching of static content. By setting expires headers for static content, you can significantly improve the loading speed of your website. In this article, we will explore how to configure Nginx to set expires headers for static content caching.

What are expires headers?

Expires headers are a way to instruct the browser on how long it should cache static content, such as images, CSS files, and JavaScript files. By setting an expiration date in the headers, you can tell the browser to store a copy of the file locally for a certain period of time. This eliminates the need for the browser to request the file from the server every time a user visits your website, resulting in faster page load times.

Configuring Nginx to set expires headers

To set expires headers for static content caching in Nginx, you need to modify the server configuration file. Here's how you can do it:

  1. Open the Nginx configuration file using a text editor. The location of the file may vary depending on your operating system and Nginx installation. Typically, it can be found at /etc/nginx/nginx.conf or /etc/nginx/conf.d/default.conf.
  2. Locate the http block in the configuration file. This block contains the global configuration settings for Nginx.
  3. Inside the http block, add the following lines of code:

http {
  ...
  server {
    ...
    location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
      expires 30d;
    }
    ...
  }
  ...
}

In the above code, we are using a regular expression to match the file extensions of static content that we want to cache. You can modify the regular expression to include or exclude specific file types based on your requirements.

The expires 30d; directive sets the expiration time for the matched files to 30 days. You can adjust the value to a different duration if needed.

Save the configuration file and restart Nginx for the changes to take effect. You can do this by running the following command:


sudo service nginx restart

Verifying the configuration

Once you have configured Nginx to set expires headers for static content caching, you can verify if it is working correctly. Open your website in a browser and inspect the network requests using the browser's developer tools.

Look for the static files that you have configured to have expires headers. In the response headers, you should see the Expires header with a value that is 30 days in the future (or the duration you have set).

Summary

Setting expires headers for static content caching is an effective way to improve website performance by reducing the number of requests made to the server. By configuring Nginx to set expires headers, you can instruct the browser to cache static files for a specified duration, resulting in faster page load times.

If you are looking for reliable VPS hosting solutions in the Hong Kong, consider Server.HK. With our top-notch VPS hosting services, you can ensure optimal performance and speed for your website.