nginx制作缩略图并配置缓存

缩略图制作:使用image_filter模块

缓存配置: 使用proxy_cache模块

目标:1.后端服务器生成缩略图

 2.前端服务器能够缓存生成的缩略图,并且及时释放,降低访问压力

(ps:我的前后端服务器是一台)

 

开始:

1.后端服务器,进行缩略图制作

 server {
        listen       8081;
        server_name  localhost1;
        location ~* /(.*).png$ {
            expires 1m;
            # 图片服务器端存储地址
            root D://photo;  
            # 图片默认宽度
            set $width -;  
            # 图片默认高度
            set $height -;  
            if ($arg_w != "") {
            set $width $arg_w;
            }
            if ($arg_h != "") {
            set $height $arg_h;
            }
            # 设置图片宽高
            #image_filter size;
            #image_filter crop $width $height;
            image_filter resize $width $height;
            image_filter_jpeg_quality 95;
            # 设置nginx读取图片最大buffer
            image_filter_buffer 100M;
            # 是否开启图片隔行扫描            
            image_filter_interlace on;   
        }

2.前端服务器,进行缩略图缓存

 ##cache 基础配置 在http中 ##
    proxy_connect_timeout 500;
    #跟后端服务器连接的超时时间_发起握手等候响应超时时间
    proxy_read_timeout 600;
    #连接成功后_等候后端服务器响应的时间_其实已经进入后端的排队之中等候处理
    proxy_send_timeout 500;
    #后端服务器数据回传时间_就是在规定时间内后端服务器必须传完所有数据
    proxy_buffer_size 128k;
    #代理请求缓存区_这个缓存区间会保存用户的头信息以供Nginx进行规则处理_一般只要能保存下头信息即可  
    proxy_buffers 4 128k;
    #同上 告诉Nginx保存单个用的几个Buffer最大用多大空间
    proxy_busy_buffers_size 256k;
    #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
    proxy_temp_file_write_size 128k;
    #proxy缓存临时文件的大小
    proxy_temp_path E:\CacheTemp;
    #用于指定本地目录来缓冲较大的代理请求
    proxy_cache_path E:\CacheImage levels=1:2 keys_zone=imgcache:100m inactive=30m max_size=1g;
#图片缓存路径 结构 缓冲区名称:缓冲区大小(100M) 失效时间(30分钟) 最大存储空间(1G)

在 location中使用proxy_pass访问后端服务器:

 location ~* /(.*).png$ {
            expires 1m;
            # 图片服务器端存储地址
            root D://photo;  
            # 图片默认宽度
            set $width -;  
            # 图片默认高度
            set $height -;  
            if ($arg_w != "") {
            set $width $arg_w;
            }
            if ($arg_h != "") {
            set $height $arg_h;
            }
            
            proxy_pass http://后端服务器地址:8081$uri?w=$width;
                
            proxy_redirect off;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_cache imgcache;
            proxy_cache_valid any 1m;
        }

以上。

 

全部配置,仅测试使用,不考虑兼容、健壮:

#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$uri | $args |$width |'
    #access_log  logs/access.log  main;
    rewrite_log on;
    sendfile        on;
    #tcp_nopush     on;
    #keepalive_timeout  0;
    keepalive_timeout  65;

    proxy_connect_timeout 500;
    proxy_read_timeout 600;
    proxy_send_timeout 500;
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_temp_path D:Cache1;
    proxy_cache_path D://Cache levels=1:2 
    keys_zone=imgcache:100m inactive=5m max_size=500M;
    #gzip  on;
    #前端服务器 控制图片缓存内容
    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        access_log  logs/host.access.log  main;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~* /(.*).png$ {
            expires 1m;
            # 图片服务器端存储地址
            root D://photo;  
            # 图片默认宽度
            set $width -;  
            # 图片默认高度
            set $height -;  
            if ($arg_w != "") {
            set $width $arg_w;
            }
            if ($arg_h != "") {
            set $height $arg_h;
            }
            
            proxy_pass http://后端服务器地址:8081$uri?w=$width;
                
            proxy_redirect off;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_cache imgcache;
            proxy_cache_valid 200 302 304 365d;
            proxy_cache_valid 301 1d;
            proxy_cache_valid any 1m;
        }
       
        error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
#后端服务器 进行缩略图制作
    server {
        listen       8081;
        server_name  localhost1;
        location ~* /(.*).png$ {
            expires 1m;
            # 图片服务器端存储地址
            root D://photo;  
            # 图片默认宽度
            set $width -;  
            # 图片默认高度
            set $height -;  
            if ($arg_w != "") {
            set $width $arg_w;
            }
            if ($arg_h != "") {
            set $height $arg_h;
            }
            # 设置图片宽高
            #image_filter size;
            #image_filter crop $width $height;
            image_filter resize $width $height;
            image_filter_jpeg_quality 95;
            # 设置nginx读取图片最大buffer
            image_filter_buffer 100M;
            # 是否开启图片隔行扫描            
            image_filter_interlace on;   
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值