Skip to content

Commit

Permalink
nginx.conf group change
Browse files Browse the repository at this point in the history
  • Loading branch information
893271511 committed May 27, 2016
1 parent 7eabf48 commit 3ae7cd9
Show file tree
Hide file tree
Showing 6 changed files with 901 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@ fastdfs开源的分布式文件系统,此脚本利用nginx lua模块,动态
3. [https://github.com/azurewang/lua-resty-fastdfs](https://github.com/azurewang/lua-resty-fastdfs)
4. [http://rhomobi.com/topics/23](http://rhomobi.com/topics/23)
5. [http://bbs.chinaunix.net/thread-4133106-1-1.html](http://bbs.chinaunix.net/thread-4133106-1-1.html)


安装:
CentOS release 6.7 (Final)
Linux 2.6.32-573.26.1.el6.x86_64

http://bitop.luajit.org/download.html
tar xf LuaBitOp-1.0.2.tar.gz
cd LuaBitOp-1.0.2
make
make intall
ll /usr/lib64/lua/5.1/bit.so
yum -y install GraphicsMagick GraphicsMagick-devel
52 changes: 52 additions & 0 deletions conf/conf.d/10.4.81.71.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
server {
listen 999;
server_name 10.4.81.71;
#lua_code_cache off;

location / {
root html;
index index.html index.htm;
}

location /group1 {
#root /data1/fdfs_tracker/data/;
#ngx_fastdfs_module;
alias /data1/group1;

set $image_root "/data1/group1";
if ($uri ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)") {
set $image_dir "$image_root/$2/$3/$4/";
set $image_name "$5";
set $file "$image_dir$image_name";
}

if (!-f $file) {
content_by_lua_file "/usr/local/nginx2/conf/lua/fastdfs.lua";
}
#curl "http://10.4.27.59:777/group1/M00/00/00/CgQbTlaoVpGAKLG7AAAqejxSlcQ450.jpg?attname=filename.jpgg" -I
#curl "http://10.4.27.59:777/group1/M00/00/00/CgQbTlaoVpGAKLG7AAAqejxSlcQ450.jpg?attname=filename.jpg" -I
if ($arg_attname ~ "^(.*).jpg$") {
add_header Content-Disposition "attachment;filename=$arg_attname";
}
}
location /group2 {
#root /data1/fdfs_tracker/data/;
#ngx_fastdfs_module;
alias /data1/group2;

set $image_root "/data1/group2";
if ($uri ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)") {
set $image_dir "$image_root/$2/$3/$4/";
set $image_name "$5";
set $file "$image_dir$image_name";
}

if (!-f $file) {
content_by_lua_file "/usr/local/nginx2/conf/lua/fastdfs.lua";
}
if ($arg_attname ~ "^(.*).jpg$") {
add_header Content-Disposition "attachment;filename=$arg_attname";
}
}
}

23 changes: 23 additions & 0 deletions conf/conf.d/localhost.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
server {
listen 999;
server_name localhost;

location /status1 {
stub_status on;
access_log off;
allow 10.0.0.0/8;
deny all;
}
location /status2 {
check_status;
access_log off;
allow 10.0.0.0/8;
deny all;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}
95 changes: 95 additions & 0 deletions conf/lua/fastdfs.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
-- 写入文件

-- ngx.log(ngx.ERR,"aaaaaaaaaaaaaaaaa",ngx.var.file)
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 )
local response = os.execute( "test -d " .. 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 bit = require('bit')
local fastdfs = require('restyfastdfs')
local fdfs = fastdfs:new()
fdfs:set_timeout(1000)
fdfs:set_tracker_keepalive(0, 100)
fdfs:set_storage_keepalive(0, 100)

local tracker_list = {"10.4.81.71","10.4.81.72"}
for NUMBER,IP in pairs(tracker_list) do
fdfs:set_tracker(IP, 22122)
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)
ngx.log(ngx.DEBUG,"tracker connect:",IP)
break
end
ngx.log(ngx.ERR,"tracker not connect:",IP)
if NUMBER == table.getn(tracker_list) then
ngx.exit(502)
end
end
end

-- 创建缩略图
local image_sizes = {"80x80", "800x600", "40x40", "60x60", "1000x1000"};
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
Loading

0 comments on commit 3ae7cd9

Please sign in to comment.