본문 바로가기

프로그래밍/Java

Nginx 캐시

728x90
반응형

nginx.conf 설정에 캐시 저장소 설정

proxy_cache_path /home/suser/cache levels=1:2 keys_zone=cache_zone:10m max_size=100m inactive=7d use_temp_path=off;

nginx server 설정

    location ~ /v1/board/event/(front|detail) {
        proxy_cache cache_zone;
        proxy_buffering on;
        proxy_ignore_headers Expires;
        proxy_ignore_headers X-Accel-Expires;
        proxy_ignore_headers Cache-Control;
        proxy_ignore_headers Set-Cookie;

        proxy_hide_header X-Accel-Expires;
        proxy_hide_header Expires;
        proxy_hide_header Cache-Control;
        proxy_hide_header Pragma;

        #proxy_cache_methods GET HEAD POST;
        proxy_cache_key "$scheme$request_method$host$uri$is_args$args";
        proxy_cache_bypass $cookie_nocache $arg_nocache $http_pragma;

        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 404 1m;
        add_header X-Cache-Status $upstream_cache_status;
        proxy_pass http://127.0.0.1:18081;
    }

nginx 재기동

  • systemctl restart nginx

캐시 HIT 확인

curl -XGET -I "http://127.0.0.1/v1/board/event/detail?eventid=105"
HTTP/1.1 200
Server: nginx
Date: Fri, 13 May 2022 04:39:54 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Cache-Status: EXPIRED
  
curl -XGET -I "http://127.0.0.1/v1/board/event/detail?eventid=105"
HTTP/1.1 200
Server: nginx
Date: Fri, 13 May 2022 04:39:56 GMT
Content-Type: application/json
Transfer-Encoding: chunked
Connection: keep-alive
X-Cache-Status: HIT
728x90
반응형