如果我向configuration为caching上游的Nginx代理服务器发出请求,我会得到以下响应头文件:
{'content-length': '13200000', 'x-cache-status': 'MISS', 'server': 'nginx/1.9.9', 'connection': 'keep-alive', 'cache-control': 'max-age=45', 'date': 'Fri, 27 Jan 2017 10:57:55 GMT'}
几秒钟后,我再次做同样的请求,我得到以下标题:
{'content-length': '13200000', 'x-cache-status': 'HIT', 'server': 'nginx/1.9.9', 'connection': 'keep-alive', 'cache-control': 'max-age=45', 'date': 'Fri, 27 Jan 2017 10:58:18 GMT'}
上游服务器指定max-age头为45秒,第二个响应头是否有更新的max-age头? 这是最大年龄= 45-(请求之间的时间)?
编辑
重现行为的示例configuration:
http { include mime.types; default_type application/octet-stream; upstream backend { server localhost:8080; } proxy_cache_path /etc/nginx/wwwroot/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off; server { listen 80; location / { proxy_pass http://backend; proxy_cache my_cache; add_header X-Cache-Status $upstream_cache_status; } } server { listen 8080; root /etc/nginx/wwwroot; expires 60s; } }
结果:
root@ubuntu:/home/parallels# curl -I localhost/testfile.txt HTTP/1.1 200 OK Server: nginx/1.10.0 (Ubuntu) Date: Sat, 28 Jan 2017 19:08:45 GMT Content-Type: text/plain Content-Length: 12 Connection: keep-alive Last-Modified: Fri, 27 Jan 2017 14:45:39 GMT ETag: "588b5d13-c" Expires: Sat, 28 Jan 2017 19:09:45 GMT Cache-Control: max-age=60 X-Cache-Status: MISS Accept-Ranges: bytes root@ubuntu:/home/parallels# curl -I localhost/testfile.txt HTTP/1.1 200 OK Server: nginx/1.10.0 (Ubuntu) Date: Sat, 28 Jan 2017 19:08:48 GMT Content-Type: text/plain Content-Length: 12 Connection: keep-alive Last-Modified: Fri, 27 Jan 2017 14:45:39 GMT ETag: "588b5d13-c" Expires: Sat, 28 Jan 2017 19:09:45 GMT Cache-Control: max-age=60 X-Cache-Status: HIT Accept-Ranges: bytes
在你的configuration中,Nginx只是一个反向代理。 这不是重写任何东西,因为你没有告诉它重写任何东西。 它只是存储由上游服务器生成的页面,并在被告知时传递它们。
上游服务器已经设置了到期时间,还有一个以秒为单位的最大年龄的caching控制。 我怀疑Nginx并没有阅读这些指令,只要你告诉它,就把它放在caching中,但别人可能会在这里纠正我。
我不认为有什么办法让Nginx跟踪上游服务器何时发送页面,并在将页面传递给客户端之前重写页面的标题。 这必须在应用程序服务器中完成。
根据这篇文章 ,Expires是http / 1.0,caching控制是http / 1.1。 你可以使用,但是它们会发生冲突,所以我猜测浏览器会告诉它刷新页面。
恕我直言,你需要找出你想要的,然后configuration你的应用程序服务器或Nginx使用它。 Nginx可以重写头文件,如果你有你的发行版或者编译的header_mod模块。 如果有一段时间,build议您使用Expires后,页面不能被caching。 如果你只是想浏览器有合理的新鲜的页面,然后caching控制更好,因为你可以定义的东西,如代理服务器和CDN的caching。 您可以始终使用过期指定的时间和caching控制指定CDN和代理caching行为,包括重新validation。
build议更多的思想和研究,你会自己解决剩下的问题。 这里有关caching控制的有用参考 。