Nginx 映像頭快取未更新

Nginx 映像頭快取未更新

我有一個圖像檔案位於https://www.example.com/img/bart-had-a-groove.gif

這是我的網站配置中唯一的快取區塊,全域 nginx.conf 中沒有快取設定。

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,例如curl -X GET -I https://www.example.com/img/bart-had-a-groove.gif您將獲得:

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追隨每一次改變。

更新 伺服器區塊的其餘部分:

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$" { }
}

答案1

您正在使用兩個關於「Cache-Control」標頭的相關設定指令(請參閱下文)。我建議僅使用新增標題正如您欣賞“Cache-Control”標頭的“no-transform”指令一樣。

1M 過期;

--> 將設定標頭「Cache-Control」和「Expires」。

查看文件

啟用或停用新增或修改“Expires”和“Cache-Control”回應標頭...

add_header Cache-Control「公共,無轉換,max-age = 2628000」;

--> 將手動設定“Cache-Control”標頭。

相關內容