如何设置Nginx仅在修改静态文件后才传输静态文件 [英] How to setup nginx to transmit static files only when they are modified

查看:101
本文介绍了如何设置Nginx仅在修改静态文件后才传输静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用nginx提供文件夹。我正在通过其他过程更改文件夹中的文件。我想以这样一种方式设置nginx:如果服务器上的文件未更改,则文件内容不会通过http发送。更改文件时(即修改日期已更改),文件将被传输。我浏览了一些文档并做了一些谷歌搜索,但是还没有找到解决这种情况的方法。多数情况下,人们正在清除缓存文件夹,但这对我来说似乎不是很优雅。是否可以通过304/200代码进行组织?即如果文件被更改,服务器将以HTTP 200及其内容进行响应,否则以HTTP 304进行响应。

I am using nginx to serve a folder. I am changing files in the folder by other process. I want to setup nginx in such a way that if file is not changed on server, file contents are not sent over http. When file is changed (i.e. modified date has been changed), file is transferred. I've looked through some docs and done some googling, but haven't found approach for this scenario. Most of the time people are purging cache folder, but this doesn't seem very elegant for me. Is it possible to organize this via 304/200 codes? I.e. if file is changed, server responds with HTTP 200 and its content, else with HTTP 304.

推荐答案

如何配置:


这已经是默认行为;为了使其正常工作,客户端必须发送 If-Modified-Since 和/或 If-None-Match ETag 标头,如 ngx_http_not_modified_header_filter() http://nginx.org/r/if_modified_since

How to configure:

This is already the default behaviour; for this to work properly, the client must be sending the If-Modified-Since and/or If-None-Match ETag headers, as per ngx_http_not_modified_header_filter() and http://nginx.org/r/if_modified_since.

您可以使用 curl ,方法是查找给定资源的 Last-Modified 字段,然后发出后续请求在 If-Modified-Since 标头中具有 exact 相同日期的相同资源(与 ETag If-None-Match )。

You can easily test this with curl by looking up the Last-Modified field of a given resource, and then making a subsequent request of the same resource with the exact same date in a If-Modified-Since header (same for ETag and If-None-Match, respectively).

%curl -i bmap.su | & fgrep -e Date -e HTTP -e Length -e Modified
HTTP/1.1 200 OK
Date: Sat, 10 Aug 2019 04:20:17 GMT
Content-Length: 12842
Last-Modified: Tue, 09 Apr 2013 17:18:16 GMT

%curl -i -H"If-Modified-Since: Tue, 09 Apr 2013 17:18:16 GMT" bmap.su | & fgrep -e Date -e HTTP -e Length -e Modified
HTTP/1.1 304 Not Modified
Date: Sat, 10 Aug 2019 04:20:41 GMT
Last-Modified: Tue, 09 Apr 2013 17:18:16 GMT

%curl -i -H"If-Modified-Since: Tue, 09 Apr 2013 17:18:17 GMT" bmap.su | & fgrep -e Date -e HTTP -e Length -e Modified
HTTP/1.1 200 OK
Date: Sat, 10 Aug 2019 04:27:47 GMT
Content-Length: 12842
Last-Modified: Tue, 09 Apr 2013 17:18:16 GMT

在上述示例中:


  1. 我们首先看到资源的最后修改日期是什么。

我们用 If-Modified-Since 请求标头进行后续请求,该请求标头为 identify 到先前的 Last-Modified 响应标头中的日期;请注意,我们收到了 304未修改响应,正如缺少 Content-Length 字段所期望的那样,

We make a subsequent request with the If-Modified-Since request header that's identical to the date from the prior Last-Modified response header; note that we get a 304 Not Modified response, which, as expected by the lack of a Content-Length field, does not come with any response body, saving the bandwidth.

我们最终测试了此功能的默认设置;根据 http://nginx.org/r/if_modified_since ,它是默认情况下完全匹配;因此,如果我们提供了一个不准确的日期(无论是较早的日期还是较晚的日期),那么我们将返回 200 OK 和完整的响应正文(这是通常是为了避免 Last-Modified 日期恢复为较早的日期而想要的缓存)。

We finally test the default settings of nginx for this feature; as per http://nginx.org/r/if_modified_since, it's an exact match by default; hence, if we provide a non-exact date (whether an earlier or a later one), then we're back to a 200 OK and a full response body (this is usually what you want in order to avoid cache poisoning when the Last-Modified date gets reverted to an earlier one).




使用 curl 来测试 If-None-Match / ETag



Using curl to test If-None-Match / ETag:

%curl -i nginx.org | & fgrep -e HTTP/ -e Date: -e Server: -e Length: -e Modified: -e ETag:
HTTP/1.1 200 OK
Server: nginx/1.15.7
Date: Sat, 10 Aug 2019 05:24:06 GMT
Content-Length: 9053
Last-Modified: Tue, 23 Jul 2019 12:20:32 GMT
ETag: "5d36fb90-235d"

%curl -i -H "If-None-Match: 5d36fb90-235d" nginx.org | & fgrep -e HTTP/ -e Length: -e Modified: -e ETag:
HTTP/1.1 200 OK
Content-Length: 9053
Last-Modified: Tue, 23 Jul 2019 12:20:32 GMT
ETag: "5d36fb90-235d"

%curl -i -H 'If-None-Match: "5d36fb90-235d"' nginx.org | & fgrep -e HTTP/ -e Length: -e Modified: -e ETag:
HTTP/1.1 304 Not Modified
Last-Modified: Tue, 23 Jul 2019 12:20:32 GMT
ETag: "5d36fb90-235d"

这是我们在上面做的事情:

Here's what we were doing above:


  1. 第一步,我们通过 ETag 响应头请求找到实体标签;参见 https://en.wikipedia.org/wiki/HTTP_ETag

  1. In the first step, we make a request to find the entity tag via the ETag response header; see https://en.wikipedia.org/wiki/HTTP_ETag.

第二步,我们在<$ c $中提供了 ETag 的请求c> If-None-Match 请求标头;请注意,我们忘了在引号周围加上引号,这显然对nginx和Apache都是强制性的(可以轻松地通过httpd.apache.org主机进行验证)。

In the second step, we make a request with the ETag being provided in the If-None-Match request header; note that we forgot to put the quotes around it, which, apparently, is mandatory, for both nginx and Apache (as can easily be verified against httpd.apache.org host).

第三步,我们使用实际的 If-None-Match 请求标头提出正确的更正请求,并收到 HTTP / 1.1 304未修改带有正文的响应,如预期的那样。

In the third step, we make a proper corrected request with the actual If-None-Match request header, receiving a HTTP/1.1 304 Not Modified response w/o a body, as expected.

这篇关于如何设置Nginx仅在修改静态文件后才传输静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆