问题原文
原帖链接 · Jonathan Prates · CC BY-SA 4.0
I have a simple nginx configuration for the /info location.
location /info {
uwsgi_read_timeout 20s;
uwsgi_send_timeout 20s;
uwsgi_pass unix:///tmp/uwsgi.sock;
include uwsgi_params;
}
This location needs to be cached. So I added
proxy_cache info_cache;
proxy_cache_valid 200 10m;
to this configuration. It worked fine.
After a while, we realized some old buggy clients of our app are sending the header If-None-Match, and nginx deliveries 304 Not Modified as expected. The problem is these clients are waiting for a 200 OK plus a response body. Unfortunately, we are not being able to fix them all.
Is it possible to have nginx ignoring If-None-Match and still delivering from cache?
After a few tests, even by dropping the header value, proxy_set_header If-None-Match "";, if the content comes from the cache, nginx checks If-None-Match. Remembering it's from the proxy pass, not static.
已采纳解答
原帖链接 · cnst · CC BY-SA 4.0
- You mention doing http://nginx.org/r/uwsgi_pass, but then doing caching with http://nginx.org/r/proxy_cache et al, not with http://nginx.org/r/uwsgi_cache et al. Are you sure it's actually working properly? I wasn't aware that these directives are interchangeable — I don't think they are at all, even though they certainly sound similar and do similar things.
Have you tried changing http://nginx.org/r/if_modified_since from the default of
exacttooff?if_modified_since off;Based on my reading of
src/http/modules/ngx_http_not_modified_filter_module.c::ngx_http_not_modified_header_filter():If the header
If-Modified-Sinceis present (and is non-empty), and theif_modified_sincedirective is set tooff(beingNGX_HTTP_IMS_OFFdefinein the source code), thenngx_http_test_if_modified()immediately returns true, resulting in the filter shortcircuiting withreturn ngx_http_next_header_filter(r); in other words, in such a case, theIf-None-Matchlogic wouldn't be executing.Note that this happens only if the
If-Modified-Sinceheader is actually present (and non-empty) in the request (in addition toIf-None-Matchthat you're dealing with), otherwise, the code path will continue without theNGX_HTTP_IMS_OFFsetting having any effect; I think the expectation here is that if the clients already includeIf-None-Match, thenIf-Modified-Sinceis likely included as well; thus, there doesn't seem to be a separate option to disable processing ofIf-None-Matchspecifically and individually.78 if (r->headers_in.if_modified_since || r->headers_in.if_none_match) { 79 80 if (r->headers_in.if_modified_since 81 && ngx_http_test_if_modified(r)) 82 { 83 return ngx_http_next_header_filter(r); 84 } 85 86 if (r->headers_in.if_none_match 87 && !ngx_http_test_if_match(r, r->headers_in.if_none_match)) 88 { 89 return ngx_http_next_header_filter(r); 90 } … 132ngx_http_test_if_modified(ngx_http_request_t *r) 133{ … 139 if (clcf->if_modified_since == NGX_HTTP_IMS_OFF) { 140 return 1; 141 }Note that
ETagsupport, together with theIf-None-Match, is only available instable-1.4and above (it's not present instable-1.2and below), so, downgrading might also be an option.
Have you tried clearing the
ETagbefore the client?Do your clients send
If-None-Match: *? If so, then as per https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.26, not performing the GET action would be correct (e.g., returning304 Not Modifiedis entirely correct), and that's exactly what nginx does, which can easily be verified by running something likecurl -H"If-None-Match: *" -v localhost:80on a default installation with nginx/1.15.9 (note thatstable-1.2doesn't haveETagorIf-None-Matchsupport, as they first appeared withstable-1.4).If not, you might try to omit the
ETagin your responses to trick the clients to not send anIf-None-Match.You can use http://nginx.org/r/add_header to clear the
ETagheader from responses to the clients:add_header ETag "";
BTW, I've also tried modifying
$http_if_none_matchdirectly with http://nginx.org/r/set, but it didn't have any effect.
As a final resort, if a better solution isn't available (although I think that the above solutions might as well work), what you could do is put another instance of nginx in front of your regular caching nginx specifically for those buggy clients. In this new nginx instance, you'd disable all sorts of caching, remove the header that's not handled correctly by your buggy client apps, and pass the request to the normal instance of nginx that does do the caching.
More specifically, you might want to make sure that this new instance wouldn't do any buffering, either, http://nginx.org/r/proxy_buffering, otherwise, you risk getting your performance reduced by an extra step of saving stuff to disk all over again by the extra instance; else, it should all be copied in memory, and will likely be fast enough.
I think this approach should definitely work, because this frontline nginx wouldn't have any cache, thus, it couldn't possibly reply with
304 Not Modified, whereas the backend nginx wouldn't receive theIf-None-Matchif you clear theIf-None-Matchon the frontend nginx with http://nginx.org/r/proxy_set_header before doing a http://nginx.org/r/proxy_pass to the backend nginx.
P.S. The other answer suggests using http://nginx.org/r/proxy_ignore_headers, but, based on documentation around it as well as around http://nginx.org/r/proxy_set_header, it doesn't seem like sprinkling If-None-Match should make any difference; plus, it looks like there's no _set_header variant for uwsgi anyways.
P.P.S. Another potential avenue, if you don't actually need to deliver things from cache, is changing http://nginx.org/r/uwsgi_cache_revalidate from the default of off to on; however, I think this, too, would depend on _set_header, which is missing in the uwsgi version.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
