@@ -46,6 +46,26 @@ img.xxx.com
4646* inotify(可选)
4747
4848## 安装
49+
50+ #### nginx vhost 配置
51+ server {
52+ listen 80;
53+
54+ index index.php index.html index.htm;
55+ set $root_path '/var/www';
56+ root $root_path;
57+
58+
59+ location / {
60+ index index.html index.htm;
61+ }
62+
63+
64+ location /lua {
65+ default_type 'text/plain';
66+ content_by_lua 'ngx.say("hello, ttlsa lua")';
67+ }
68+
4969 #/thumb目录下的图片请求不经过缩略图模块
5070 location ^~ /thumb/ {
5171
@@ -69,4 +89,75 @@ img.xxx.com
6989 set $img_ext $5; # 图片文件格式后缀
7090 content_by_lua_file /etc/nginx/lua/img.lua; # 加载外部 Lua 文件
7191 }
72- }
92+ }
93+
94+ location ~ /\.ht {
95+ deny all;
96+ }
97+ }
98+
99+ #### lua文件
100+ -- 检测路径是否目录
101+ local function is_dir(sPath)
102+ if type(sPath) ~ = "string" then return false end
103+
104+ local response = os.execute("cd " .. sPath)
105+ if response == 0 then
106+ return true
107+ end
108+ return false
109+ end
110+
111+ -- 文件是否存在
112+ function file_exists(name)
113+ local f = io.open(name, "r")
114+ if f ~ = nil then io.close(f) return true else return false end
115+ end
116+
117+ -- 获取文件路径
118+ function getFileDir(filename)
119+ return string.match(filename, "(.+)/[ ^ / ] * %.%w+$") --* nix system
120+ end
121+
122+ -- 获取文件名
123+ function strippath(filename)
124+ return string.match(filename, ".+/([ ^ / ] * %.%w+)$") -- * nix system
125+ end
126+
127+ --去除扩展名
128+ function stripextension(filename)
129+ local idx = filename: match (".+()%.%w+$")
130+ if (idx) then
131+ return filename: sub (1, idx - 1)
132+ else
133+ return filename
134+ end
135+ end
136+
137+ --获取扩展名
138+ function getExtension(filename)
139+ return filename: match (".+%.(%w+)$")
140+ end
141+
142+ -- 开始执行
143+ -- ngx.log(ngx.ERR, getFileDir(ngx.var.file));
144+
145+ local gm_path = '/usr/local/bin/gm'
146+
147+ -- check image dir
148+ if not is_dir(getFileDir(ngx.var.file)) then
149+ os.execute("mkdir -p " .. getFileDir(ngx.var.file))
150+ end
151+
152+ -- 如果原始文件存在,则缩放(以最小边,不变形)
153+ if (file_exists(ngx.var.request_filepath)) then
154+ local cmd = gm_path .. ' convert ' .. ngx.var.request_filepath
155+ cmd = cmd .. " -resize " .. ngx.var.img_width .. "x" .. ngx.var.img_height
156+ cmd = cmd .. " +profile \" * \" " .. ngx.var.file;
157+ ngx.log(ngx.ERR, cmd);
158+ os.execute(cmd);
159+ ngx.exec(ngx.var.uri);
160+ else
161+ ngx.exit(ngx.HTTP_NOT_FOUND);
162+ end
163+
0 commit comments