60 Nginx关于浏览器缓存相关的配置指令

60 Nginx关于浏览器缓存相关的配置指令

60.1 expires

expires:用来控制页面缓存的作用,可以通过该指令控制HTTP应答响应中的"Expires"和"Cache-Control"的头信息

语法 expires [modified] time;
expires epoch | max | off;
默认值 expires off;
位置 http 、 server 、location

 

 

 

 

expires 方式1:

  time:整数或负数,指定过期时间

    负数:Cache-control 则为 no-cache

    正整数、0:Cache-control 的值为 max-age=time

  注意:expires 头信息是 HTTP 1.0 出现,当客户端与服务端时间不一致,返回内容会出现问题,因此出现了 Cache-control 的 max-age 代替,两者同时出现 Cache-control 的 max-age 优先级更高,Cache-control 的 max-age 失效时,将使用 expires

expires 方式2:

  epoch::指定Expires的值为'1 January,1970,00:00:01 GMT'(1970-01-01 00:00:00),Cache-Control的值no-cache

  max:指定Expires的值为'31 December2037 23:59:59GMT' (2037-12-31 23:59:59) ,Cache-Control的值为10年

  off:默认不缓存

60.2 expires 整数

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;
    server {
        listen       80;
        server_name  localhost;
                
        location / {
          root html;
          index index.html index.htm;
        }
    
        location ~ .*\.(html|js|css|png)$ {
          expires 1000;      
        }

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code

浏览器添加配置前后信息对比

# Response Headers
HTTP/1.1 304 Not Modified
Server: nginx/1.16.1
Date: Mon, 18 May 2026 15:43:58 GMT
Last-Modified: Thu, 14 May 2026 08:55:52 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "6a058e18-1d0b"
# Response Headers
HTTP/1.1 304 Not Modified
Server: nginx/1.16.1
Date: Mon, 18 May 2026 15:41:11 GMT
Last-Modified: Thu, 14 May 2026 08:55:52 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "6a058e18-1d0b"
Expires: Mon, 18 May 2026 15:57:51 GMT
Cache-Control: max-age=1000

60.3 expires 负数

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;
    server {
        listen       80;
        server_name  localhost;
                
        location / {
          root html;
          index index.html index.htm;
        }
    
        location ~ .*\.(html|js|css|png)$ {
          expires -1000;      
        }

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code
# Response Headers
HTTP/1.1 304 Not Modified
Server: nginx/1.16.1
Date: Mon, 18 May 2026 15:49:12 GMT
Last-Modified: Thu, 14 May 2026 08:55:52 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "6a058e18-1d0b"
Expires: Mon, 18 May 2026 15:32:32 GMT
Cache-Control: no-cache

60.3 expires epoch

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;
    server {
        listen       80;
        server_name  localhost;
                
        location / {
          root html;
          index index.html index.htm;
        }
    
        location ~ .*\.(html|js|css|png)$ {
          expires epoch;      
        }

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code
# Response Headers
HTTP/1.1 304 Not Modified
Server: nginx/1.16.1
Date: Mon, 18 May 2026 15:52:52 GMT
Last-Modified: Thu, 14 May 2026 08:55:52 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "6a058e18-1d0b"
Expires: Thu, 01 Jan 1970 00:00:01 GMT
Cache-Control: no-cache

60.4 expires max

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;
    server {
        listen       80;
        server_name  localhost;
                
        location / {
          root html;
          index index.html index.htm;
        }
    
        location ~ .*\.(html|js|css|png)$ {
          expires max;      
        }

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code
# Response Headers
HTTP/1.1 304 Not Modified
Server: nginx/1.16.1
Date: Mon, 18 May 2026 15:55:28 GMT
Last-Modified: Thu, 14 May 2026 08:55:52 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "6a058e18-1d0b"
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000

60.5 add_header

add_header:添加指定的响应头和响应值

语法 add_header name value [always];
默认值 -
位置 http 、server 、location

 

 

 

 

Cache-Control 作为响应头信息,可以设置如下值: 

Cache-control: must-revalidate
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: public
Cache-control: private
Cache-control: proxy-revalidate
Cache-Control: max-age=<seconds>
Cache-control: s-maxage=<seconds>

 

指令 说明
must-revalidate 可缓存但必须再向源服务器进行确认
no-cache 缓存前必须确认其有效性
no-store 不缓存请求或响应的任何内容
no-transform 代理不可更改媒体类型
public 可向任意方提供响应的缓存
private 仅向特定用户返回响应
proxy-revalidate 要求中间缓存服务器对缓存的响应有效性再进行确认
max-age=<秒> 响应最大Age值
s-maxage=<秒> 公共缓存服务器响应的最大Age值

 

 

 

 

 

 

 

 

 

 

 

60.5 add_header no-store

[root@nginx-100 /usr/local/nginx/conf]# cat nginx.conf
user www;
worker_processes 2;
error_log logs/error.log;
pid logs/nginx.pid;
#daemon on; # 默认on
events {
    accept_mutex on;
    multi_accept on;
    worker_connections  1024;
    use epoll;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout  65;
    include nginx_gzip.conf;
    server {
        listen       80;
        server_name  localhost;
                
        location / {
          root html;
          index index.html index.htm;
        }
    
        location ~ .*\.(html|js|css|png)$ {
          expires max;      
          add_header Cache-Control no-store;
        }

        error_page   500 502 503 504 404 /50x.html;
        location = /50x.html {
            root   html;
        }
    }  
}
[root@nginx-100 /usr/local/nginx/conf]# nginx -t && nginx -s reload
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
View Code
# Response Headers
HTTP/1.1 304 Not Modified
Server: nginx/1.16.1
Date: Mon, 18 May 2026 16:21:29 GMT
Last-Modified: Thu, 14 May 2026 08:55:52 GMT
Connection: keep-alive
Vary: Accept-Encoding
ETag: "6a058e18-1d0b"
Expires: Thu, 31 Dec 2037 23:55:55 GMT
Cache-Control: max-age=315360000
Cache-Control: no-store

 

———————————————————————————————————————————————————————————————————————————

                                                                                                                         无敌小马爱学习

posted on 2026-05-16 14:24  马俊南  阅读(4)  评论(0)    收藏  举报