Nginx 下关于缓存控制字段 cache-control 的配置说明( 五 )

运维案例分享: Nginx 增加缓存控制字段 cache-control
开发发过来的需求:
1) 对于 html 文件,cache control 设置为 no-cache;
2) 对于 js,图片,css,字体等,设置 max-age=2592000. 也就是30天;
注意点:
- 缓存控制字段 cache-control 的配置 (add_header) 要放在 http, server, location 区域, 或是放在 location 的 if 判断里, 例如"add_header Cache-Control no-cache;".
- 如果前面有 LB 负载代理层, 则缓存控制字段 cache-control 配置要放在后端的真实服务器 nginx 的 location 区域, 并且要指定 root 根路径, 否则访问会出现 404 (即找不到访问路径);
针对上面的案例需求, 操作记录如下:
1) 本案中在实际场景中, 有 LB 层. LB 层的 nginx 配置不需要配置, 这里只是粘贴下负载配置:
[root@fvtlb01 ~]# cat /data/nginx/conf/vhosts/fvtkevin-web.veredholdings-inc.com.confupstream fvtkevin-web-inc { ip_hash; server 172.16.50.73:80 max_fails=3 fail_timeout=15s; server 172.16.50.74:80 max_fails=3 fail_timeout=15s;}server { listen 80; server_name fvtkevin-web.veredholdings-inc.com;access_log /data/nginx/logs/fvtkevin-web.veredholdings-inc.com-access.log main; error_log /data/nginx/logs/fvtkevin-web.veredholdings-inc.com-error.log;location / { proxy_pass http://fvtkevin-web-inc; proxy_redirect off ; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 600; proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 http_404; proxy_max_temp_file_size 128m; #proxy_cache mycache;#proxy_cache_valid 200 302 1h; #proxy_cache_valid 301 1d; #proxy_cache_valid any 1m;}} 2) 缓存控制字段 cache-control 的配置要放在后端两台真实服务器 172.16.50.73 和172.16.50.74上.
a) 172.16.60.73 (即 fvtkevin-dmz01.veredholdings.cn)服务器上缓存控制字段 cache-control 的配置如下:
[root@fvtkevin-dmz01 ~]# cat /data/nginx/conf/vhosts/fvtkevin-web01.veredholdings.cn.confserver { listen 80; server_name fvtkevin-dmz01.veredholdings.cn;access_log /data/nginx/logs/fvtkevin-dmz01.veredholdings.cn-access.log main; error_log /data/nginx/logs/fvtkevin-dmz01.veredholdings.cn-error.log;location / { root /data/web/kevin; index index.php index.html index.htm; }location ~ .(css|js|gif|jpg|jpeg|png|bmp|swf|ttf|woff|otf|ttc|pfa)$ { root /data/web/kevin; expires 30d; }location ~ .(html|htm)$ { root /data/web/kevin; add_header Cache-Control no-cache; }location /document/ { alias /data/web/document/; }location ~ .(css|js|gif|jpg|jpeg|png|bmp|swf|ttf|woff|otf|ttc|pfa)$ { root /data/web/document; expires 30d; }location ~ .(html|htm)$ { root /data/web/document; add_header Cache-Control no-cache; }}b) 172.16.60.74 (即 fvtkevin-dmz02.veredholdings.cn)服务器上缓存控制字段 cache-control 的配置如下:
[root@fvtkevin-dmz02 ~]# cat /data/nginx/conf/vhosts/fvtkevin-web02.veredholdings.cn.confserver { listen 80; server_name fvtkevin-web02.veredholdings.cn;access_log /data/nginx/logs/fvtkevin-web02.veredholdings.cn-access.log main; error_log /data/nginx/logs/fvtkevin-web02.veredholdings.cn-error.log;location / { root /data/web/kevin; index index.php index.html index.htm; }location ~ .(css|js|gif|jpg|jpeg|png|bmp|swf|ttf|woff|otf|ttc|pfa)$ { root /data/web/kevin; expires 30d; }location ~ .(html|htm)$ { root /data/web/kevin; add_header Cache-Control no-cache; }location /document/ { alias /data/web/document/; }location ~ .(css|js|gif|jpg|jpeg|png|bmp|swf|ttf|woff|otf|ttc|pfa)$ { root /data/web/document; expires 30d; }location ~ .(html|htm)$ { root /data/web/document; add_header Cache-Control no-cache; }}以上配置中, 关于缓存控制字段 cache-control 的配置主要有两个:
1) http://fvtkevin-web.veredholdings-inc.com/ 下:
对于 html, html 格式的文件,cache control 设置为 no-cache;
对于js,图片,css,字体等,设置 max-age=2592000;
这是基于 /data/web/kevin 的 root 根目录下的
2) http://fvtkevin-web.veredholdings-inc.com/document 下:
对于 html, html 格式的文件,cache control 设置为 no-cache;
对于js,图片,css,字体等,设置 max-age=2592000;
这是基于 /data/web/document 的 root 根目录下的
以上配置后, 访问 http://fvtkevin-web.veredholdings-inc.com/ 或者 http://fvtkevin-web.veredholdings-inc.com/document 进行验证. 这里验证下 http://fvtkevin-web.veredholdings-inc.com/, 效果如下:

Nginx 下关于缓存控制字段 cache-control 的配置说明

文章插图
 
上面显示了 http://fvtkevin-web.veredholdings-inc.com/ 首页(即 index.html 文件)访问头部信息里有"no-cache"信息!


推荐阅读