相关文章推荐
飘逸的登山鞋  ·  405 Method Not ...·  1 月前    · 
活泼的高山  ·  Azure REST API ...·  1 月前    · 
跑龙套的青蛙  ·  HTTP headers - HTTP | MDN·  1 月前    · 
风流的绿豆  ·  29. Cache Abstraction·  2 年前    · 
果断的汉堡包  ·  itext 7 html to pdf ...·  2 年前    · 

This topic describes how to configure an NGINX cache policy by using the add_header and expires directives of NGINX.

Note

Disclaimer: This topic may contain information about third-party products. The information is only for reference. Alibaba Cloud does not make guarantees or other forms of commitments for the performance and reliability of the third-party tools, or the potential impacts of operations performed by using these tools.

Run the add_header and expires commands to configure an NGINX cache policy. You can run the commands to specify cache-related fields in HTTP response headers, which affects how browsers and proxy servers cache content.

Procedure

  1. Use the add_header command to specify a custom HTTP header:

    add_header name value;

    For example, you can run the following command to create a header Cache-Control to specify that the resource is not cached:

    add_header Cache-Control "no-cache";
  2. Run the following expires command to configure the time-to-live (TTL):

    expires [time|epoch|max|off];
    • off : does not allow modifying the Expires or Cache-Control response header.

    • time : specifies the time when the cache expires. For example, expires 1h; indicates that the cache expires in 1 hour.

    • epoch : sets the Expires header to a timestamp that follows the UNIX time format. It is the number of seconds that have elapsed since 00:00:00 Thursday, January 1, 1970.

    • max : sets the Expires header to a future point in time and the Cache-Control header to 10 years.

Sample configurations

  • Set the TTL of PHP files to 1 hour.

    location ~ \.php$ {
     expires 1h;
    }
  • Set the cache policy for PHP files to no-cache.

    location ~ \.php$ {
     add_header Cache-Control "no-cache";
    }

This way, you can configure different cache policies for different types of resources or specific locations based on your business requirements.