server { listen 80; server_name test.anerg.com; root /data/www/imgtest; index default.html default.php index.php index.html; location /images/ { set $needCreateImg 0; if (!-f $request_filename) { set $needCreateImg "${needCreateImg}1"; } if ($uri ~* "/images/([0-9a-zA-Z]+)_(\d+x\d+).(gif|jpg|jpeg|png)") { set $needCreateImg "${needCreateImg}2"; } if ($needCreateImg = "012") { set $filePath "$document_root/images/$1_$2.$3"; set $originalPath "$document_root/images/$1.$3"; set $fileSize "$2"; set $fileExt "$3"; set $reqFile "$1_$2.$3"; rewrite_by_lua ' function file_exists(name) local f=io.open(name,"r") if f~=nil then io.close(f) return true else return false end end local image_sizes = {"155x155", "400x400","104x104", "50x50", "40x40", "56x56", "172x172","800x600"}; function table.contains(table, element) for _, value in pairs(table) do if value == element then return true end end return false end if table.contains(image_sizes, ngx.var.fileSize) and file_exists(ngx.var.originalPath) then local command = "gm convert " .. ngx.var.originalPath .. " -thumbnail " .. ngx.var.fileSize .. " -background white -gravity center -extent " .. ngx.var.fileSize .. " " .. ngx.var.filePath; os.execute(command); ngx.req.set_uri("/images/" .. ngx.var.reqFile, true); else ngx.exit(404); end; '; } } location = /favicon.ico { log_not_found off; access_log off; } }