Fastdfs與nginx進(jìn)行壓縮圖片比率
在此先把前面自己做的那個(gè)功能在此分享下,是一個(gè)模仿淘寶的,希望大神們有什么想法可以不吝賜教:
自己是通過(guò)前面的參考配置lua與nginx的結(jié)合使用,利用腳本語(yǔ)言lua的強(qiáng)大特性和nginx的特性來(lái)實(shí)現(xiàn)這個(gè)功能,在nginx.conf的配置文件中加入如下代碼:
server {
listen 22222;
server_name localhost;
# server_name somename alias another.alias;
location /images/{
alias /root/images;
set $image_root /root;
set $file $image_root$uri;
content_by_lua '
ngx.header.content_type = "text/plain";
ngx.say(ngx.var.file);
';
}
location /lua{
set $test "hello, world.";
content_by_lua '
ngx.header.content_type = "text/plain";
ngx.say(ngx.var.test);
';
}
location /group1/M00 {
alias /usr/local/servers/data/fdfs/data;
set $image_root "/usr/local/servers/data/fdfs/data";
# alias /root/images;
# set $image_root "/root/images";
if ( $uri ~ "/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/(.*)" ) {
set $image_dir "$image_root/$3/$4";
set $image_name "$5";
set $file "$image_dir/$image_name";
}
# content_by_lua '
# ngx.header.content_type = "text/plain";
# ngx.say(ngx.var.image_dir);
# ngx.say(ngx.var.image_name);
# ngx.say(ngx.var.file);
# ';
if ( !-f $file ) {
# 關(guān)閉lua代碼緩存,方便調(diào)試lua腳本
#lua_code_cache off;
content_by_lua_file "/usr/local/servers/lua/convert.lua";
}
ngx_fastdfs_module;
}
# location ~ /group[1-3]/M00{
# root /usr/local/servers/data/fdfs/data; #/fdfs/storage/data;
# ngx_fastdfs_module;
# }
}
這里面利用了nginx的正則表達(dá)式,正則表達(dá)式是相當(dāng)強(qiáng)悍的,可以得到你需要訪問(wèn)的uri的值。
然后再convert.lua中寫入如下代碼:
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);
index1 = string.find(area, "([.])");
area1 = string.sub(area, 0, index1-1);
local index2 = string.find(originalFile, "([0-9]+)x([0-9]+)");
originalFile1 = string.sub(originalFile, 0, index2-2)
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, area1) then
local command = "/usr/bin/gm convert " .. originalFile1 .. " -thumbnail " .. area1 .. " -background gray -gravity center -extent " .. area1 .. " " .. ngx.var.file;
os.execute(command);
end;
~差不多這樣就可以實(shí)現(xiàn)功能了,通過(guò)訪問(wèn)可以實(shí)現(xiàn)比率壓縮,因?yàn)樵谏习鄷r(shí)間暫時(shí)就這么寫下了,目前的一個(gè)工作還有很多功能需要實(shí)現(xiàn),如有看不懂的可以留言,大神們可以指教,謝謝!
參考:http://www.v2ex.com/t/113845
http://blog.sina.com.cn/openresty
http://write.blog.csdn.net/postedit
https://github.com/azurewang/lua-resty-fastdfs/blob/master/lib/resty/fastdfs/storage.lua
相關(guān)文章
Nginx 緩存系統(tǒng) proxy_cache工作原理解析
Nginx 的 proxy_cache 模塊允許 Nginx 作為反向代理服務(wù)器時(shí)緩存后端服務(wù)器的響應(yīng),本文給大家介紹Nginx 緩存系統(tǒng) proxy_cache的工作原理,感興趣的朋友跟隨小編一起看看吧2024-12-12
Nginx實(shí)現(xiàn)https網(wǎng)站配置代碼實(shí)例
這篇文章主要介紹了Nginx實(shí)現(xiàn)https網(wǎng)站配置代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
詳解Linux環(huán)境下使Nginx服務(wù)器支持中文url的配置流程
這篇文章主要介紹了Linux環(huán)境下使Nginx服務(wù)器支持中文url的配置流程,文中還介紹了一個(gè)在Linux下將非UTF-8的文件名轉(zhuǎn)換為UTF-8編碼,的方法,需要的朋友可以參考下2016-04-04
當(dāng) Nginx 出現(xiàn) 504 錯(cuò)誤的完美解決方法
Nginx是一款流行的Web服務(wù)器和反向代理服務(wù)器,但有時(shí)會(huì)遇到504網(wǎng)關(guān)超時(shí)錯(cuò)誤,這種錯(cuò)誤通常是由后端服務(wù)器響應(yīng)緩慢、Nginx配置不當(dāng)或網(wǎng)絡(luò)問(wèn)題導(dǎo)致的,下面給大家分享Nginx 出現(xiàn) 504 錯(cuò)誤的完美解決方法,一起看看吧2024-09-09
實(shí)例詳解SpringBoot+nginx實(shí)現(xiàn)資源上傳功能
這篇文章主要介紹了SpringBoot+nginx實(shí)現(xiàn)資源上傳功能,由于小編最近在使用nginx放置靜態(tài)資源問(wèn)題,遇到很多干貨,特此分享到腳本之家平臺(tái),供大家參考,需要的朋友可以參考下2019-10-10

