Home  >  Article  >  Operation and Maintenance  >  How to configure the expires function of nginx

How to configure the expires function of nginx

(*-*)浩
(*-*)浩Original
2019-06-06 16:55:216211browse

Configuring expires

How to configure the expires function of nginx

##expires plays the role of controlling page caching, reasonable configuration Expires can reduce many server requests. To configure expires, you can add it to the http segment or the server segment or the location segment.

location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
    root /var/www/img/;
    expires 30d;
}
Control the expiration time of pictures, etc. to 30 days. Of course, this time Can be set longer. It depends on the situation.

For example, the

 location ~ \.(wma|wmv|asf|mp3|mmf|zip|rar|swf|flv)$ {
       root /var/www/upload/;
       expires max;
}
expires directive can control the "Expires" and "Cache-Control" headers in the HTTP response (functioning to control the page The role of caching)


Syntax: expires [time|epoch|max|pff]

Default value: off

The expires directive controls the "Expires" in the HTTP response " and "Cache-Control" Header header information, start the function of controlling page caching

time: You can use positive or negative numbers. The value of the "Expires" header will be set by adding the current system time to the set time value.
The time value also controls the value of "Cache-Control":
Negative number means no-cache
Positive number or zero means max-age=time

epoch: Specify the value of "Expires" For 1 January,1970,00:00:01 GMT

max: Specify the value of "Expires" as 31 December2037 23:59:59GMT, and the value of "Cache-Control" as 10 years.
-1: Specify the value of "Expires" as the current server time -1s, which means it will expire forever.
off: Do not modify the values ​​of "Expires" and "Cache-Control"

expires uses a specific time and requires strict synchronization between the server and the client.

Cache-Control uses the max-age directive to specify how long the component will be cached.
For browsers that do not support http1.1, expires still need to be controlled. So it's best to specify two response headers. But the HTTP specification stipulates that the max-age directive will rewrite the expires header.

For more Nginx related technical articles, please visit the

Nginx usage tutorial column to learn!

The above is the detailed content of How to configure the expires function of nginx. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn