Nginx as proxy for Amazon S3 public img files

##

一. sample s3 proxy and img_filter (nginx + img_filter)

#####

 

return code:

200 ok
415 file not found

#####

 

nginx config file:
server {
    listen        80;
    server_name  s3.test.com;
    client_max_body_size 40960m;
    access_log /tmp/s3.log main;
    error_log /tmp/s3.err debug;

    location ~ /(.*)!([0-9]+)x([0-9]+)\.(jpg|gif|png|PNG|GIF|JPG)$ {
        set $s3_bucket        'myxx.s3.amazonaws.com';
        set $url_full         '$1.$4';

        proxy_http_version     1.1;
        proxy_set_header       Host $s3_bucket;
        proxy_set_header       Authorization '';
        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_buffering        off;
        proxy_intercept_errors on;

        resolver               8.8.8.8 114.114.114.114 valid=300s;
        resolver_timeout       10s;
        proxy_pass             http://$s3_bucket/$url_full;

        set $w  $2;
        set $h  $3;
        image_filter_jpeg_quality 100;
        image_filter resize $w $h;
        image_filter_buffer 10M;
        expires         120d;
    }
}
  ## 二. if img file in s3, read and img_filter,else read from tfs.xx.com and img_filter (nginx + ngx_lua + img_filter) #####   ##### **return code:** 200 ok 415 file not found #####   ##### **nginx config file:**
lua_package_path '/home/work/app/nginx/conf/vhosts/s3_proxy/lib/?.lua;';
server {
        listen        80;
        server_name  test.test.com;
        client_max_body_size 40960m;
        access_log /tmp/test.log main;
        error_log /tmp/test.err debug;

        location ~ /(.*)!([0-9]+)x([0-9]+)\.(jpg|gif|png|PNG|GIF|JPG)$ {
        resolver               8.8.8.8 114.114.114.114 valid=300s;
        resolver_timeout       10s;
        set $url_full  '$1.$4';
        content_by_lua_file "/home/work/app/nginx/conf/vhosts/s3_proxy/lib/main.lua";
                set $w  $2;
                set $h  $3;
                image_filter_jpeg_quality 100;
                image_filter resize $w $h;
                image_filter_buffer 10M;
                expires         120d;
        }
}
lua code:
local ngx = ngx
local http = require "resty.http"

local s3_domain = 'myxx.s3.amazonaws.com'
local tfs_domain = 'tfs.xx.com'

if not ngx.var.url_full then
    ngx.log(ngx.ERR, 'nginx config not url_full value')
    ngx.exit(ngx.HTTP_FORBIDDEN)
end
local img_url = ngx.var.url_full
local http_s3 = http.new()
http_s3:set_timeout(5000)
local res, err = http_s3:request_uri("http://"..s3_domain.."/"..img_url, {
    method = "GET",
    headers = {}   
})
http_s3:close()
if res.status == 200 then
    ngx.status = 200
    ngx.say(res.body)
elseif res.status == 404 then
    local http_tfs = http.new()
    http_tfs:set_timeout(5000)
    local res, err = http_tfs:request_uri("http://"..tfs_domain.."/"..img_url, {
        method = "GET",
        headers = {}   
    })
    http_tfs:close()
    if res.status == 200 then
        ngx.status = 200
        ngx.say(res.body)
    else
        ngx.status = res.status
        ngx.exit(ngx.status)
    end
else
    ngx.log(ngx.ERR, err)
    ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
end
require:

https://github.com/pintsized/lua-resty-http