edit-icon download-icon

Nginx cache policy settings

Last Updated: Dec 18, 2017

The HTTP header processing module (HTTP Headers) allows you to set any HTTP header.

  • add_header

    • Syntax: add_header name value

    • Default value: none

    • Fields: http, server, and location

  • expires

    • Syntax: expires [time|epoch|max|off]

    • Default value: expires off

    • Fields: http, server, and location

      The directive controls whether and how to specify the expiration time in a response packet.

    • off: forbids the modification of the Expires and Cache-Control headers.

    • Time: controls the value of “Cache-Control”. A negative value indicates no-cache.

    • epoch: sets the Expires header to 1 January, 1970 00:00:01 UTC.

    • max: sets the Expires header to 31 December 2037 23:59:59 UTC, and sets Cache-Control to the maximum value, which is 10 years.

The following example shows that the expiration time for PHP files is set to 1 hour:

The following example shows that Cache-Control for PHP files is set to no-cache so that PHP files are not cached by a cache server:

You can also configure a cache policy using add_header, setting dynamic PHP files to no-cache:

  1. location ~ .*.php$ {
  2. if ($request_uri !~ ^/dynamicimg/) {
  3. add_header Cache-Control "no-cache";
  4. add_header Pragma no-cache;
  5. }
  6. }
Thank you! We've received your feedback.