Disclaimer: this document may contain information about third-party products that are for reference only. Alibaba Cloud does not make any guarantee, express or implied, with respect to the performance and reliability of third-party products, as well as potential impacts of operations on the products.

Overview

This article describes how to configure the cache policy of an Nginx server.


Detailed information

HTTP header processing module

You can set any HTTP header as follows.

add_header

  • Syntax: add_header name value.
  • Default value: none.
  • Used fields: http, server, and location.
 

expires

  • Syntax: expires [time|epoch|max|off].
  • Default value: expires off.
  • Used fields: http, server, and location.
  • This directive controls whether an expiration time is set in a response packet. The following describes the Expiration Time.
    • off disables adding or modifying the Expires and Cache-Control response headers.
    • The value of Cache-Control in Time Control. A negative value indicates no-cache.
    • epoch sets the Expires header to 1 January, 1970 00:00:01 GMT.
    • max sets the Expires header to 31 December 2037 23:59:59 GMT, and maximizes the Cache-Control to 10 years.
 

Nginx cache settings

The configuration example is as follows:
  • Set the cache duration for php files to one hour.


  • Set php files to no-cache to disable the cache function.


  • Use the following sample configuration file add_header to set a cache policy for dynamic php files.
    location ~. *\.php$ {
    if ($request_uri! ~ ^/dynamicimg/) {
    add_header Cache-Control "no-cache";
    add_header Pragma no-cache
       }
    }

 


Applicable to

  • CDN