使用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;

...
...
...

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

猜你喜欢

转载自blog.csdn.net/pushiqiang/article/details/80319745