首页 > 系统相关 >nginx-lua-fastdfs-GraphicsMagick整合

nginx-lua-fastdfs-GraphicsMagick整合

时间:2023-04-25 17:03:31浏览次数:52  
标签:end string GraphicsMagick -- ngx fastdfs nginx var local


    无意发现了一个不错的分布式文件系统。 fastdfs开源的分布式文件系统,此脚本利用nginx lua模块,动态生成图片缩略图,fastdfs只存一份原图。lua通过socket获取fastdfs的原图,并存放到本地,根据不同规则url,例如:_60x60.jpg、_80x80.jpg,类似淘宝图片url规则。利用gm命令生成本地缩略图,第二次访问直接返回本地图片。定时任务凌晨清除7天内未访问的图片,节省空间


参考:https://github.com/hpxl/nginx-lua-fastdfs-GraphicsMagick


-- 写入文件
local function writefile(filename, info)
    local wfile=io.open(filename, "w") --写入文件(w覆盖)
    assert(wfile)  --打开时验证是否出错		
    wfile:write(info)  --写入传入的内容
    wfile:close()  --调用结束后记得关闭
end

-- 检测路径是否目录
local function is_dir(sPath)
    if type(sPath) ~= "string" then return false end

    local response = os.execute( "cd " .. sPath )
    if response == 0 then
        return true
    end
    return false
end

-- 检测文件是否存在
local file_exists = function(name)
    local f=io.open(name,"r")
    if f~=nil then io.close(f) return true else return false end
end

local area = nil
local originalUri = ngx.var.uri;
local originalFile = ngx.var.file;
local index = string.find(ngx.var.uri, "([0-9]+)x([0-9]+)");  
if index then 
    originalUri = string.sub(ngx.var.uri, 0, index-2);  
    area = string.sub(ngx.var.uri, index);  
    index = string.find(area, "([.])");  
    area = string.sub(area, 0, index-1);  

    local index = string.find(originalFile, "([0-9]+)x([0-9]+)");  
    originalFile = string.sub(originalFile, 0, index-2)
end

-- check original file
if not file_exists(originalFile) then
    local fileid = string.sub(originalUri, 2);
    -- main
    local fastdfs = require('restyfastdfs')
    local fdfs = fastdfs:new()
    fdfs:set_tracker("192.168.1.113", 22122)
    fdfs:set_timeout(1000)
    fdfs:set_tracker_keepalive(0, 100)
    fdfs:set_storage_keepalive(0, 100)
    local data = fdfs:do_download(fileid)
    if data then
       -- check image dir
        if not is_dir(ngx.var.image_dir) then
            os.execute("mkdir -p " .. ngx.var.image_dir)
        end
        writefile(originalFile, data)
    end
end

-- 创建缩略图
local image_sizes = {"80x80", "800x600", "40x40", "60x60"};  
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, area) then  
    local command = "gm convert " .. originalFile  .. " -thumbnail " .. area .. " -background gray -gravity center -extent " .. area .. " " .. ngx.var.file;  
    os.execute(command);  
end;

if file_exists(ngx.var.file) then
    --ngx.req.set_uri(ngx.var.uri, true);  
    ngx.exec(ngx.var.uri)
else
    ngx.exit(404)
end




标签:end,string,GraphicsMagick,--,ngx,fastdfs,nginx,var,local
From: https://blog.51cto.com/u_16088628/6224589

相关文章

  • Nginx + Nacos2.x集群配置
    Nginx:#集群配置http{upstreamnacos-cluster{ip_hash;server127.0.0.1:8858;server127.0.0.1:8868;server127.0.0.1:8878;}server{listen8838;server_namelocalhost;}location/nacos{ ......
  • nginx负载均衡
     //默认为轮询,权重默认值为1upstreamservers{server192.168.1.101:80weight=4down;//不参与随机server192.168.1.102:80weight=2;server192.168.1.102:80weight=1backup;//备用}ip_hash:根据客户端的IP地址转发同一台服务器,可以保存会话。least_co......
  • Ubuntu 22.04.1 LTS 编译安装 nginx-1.23.4
    一、安装环境依赖sudoapt-getinstallg++sudoapt-getinstallopenssllibssl-devsudoapt-getinstalllibpcre3libpcre3-devsudoapt-getinstallzlib1g-devsudoapt-getinstalllibgd-dev 二、下载安装包wgethttp://nginx.org/download/nginx-1.23.4.tar.gz......
  • Nginx配置vue项目模版
    1、hash模式时(#),nginx配置方法location~^/test/cms-h5/{rewrite^/test/cms-h5(.*)$$1break;root/opt/nginx/web/cms-h5/dist;}2、hisotry模式时,nginx配置方法location~^/test/cms-h5/{try_files$uri@cms-h5;}location@cms......
  • Nginx配置跳转HTTPS方法汇总
    1、采用nginx的rewrite方法#server{listen80;server_namedev.herlly.com;indexindex.htmlindex.phpindex.htm;access_log/usr/local/nginx/logs/8080-access.logmain;error_log/usr/local/nginx/logs/8080-error.log;rewrite^(.*)$......
  • Nginx实现流量复制
    1、简介Nginx自1.13.4开始引入nginx_mirror_module模块,利用此模块可以将线上实时流量镜像至其他环境,而Nginx最终会丢弃mirror的响应,从而不影响源站请求的响应。2、配置upstreambackend{serverbackend.server:10000;}upstreamtest_backend{servertest.server:......
  • nginx解决Ajax跨域问题
    今天遇到一个ajax跨域问题,下拉框的数据源要从一个接口获得,但是该接口被部署到另外一台服务器上,在本地可以通过http请求访问,并可以返回json的数据,但是放到页面中不可以获取到下拉框的值,发现chrome控制台中该请求成功,但是没有返回值,于是便遇到了跨域的问题,请教一同事,问题得到解决:1.搭......
  • Nginx常用配置及和基本功能讲解
    作者:京东物流 殷世杰Nginx已经广泛应用于J-one和Jdos的环境部署上,本文对Nginx的常用的配置和基本功能进行讲解,适合Nginx入门学习。1核心配置找到Nginx安装目录下的conf目录下nginx.conf文件,Nginx的基本功能配置是由它提供的。Nginx的配置文件(conf/nginx.conf)整体上分为如下几个......
  • LINUX安装nginx详细步骤
    1.安装依赖包//一键安装上面四个依赖yum-yinstallgcczlibzlib-develpcre-developensslopenssl-devel2.下载并解压安装包cd/usr/localmkdirnginxcdnginx//下载tar包wgethttp://nginx.org/download/nginx-1.13.7.tar.gztar-xvfnginx-1.13.7.tar.gz3.安装n......
  • nginx - 反向代理tcp地址
    在http同级添加红色部分即可#tcp配置stream{server{listen9101;proxy_pass127.0.0.1:8080;}}.........http{......}stream的端口不可与http共用,需要单独占用一个新的......