Nginx proxy cache를 이용하니 이미지 캐시서버 구축이 훨씬 쉬워지네요.
https://www.wsgvet.com/web/243
여기에 정리해뒀습니다.
---
php도 필요없고 그냥 서버에 Nginx 딱 1개만 설치하면 됩니다. (SSL 인증서 발급은 먼저 하시구요)
그리고 설정파일에
[code]
proxy_cache_path /var/cache levels=1:2 keys_zone=static:100m max_size=10g inactive=30d;
proxy_cache_key "$scheme$request_method$host$request_uri";
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name cdn.example.com; # 이미지 캐시서버 도메인
server_tokens off;
location ~* \.(?:css|js|gif|png|jpg|jpeg|mp4|webm)$ {
valid_referers none blocked *.example.com example.com; # 본서버 외 불펌금지
if ($invalid_referer) {
return 403;
}
proxy_pass https://example.com; # 본서버 도메인
proxy_cache_valid 200 301 302 600m;
proxy_cache static;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_revalidate on;
proxy_cache_lock on;
proxy_ignore_headers Set-Cookie;
access_log off;
add_header My-Cache-Status $upstream_cache_status;
add_header my-ray "KR";
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
}
location / {
return 403;
}
ssl_certificate ssl/fullchain.pem; #자신의 인증서 경로로..
ssl_certificate_key ssl/privkey.pem; #자신의 인증서 경로로..
ssl_trusted_certificate ssl/chain.pem; #자신의 인증서 경로로..
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 10s;
}
[/code]
위와 같이 셋팅하고 Nginx를 재시작해줍니다.
마지막으로 본서버에 가서 캐시하고 싶은 파일들의 경로를 캐시서버 도메인으로 바꿉니다.
그러면 알아서 캐시됩니다.
정말 쉽죠?
도커로 SSL 인증서 자동화까지 합쳐서 스크립트로 만들어봐야겠네요.
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기
댓글 8개