首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nginx图像头缓存不更新

Nginx图像头缓存不更新
EN

Server Fault用户
提问于 2018-03-20 00:22:43
回答 1查看 2.1K关注 0票数 0

我在https://www.example.com/img/bart-had-a-groove.gif有一个图像文件

这是我的站点配置中唯一的缓存块,全局nginx.conf中没有缓存设置。

代码语言:javascript
复制
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public, no-transform, max-age=2628000";
}

现在,您可以看到我的标题被设置为Cache-Control "public, no-transform, max-age=2628000";,但是如果您将图像卷曲(例如,curl -X GET -I https://www.example.com/img/bart-had-a-groove.gif ),您将得到:

代码语言:javascript
复制
HTTP/1.1 200 OK
Content-Type: image/gif
Connection: keep-alive
Server: nginx/1.13.9
Content-Length: 771510
Last-Modified: Fri, 17 Feb 2017 17:26:23 GMT
ETag: "58a7323f-bc5b6"
Pragma: public
Cache-Control: public
Accept-Ranges: bytes
Date: Mon, 19 Mar 2018 21:49:33 GMT
Expires: Wed, 18 Apr 2018 21:49:33 GMT

您可以看到Cache-Control: public与配置不匹配。它缺少了no-transform, max-age=2628000部分。

每次变更后我都会运行systemctl stop nginx && systemctl start nginx && systemctl reload nginx

更新服务器块的其余部分:

代码语言:javascript
复制
server {
  listen 80;
  listen 443;
  server_name example.com;
  return 301 https://www.$server_name$request_uri;
}

server {
  listen 80;
  listen 443 ssl http2;
  root /var/www/example.com/htdocs/;

  index index.html index.htm;

  server_name www.example.com;

  location / {
    autoindex on;
    try_files $uri $uri/ =404;    
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
    # Security note: If you're running a version of PHP older than the
    # latest 5.3, you should have "cgi.fix_pathinfo = 0;" in php.ini.
    # See http://serverfault.com/q/627903/94922 for details.
    include fastcgi_params;
    # Block httpoxy attacks. See https://httpoxy.org/.
    fastcgi_param HTTP_PROXY "";
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_intercept_errors on;
    # PHP 5 socket location.
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    # PHP 7 socket location.
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  }

location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
  #expires 1M;
  access_log off;
  add_header Cache-Control "public, no-transform, max-age=2628000";
}

  pagespeed on;
  # Needs to exist and be writable by nginx.  Use tmpfs for best performance.
  pagespeed FileCachePath /var/ngx_pagespeed_cache;
  # Ensure requests for pagespeed optimized resources go to the pagespeed handler
  # and no extraneous headers get set.
  location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
    add_header "" "";
  }
  location ~ "^/pagespeed_static/" { }
  location ~ "^/ngx_pagespeed_beacon$" { }
}
EN

回答 1

Server Fault用户

回答已采纳

发布于 2018-03-20 10:03:39

您正在使用两个相关的配置指令,涉及“缓存-控制”头(见下文)。我建议只使用add_ header ,因为您欣赏"Cache-Control“头部的"no-transform”指令。

expires 1M;

->将设置标题“缓存-控制”和“过期”。

见文件

启用或禁用添加或修改“过期”和“缓存-控制”响应标头.

add_header缓存-控制“公共的,不转换的,最大年龄=2628000”;

->将手动设置“缓存-控制”标题。

票数 2
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/903487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档