캐싱은 'client' 와 'cache proxy' 모두에서 발생할 수 있습니다.
원본서버(Origin)에서는 HTTP 응답 헤더를 사용하여 'client' 와 'cache proxy' 모두에 대한 캐싱을 제어할 수 있습니다.
원본의 Cache-Control
원본 서버는 Cache-Control응답 헤더를 통해 콘텐츠 캐싱 여부 및 TTL등을 제어할 수 있습니다..
- public: client 및 proxy 모두에 의해 캐시될 수 있음
- private: client 에 의해 캐시될 수 있지만 proxy 는 캐시될 수 없음
- max-age: cache TTL을 결정하고 그 후에 요청은 Origin에 요청
- s-maxage: proxy에만 사용되는 TTL
- must-revalidate: max-age TTL이 만료되지 않은 경우 캐시된 콘텐츠를 제공하고, 만료된 경우 캐시된 콘텐츠의 유효성을 다시 검사해야함
- no-store: client 및 proxy 가 콘텐츠를 캐싱하지 못하도록 함.
- no-transform: proxy는 콘텐츠를 수정 할 수 없음
nginx proxy에서의 Cache-Control
원본 서버는 보낸 Cache-Control에 대해 중간 nginx proxy는 아래와 같이 Cache 여부 및 TTL을 설정하고, 필요 시 Origin응답을 무시/변경/삭제를 통해 Cache여부 TTL등을 재 설정할 수 있습니다..
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid
Module ngx_http_proxy_module
Module ngx_http_proxy_module The ngx_http_proxy_module module allows passing requests to another server. Example Configuration location / { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } Directives
nginx.org
- 코드 200 및 302의 응답에 대해 10분의 캐싱을 설정하고 코드 404의 응답에 대해 1분을 설정
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m; - 캐싱만 time지정된 경우 200, 301 및 302 응답만 지정한 시간동안 cache 설정
proxy_cache_valid 5m; - any 매개변수를 통해 모든 응답을 캐시하도록 지정
proxy_cache_valid any 1m; - cache TTL 우선 순위
X-Accel-Expires > Cache-Control : max-age > Expires > proxy_cache_valid 순으로 TTL이 지정됩니다. - 헤더에 "Set-Cookie" 필드가 포함된 경우 해당 응답은 캐시되지 않습니다.
- 헤더에 "Vary 필드에 특수 값 " *"이 있는 경우 해당 응답은 캐시되지 않습니다(1.7.7)
- 응답 헤더 필드는 proxy_ignore_headers 지시문을 사용하여 비활성화할 수 있습니다.
Stale Contents제공
콘텐츠의 TTL이 만료되었을 때 원본의 캐시된 콘텐츠의 오래된 버전을 제공하는 방식을 설정할 수 있습니다.
- stale-while-revalidate: Origin에 캐쉬된 콘텐츠의 유효성을 검사하는 동안 오래된 콘텐츠를 제공합니다.
- stale-if-error: Origin에 대한 요청이 실패하면 오래된 콘텐츠를 제공합니다.
해당요청은 조건부 요청으로 주로 If-Modified-Since및 If-None-Match헤더를 사용하여 콘텐츠의 유효성을 확인하게 되고, Origin은 Full 200응답이나 콘텐츠가 변화가 없다면 304 Not Modified 작은 해더응답을 제공합니다.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Conditional_requests
HTTP conditional requests - HTTP | MDN
HTTP has a concept of conditional requests, where the result, and even the success of a request, can be changed by comparing the affected resources with the value of a validator. Such requests can be useful to validate the content of a cache, and sparing a
developer.mozilla.org
원본 서버에서 Cache-Control을 통해 stale 콘텐츠 제어에 대한 설정을 하지 않다라도 nginx proxy에서는 아래 directive 설정를 통해 TTL만료 시 expired contents에 대한 revalidae를 진행할 수 있습니다.
- proxy_cache_revalidate on;
또한 원본에서 stale 관련 해더가 없더라도 nginx proxy에서 원본의 응답코드나 상태에 따라 stale 콘텐트를 제공할 수 있습니다
https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_use_stale
Module ngx_http_proxy_module
Module ngx_http_proxy_module The ngx_http_proxy_module module allows passing requests to another server. Example Configuration location / { proxy_pass http://localhost:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } Directives
nginx.org
- proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 ;
'nginx' 카테고리의 다른 글
nginx upstream module - part1 (load balancing) (0) | 2023.06.15 |
---|---|
nginx cache status 확인 (0) | 2023.06.15 |
cli로 nginx cache file 찾기, 삭제 및 TTL변경 (0) | 2023.06.14 |
nginx cache key설정 (0) | 2023.06.11 |
nginx (web server, proxy, api gateway...) (0) | 2023.06.11 |