pushiqiang的博客

python技术爱好者,python开发工程师,Patrick

使用Nginx代理s3,动态生成缩略图并缓存

使用Nginx代理s3,动态生成缩略图并缓存

通过 {domain}/{uri}?s={size}实现获取指定大小缩略图

原图 eg:localhost/u/1523562/avatar

缩略图 eg:localhost/u/1523562/avatar?s=200

缩略图 eg:localhost/u/1523562/avatar?s=100

github : https://github.com/pupatrick/nginx_s3_thumbnail

实现

nginx.conf

Nginx 1.9.11开始增加加载动态模块支持,从此不再需要替换nginx文件即可增加第三方扩展。目前官方只有几个模块支持动态加载,查看支持的动态模块:

root@121b5dd70a5c:~# ls -l /etc/nginx/modules/
total 540
-rw-r--r-- 1 root root  20656 May 31  2016 ngx_http_geoip_module-debug.so
-rw-r--r-- 1 root root  20656 May 31  2016 ngx_http_geoip_module.so
-rw-r--r-- 1 root root  24000 May 31  2016 ngx_http_image_filter_module-debug.so
-rw-r--r-- 1 root root  24000 May 31  2016 ngx_http_image_filter_module.so
-rw-r--r-- 1 root root 176232 May 31  2016 ngx_http_js_module-debug.so
-rw-r--r-- 1 root root 180328 May 31  2016 ngx_http_js_module.so
-rw-r--r-- 1 root root  24048 May 31  2016 ngx_http_perl_module-debug.so
-rw-r--r-- 1 root root  24048 May 31  2016 ngx_http_perl_module.so
-rw-r--r-- 1 root root  19888 May 31  2016 ngx_http_xslt_filter_module-debug.so
-rw-r--r-- 1 root root  19888 May 31  2016 ngx_http_xslt_filter_module.so

在nginx.conf配置文件中加载image-filter模块

# 加载image-filter模块
load_module /etc/nginx/modules/ngx_http_image_filter_module.so;

...

http {

    ...

    proxy_cache_lock on;
    proxy_cache_lock_timeout 60s;
    proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=s3cache:10m max_size=10g;
    # place used to buffer large proxied requests to the fs
    proxy_temp_path /var/cache/nginx/tmp;

    ...

    include /etc/nginx/conf.d/*.conf;
}

media.conf

/etc/nginx/conf.d/media.conf


server {
    listen 80;
    listen [::]:80;

    server_tokens off;
    server_name  localhost; # 你的域名

    location / {
        set $backend 'your.s3.bucket_name.s3.amazonaws.com';

        proxy_cache s3cache;
        proxy_cache_key "$host$uri$is_args$arg_s";   # 需要优化

        # cache successful responses for 24 hours
        proxy_cache_valid  200 302  24h;
        # cache missing responses for 1 minutes
        proxy_cache_valid  403 404 415 1m;

        proxy_redirect off;
        # need to set the hot to be $backend here so s3 static website hosting service knows what bucket to use
        proxy_set_header        Host $backend;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        add_header              X-Cache-Status $upstream_cache_status;
        proxy_hide_header       x-amz-id-2;
        proxy_hide_header       x-amz-request-id;
        proxy_hide_header       Set-Cookie;
        proxy_ignore_headers    "Set-Cookie";
        proxy_intercept_errors on;

        resolver 8.8.8.8;
        resolver_timeout 5s;

        # querystring带有合法的 s 参数的请求代理到缩略图服务器
        if ($arg_s ~* "^([0-9]+)$") {
            proxy_pass  http://127.0.0.1:10199;
        }

        proxy_pass http://$backend;
    }
}

thumbnail.conf

/etc/nginx/conf.d/thumbnail.conf


server {
    listen 10199;

    server_tokens off;
    server_name  127.0.0.1;

    image_filter_buffer 100M;   # 设置读取图像缓冲的最大大小,超过则415错误。

    location / {
        set $backend 'your.s3.bucket_name.s3.amazonaws.com';

        proxy_redirect off;
        # need to set the hot to be $backend here so s3 static website hosting service knows what bucket to use
        proxy_set_header        Host $backend;
        proxy_set_header        X-Real-IP $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        add_header              X-Cache-Status $upstream_cache_status;
        proxy_hide_header       x-amz-id-2;
        proxy_hide_header       x-amz-request-id;
        proxy_hide_header       Set-Cookie;
        proxy_ignore_headers    "Set-Cookie";
        proxy_intercept_errors on;

        resolver 8.8.8.8;
        resolver_timeout 5s;

        image_filter resize $arg_s $arg_s;

        proxy_pass http://$backend;

        allow 127.0.0.1;
        deny all;
    }
}

参考: https://segmentfault.com/a/1190000004604380

阅读更多
文章标签: nginx thumbnail
个人分类: nginx
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

使用Nginx代理s3,动态生成缩略图并缓存

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭