search
HomeOperation and MaintenanceNginxnginx expires method to control page caching

nginx expires method to control page caching

May 12, 2023 am 08:04 AM
nginxexpires

Syntax: expires [time|epoch|max|pff] Default value: offexpires directive controls the "expires" and "cache-control" header information in the http response, and starts the function of controlling the page cache time: you can use the correct number or negative number. 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": a negative number indicates no-cache, a positive number or zero indicates max-age=time epoch: Specify the value of "expires" as 1january,1970,00:00:01 gmtmax: Specify "expires" "The value of "cache-control" is 31december2037 23:59:59gmt, and the value of "cache-control" is 10 years. -1: Specify the value of "expires" to be -1s of the current server time, 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.
The 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.

If you do not want the proxy or browser to cache, add the no-cache parameter or private parameter:
# expires 1d;
add_header cache-control no-cache;
add_header cache-control private ;
In this way, when the browser f5 is refreshed, the returned value is still 200, not 304.

Record an example of nginx controlling cache:
                                                                                                                                       cache;
add_header cache-control private;
if ( !-e $request_filename) {
rewrite ^(.*) http://test.zhaopin.com/index.html break;
# add_header cache-control no-cache;
# add_header cache-control private;
When I wrote add_header after rewrite, I found that add_header did not work. . It is because it is written into the if..


apache's mod_expires module enables the date to be set in a relative manner like max-age when using the expires header, which is completed through the expiresdefault directive. For example: the expiration time of pictures etc. is 10 years after the request started.

expiresdefault "access plus 10years"
filematch>
It sends expires header and cache-control max-age header in the response.

expires|etag controls the page cache. The difference expires: As mentioned in the above article: the expires directive controls the "expires" and "cache-control" header information in the http response, and starts the function of controlling the page cache time. :Positive or negative numbers can be used. 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": a negative number indicates no-cache, a positive number or zero indicates max-age=time epoch: Specify the value of "expires" as 1january,1970,00:00:01 gmtmax: Specify "expires" "The value is 31december203723:59:59gmt, and the value of "cache-control" is 10 years. -1: Specify the value of "expires" to be -1s of the current server time, 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.
The 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. It is generally used when the page does not change very quickly. If the cache expires, the browser will first confirm whether it is valid before reusing it. It is a "conditional get request". If it is valid, it will return a 304 status code. Expirations are determined via the last-modified response header. As shown in the picture:
First visit:
Request:

nginx expires控制页面缓存的方法
Return:

nginx expires控制页面缓存的方法
Second visit:
Request:

nginx expires控制页面缓存的方法
Return:

nginx expires控制页面缓存的方法
The status code returned at this time is 304, and there are more if-modified in the request than the first time The -since header is compared with the last-modified in the original server, so browser caching is implemented and whether it is expired is determined.
To put it simply, last-modified and if-modified-since are both http header information used to record the last modification time of the page, but last-modified is the http header sent by the server to the client, while if-modified-since is From the header sent by the client to the server, you can see that when requesting a locally existing cache page again, the client will send back the last-modified last modification timestamp sent by the previous server through the if-modified-since header. This It is for the server to perform verification and use this timestamp to determine whether the client's page is the latest. If it is not the latest, new content will be returned. If it is the latest, 304 will be returned to tell the client that the page in its local cache is the latest. , so the client can load the page directly from the local, which will greatly reduce the data transmitted on the network and also reduce the burden on the server.

If you do not want the proxy or browser to cache, add the no-cache parameter or private parameter:
# expires 1d;
add_header cache-control no-cache;
add_header cache-control private ;
nginx expires控制页面缓存的方法



etag: Entity tag is a mechanism used to confirm the validity of web server and browser caches. The origin server uses the etag response header to specify the component's etag and the browser passes the etag back to the origin server via the if-none-match header. If it matches, return 304
as shown:
First visit:
Request:
nginx expires控制页面缓存的方法

Return:
nginx expires控制页面缓存的方法

Second request:
nginx expires控制页面缓存的方法

Return:
nginx expires控制页面缓存的方法

The status code still returned is 304, and there are more ifs in the request than in the first time -none-match header. So implement browser caching. If this value does not match, the cache expires.
etags and if-none-match are a commonly used method to determine whether a resource has changed. Similar to last-modified and http-if-modified-since. But the difference is that last-modified and http-if-modified-since only determine the last modification time of the resource, while etags and if-none-match can be any attribute of the resource.
The working principle of etags and if-none-match is to add etags information in httpresponse. When the client requests the resource again, the if-none-match information (the value of etags) will be added to the httprequest. If the server verifies that the resource's etags have not changed (the resource has not changed), it will return a 304 status; otherwise, the server will return a 200 status and return the resource and the new etags.
The etag format on iis needs to be modified.

The above is the detailed content of nginx expires method to control page caching. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

How to start nginxHow to start nginxApr 14, 2025 pm 01:06 PM

Question: How to start Nginx? Answer: Install Nginx Startup Nginx Verification Nginx Is Nginx Started Explore other startup options Automatically start Nginx

How to check whether nginx is startedHow to check whether nginx is startedApr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to close nginxHow to close nginxApr 14, 2025 pm 01:00 PM

To shut down the Nginx service, follow these steps: Determine the installation type: Red Hat/CentOS (systemctl status nginx) or Debian/Ubuntu (service nginx status) Stop the service: Red Hat/CentOS (systemctl stop nginx) or Debian/Ubuntu (service nginx stop) Disable automatic startup (optional): Red Hat/CentOS (systemctl disabled nginx) or Debian/Ubuntu (syst

How to configure nginx in WindowsHow to configure nginx in WindowsApr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to solve nginx403 errorHow to solve nginx403 errorApr 14, 2025 pm 12:54 PM

The server does not have permission to access the requested resource, resulting in a nginx 403 error. Solutions include: Check file permissions. Check the .htaccess configuration. Check nginx configuration. Configure SELinux permissions. Check the firewall rules. Troubleshoot other causes such as browser problems, server failures, or other possible errors.

How to start nginx in LinuxHow to start nginx in LinuxApr 14, 2025 pm 12:51 PM

Steps to start Nginx in Linux: Check whether Nginx is installed. Use systemctl start nginx to start the Nginx service. Use systemctl enable nginx to enable automatic startup of Nginx at system startup. Use systemctl status nginx to verify that the startup is successful. Visit http://localhost in a web browser to view the default welcome page.

How to check whether nginx is started?How to check whether nginx is started?Apr 14, 2025 pm 12:48 PM

In Linux, use the following command to check whether Nginx is started: systemctl status nginx judges based on the command output: If "Active: active (running)" is displayed, Nginx is started. If "Active: inactive (dead)" is displayed, Nginx is stopped.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software