NGINX Cache
- Official URL
- Guide Docs
NGINX Cache 동작 - Process관점
Cache Manager
- Cache 관리 등등 활용
- Cache Delete 등등도 활용
Cache Loader
- 디스크의 캐시 파일을 반복하여 캐싱 메타데이터를 메모리에 로드 수행
Image
NGINX Cache 동작 - caching Data 관점
DATA 영역에서의 관점
- Content 저장 Disk
- Hash Cached 저장 : RAM
Working Flow
Client -> NGINX Server Access (First Access) -> No Cached Data Client -> NGINX Server -> Backend Server Access Client Received Data <- NGINX Server <- Data Send Client -> NGINX Server Access (Second Access / Same Request ) -> NGINX Generator Hash (Shared Memory) -> Hash Key -> Cached Data -> Client Received
Image
NGINX Cache 기본 설정
- HTTP Context : proxy_cache_path 에 Cache 설정
- HTTP / Server / Location : proxy_cache 설정 ( Cache Enable )
NGINX Cache Options
NGINX Cache Options - Proxy_cache_path
- Sample : proxy_cache_path /path/to/cache levels=1:2 keys_zone=my_cache:10m max_size=10g inactive=60m use_temp_path=off;
- Options
- /path/to/cache → Cache 저장할 위치 ( NGINX User R/W 가능하여야 함 )
- levels=1:2 → Disk 에서 Cache 파일구조 정의 ( 디렉토리갯수 : 계층(Depth) )
- keys_zone=mycache:10m → Cache Memory 이름 및 용량 설정 (Shared Memory)
- max_size=10g → 디스크의 최대 캐시 크기 정의
- inactive=60m → 해당 시간 이후, Cache Data 자동 삭제 ( Default :10m)
- use_temp_path=off → 임시 디렉토리에 먼저 저장 설정하는 기능 ( Default : Off )
off : 임시 파일을 캐시 디렉터리에 직접 저장 / On : 지정된 위치에 proxy_temp_path 에 의해 설정된 디렉토리 사용 - cache Loader : SharedMemory 에 저장된 기존 메터데이터 불러옴
- loader_threshold → 요청에 대한 최대 대기 시간을 지정(ms)
- loader_files → 한번의 호출에 불러오는 파일 갯수 지정
- loader_sleeps → 호출 간격 (ms)
- Cache Manager : 최대 가용한 사이즈(max_size)에 도달 시 기존 캐시 컨텐츠의 삭제 관리
- manager_files → 한번 수행 시 항목의 제거 ( Default : 100 EA )
- manager_threshold → 한번 수행 시 지속시간을 설정
- manager_sleep → 반복 실행 간의 Sleep 시간 설정
- Cache Purger : 캐시된 컨텐츠 기반의 API 삭제 수행
- purger → 정의된 Key 와 일치하는 Cache항목이 디스크에서 제거되는지 여부를 확인
- purger_files → 한번 실행 시, 검색하는 항목 수 설정
- purger_threshold → 한번 실행 시, 지속 시간 설정
- purger_sleep → 반복 실행 간의 Sleep 시간 설정
NGINX Cache Options - proxy_cache_key
- Caching 을 위한 Key 를 정의함
- Default : proxy_cache_key $scheme$proxy_host$request_uri;
NGINX Cache Status Check - CURL & Header
- CURL -I
- HIT / Miss 확인
- Header 추가로 확인 가능 #Trouble Shoot 에 유용함
add_header X-Cache-Status $upstream_cache_status;
$upstream_cache_status 값
MISS – The response was not found in the cache and so was fetched from an origin server. The response might then have been cached.
BYPASS – The response was fetched from the origin server instead of served from the cache because the request matched a proxy_cache_bypass directive (see Can I Punch a Hole Through My Cache? below.) The response might then have been cached.
EXPIRED – The entry in the cache has expired. The response contains fresh content from the origin server.
STALE – The content is stale because the origin server is not responding correctly, and proxy_cache_use_stale was configured.
UPDATING – The content is stale because the entry is currently being updated in response to a previous request, and proxy_cache_use_stale updating is configured.
REVALIDATED – The proxy_cache_revalidate directive was enabled and NGINX verified that the current cached content was still valid (If-Modified-Since or If-None-Match).
HIT – The response contains valid, fresh content direct from the cache.
- (Plus) DashBoard 확인 가능
CAP
NGINX Sample Config
Case1 - Default NGINX Cache Config
Default NGINX Cache Config
Config
log_format caching '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$upstream_cache_status"'; proxy_cache_path /etc/nginx/cache keys_zone=nginx_cache:100m; upstream upstream_test_pool { zone upstream_test_pool 128k; server 10.250.11.101:80; server 10.250.11.102:80; server 10.250.11.201:80; server 10.250.11.202:80; } server { listen 80 default_server; server_name localhost; proxy_cache nginx_cache; access_log /var/log/nginx/host.access.log main; location / { proxy_pass http://upstream_test_pool; health_check; } }
Case2 - Logging Cache & Advanced Config
Cache Config 수정
Config
log_format caching '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$upstream_cache_status"'; proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=nginx_cache:100m inactive=10m max_size=200m; upstream upstream_test_pool { zone upstream_test_pool 128k; server 10.250.11.101:80; server 10.250.11.102:80; server 10.250.11.201:80; server 10.250.11.202:80; } server { listen 80 default_server; server_name localhost; location / { access_log /var/log/nginx/host.access.log caching; proxy_cache nginx_cache; proxy_cache_valid 5m; add_header X-Cache-Status $upstream_cache_status; proxy_pass http://upstream_test_pool; health_check; } }
NGINX Cache Monitoring
NGINX Plus - API Cache Status Check
API 를 통한 Cache 상태 확인
Code
curl 127.0.0.1:8080/api/8/http/caches | jq % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 458 100 458 0 0 804k 0 --:--:-- --:--:-- --:--:-- 447k { "nginx_cache": { "size": 0, "max_size": 209715200, "cold": false, "hit": { "responses": 5411, "bytes": 3684891 }, "stale": { "responses": 0, "bytes": 0 }, "updating": { "responses": 0, "bytes": 0 }, "revalidated": { "responses": 0, "bytes": 0 }, "miss": { "responses": 1270, "bytes": 30434706, "responses_written": 3, "bytes_written": 9841859 }, "expired": { "responses": 2, "bytes": 1362, "responses_written": 2, "bytes_written": 1362 }, "bypass": { "responses": 0, "bytes": 0, "responses_written": 0, "bytes_written": 0 } } } [root@
NGINX Plus - Cache Purge
Cached 된 DATA 에 대해 Purge 기능 수행
NGINX Config
log_format caching '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$upstream_cache_status"'; proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=nginx_cache:100m inactive=5m max_size=200m; upstream upstream_test_pool { zone upstream_test_pool 128k; server 10.250.11.101:80; server 10.250.11.102:80; server 10.250.11.201:80; server 10.250.11.202:80; } map $request_method $purge_method { PURGE 1; default 0; } server { listen 80 default_server; server_name localhost; location / { access_log /var/log/nginx/host.access.log caching; proxy_pass http://upstream_test_pool; health_check; ## Cache Options proxy_cache nginx_cache; proxy_cache_valid 60m; ## 200,301,302 Code - 5Min add_header X-Cache-Status $upstream_cache_status; ## $upstream_cache_status : Cache Status Check 가능 proxy_cache_purge $purge_method; } }
NGINX Cache Data Check
# tree . └── 1 └── c6 └── 9f64efd2a14c3bc10425bdba56f21c61 2 directories, 1 file # cd 1/c6 [root@sglee c6]# ll total 4 -rw------- 1 nginx nginx 1283 Apr 7 09:47 9f64efd2a14c3bc10425bdba56f21c61 [root@sglee c6]#
NGINX Cache Purge
curl -X PURGE 10.250.11.97
NGINX Cache Data Remove Check
# tree . └── 1 └── c6 2 directories, 0 files # date Fri Apr 7 09:48:27 KST 2023
NGINX Cache Data Remove Check Logging (Access LOG)
127.0.0.1 - - [07/Apr/2023:09:47:17 +0900] "GET / HTTP/1.1" 200 681 "-" "curl/7.29.0" "-" "HIT" 10.250.11.97 - - [07/Apr/2023:09:48:11 +0900] "PURGE / HTTP/1.1" 204 0 "-" "curl/7.29.0" "-" "-" 127.0.0.1 - - [07/Apr/2023:09:49:09 +0900] "GET / HTTP/1.1" 200 681 "-" "curl/7.29.0" "-" "MISS" 127.0.0.1 - - [07/Apr/2023:09:49:10 +0900] "GET / HTTP/1.1" 200 681 "-" "curl/7.29.0" "-" "HIT"