This topic describes how to configure an NGINX cache policy by using the add_header and expires directives of NGINX.
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
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";
Run the following
expires
command to configure the time-to-live (TTL):expires [time|epoch|max|off];
off
: does not allow modifying theExpires
orCache-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 theExpires
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 theExpires
header to a future point in time and theCache-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.