Skip to content

Instantly share code, notes, and snippets.

@dca
Created January 13, 2015 15:16
Show Gist options
  • Save dca/b66b825ade379f04e520 to your computer and use it in GitHub Desktop.
Save dca/b66b825ade379f04e520 to your computer and use it in GitHub Desktop.
Nginx - Cache & Resize
#keys_zone=cache1:100m 表示这个zone名称为cache1,分配的内存大小为100MB
#/usr/local/nginx/proxy_cache_dir/cache1 表示cache1这个zone的文件要存放的目录
#levels=1:2 表示缓存目录的第一级目录是1个字符,第二级目录是2个字符,即/usr/local/nginx/proxy_cache_dir/cache1/a/1b这种形式
#inactive=1d 表示这个zone中的缓存文件如果在1天内都没有被访问,那么文件会被cache manager进程删除掉
#max_size=10g 表示这个zone的硬盘容量为10GB
proxy_temp_path /usr/share/nginx/html/proxy_temp_dir 1 2;
proxy_cache_path /usr/share/nginx/html/proxy_temp_dir/cache1 levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g;
server {
listen 80;
server_name cache.example.com;
access_log /var/log/nginx/cdn.access.log;
add_header X-Cache $upstream_cache_status;
location ~ .(jpg|JPG|jpeg|JPEG|png|PNG|gif|css|js)$ {
proxy_pass http://127.0.0.1;
proxy_cache cache1;
proxy_cache_key $host$uri$is_args$args;
#only cache 200 304, time 1hr
proxy_cache_valid 200 304 1d;
expires 30d;
}
}
server {
listen 80;
server_name resize.example.com;
location ~* ^/uploads {
set $h '-';
set $w '-';
if ($arg_h != ''){
set $h $arg_h;
}
if ($arg_w != ''){
set $w $arg_w;
}
image_filter resize $w -;
image_filter_jpeg_quality 85;
image_filter_buffer 8M;
proxy_set_header Host $host;
proxy_pass http://example.com;
expires 2h;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment