nginx lua处理图片

nginx配置
user  apache apache;
worker_processes 4;
worker_rlimit_nofile 100000;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
use epoll;
multi_accept on;
worker_connections 51200;
}

# load modules compiled as Dynamic Shared Object (DSO)
#
dso {
# load ngx_http_fastcgi_module.so;
# load ngx_http_rewrite_module.so;
}

http
{
server_tokens off;
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 20;

server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_body_buffer_size 512k;
client_max_body_size 20m;
gzip on;
gzip_disable "msie6";
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_http_version 1.1;
gzip_comp_level 6;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript;
gzip_vary on;

lua_package_path '/usr/local/tengine/lua/?.lua;;';

upstream tomcat_server {
ip_hash;
server 172.16.151.112:6080 ;
server 172.16.151.113:6080 ;
# server 172.16.151.235 max_fails=2 fail_timeout=30s;
}


server {
listen 80;
server_name image0.lovebuy.com.cn;
# root /usr/html/lovebuy;


location = / {
root /usr/html/lovebuy/html;
index index.html;
}

location ~ ^/index.html$ {
root /usr/html/lovebuy/html;
}

location ^~ /subject/ {
root /usr/html/lovebuy/html;
}

# location ~ ^/goods_(\d\d\d).html$ {
#
# if (-f $request_filename) {
# root /usr/html/lovebuy/html;
# rewrite ^/goods_(\d\d).html$ /page0$1/goods_0$1.html break;
# rewrite ^/goods_(\d+).html$ /page$1/goods_$1.html break;
# }
#
# if (!-f $request_filename) {
# # rewrite ^/goods_(\d+).html http://www.lovebuy.com.cn/goods_3784.html permanent;
# rewrite ^/goods_(\d+).html http://172.16.151.112:6080/goods_$1.htm permanent;
# }
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_pass http://tomcat_server;
# }


location ~ ^/goods_(.*)(\d\d\d).html$ {

root /usr/html/lovebuy/html/page$2/;

if (-f $request_filename) {
rewrite ^/goods_(.*)(\d\d\d).html$ /goods_$1$2.html break;
}

if (!-f $request_filename) {
rewrite ^/goods_(\d+).html http://$host/goods_$1.htm ;
# proxy_pass http://tomcat_server ;
}
# proxy_set_header Host $host;
# proxy_set_header X-Forwarded-For $remote_addr;
# proxy_pass http://tomcat_server;
}

# location ~ ^/goods_(.*)(\d\d\d).html$ {
#
# root /usr/html/lovebuy/html;
# rewrite ^/goods_(.*)(\d\d\d).html$ /page$2/goods_$1$2.html break;
# }


#/thumbnail目录下的图片请求不经过缩略图模块
location ^~ /thumbnail/ {
root /usr/html/lovebuy/upload;

}


location /lua {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
}

#########################################################
#对类似_100x100.gif/jpg/png/jpeg进行缩略图处理
location ~* _([0-9]+)x([0-9]+)\.(gif|jpg|png|jpeg)$ { #匹配文件名规则
# location ^~ /photo/ { #匹配文件名规则
rewrite ^/(upload/)(.*)$ /$2 last;
root /usr/html/lovebuy/upload; #点根目录
lua_code_cache off;
set $image_root /usr/html/lovebuy/upload; #图片目录
set $thumbnail_root /usr/html/lovebuy/upload/thumbnail; #缩略图存放目录
#果缩略图文件存在,直接返回
set $file $thumbnail_root$uri;
if (-f $file) {
rewrite ^/(.*)$ /thumbnail/$1 last;
}

#果缩略图文件不存在,则应用缩略图模块处理
if (!-f $file) {
rewrite_by_lua_file /usr/local/tengine/lua/thumbnail.lua;
# rewrite_by_lua '
# local command = "/usr/local/GraphicsMagick/bin/gm convert /usr/local/tengine/html/image/photo/2m.jpg /usr/local/tengine/html/image/photo/2m_300x300.jpg" ;
# os.execute(command);

# ';
}

}
location /nginx-status {
stub_status on;
access_log off;
#加入访问限制
#allow 60.195.252.106;
allow 172.16.151.83;
#deny all;
}
##################缓存配置###############################3
location ~ \.(gif|jpg|jpeg|png|bmp|ico)$ {
root /usr/html/lovebuy/;
expires 1d;

}

location ^~ /resources/ {
root /usr/html/lovebuy/;
expires 30d;

}


###################缓存配置-end##########################
location / {
concat on; #开启concat模块
root /usr/html/lovebuy/;
}

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


lua文件
-- nginx thumbnail module 
-- last update : 2014/8/21
-- version : 0.4.1

local c = require 'config'

--[[
uri :链接地址,如/goods/0007/541/001_328x328.jpg
ngx_img_root :图片根目录
ngx_thumbnail_root:缩略图根目录
img_width :缩略图宽度
img_width :缩略图高度
img_size :缩略图宽x高
img_crop_type :缩略图裁剪类型
cur_uri_reg_model :缩略图uri正则规则
]]
local uri = ngx.var.uri
local ngx_img_root = ngx.var.image_root
local ngx_thumbnail_root = ngx.var.thumbnail_root
local img_width,img_height,img_size,img_crop_type = 0
local cur_uri_reg = c.default_uri_reg

--[[
日志函数
log_level: 默认为ngx.NOTICE
取值范围:ngx.STDERR , ngx.EMERG , ngx.ALERT , ngx.CRIT , ngx.ERR , ngx.WARN , ngx.NOTICE , ngx.INFO , ngx.DEBUG
请配合nginx.conf中error_log的日志级别使用
]]
function lua_log(msg,log_level)
log_level = log_level or c.lua_log_level
if(c.enabled_log) then
ngx.log(log_level,msg)
end
end

-- 匹配链接对应缩略图规则
function table.contains(table,element)
local i = 1
img_crop_type = 0
for _, value in pairs(c.cfg) do
local dir = value['dir']
local sizes = value['sizes']
local uri_reg = value['uri_reg']
_,_,img_width,img_height = string.find(uri,''..dir..'+.*_([0-9]+)x([0-9]+)')
if(img_width and img_height and img_crop_type==0) then
img_size = img_width..'x'..img_height
for _, value in pairs(sizes) do
if(uri_reg) then
lua_log('value[uri_reg]==='..uri_reg)
else
lua_log('value[uri_reg]===nil,dir='..dir..',cur_uri_reg='..cur_uri_reg)
end
cur_uri_reg = uri_reg or cur_uri_reg
if (img_size == value) then
img_crop_type=1
return true
elseif (img_size..'_' == value) then
img_crop_type=2
return true
elseif (img_size..'!' == value) then
img_crop_type=3
return true
elseif (img_size..'^' == value) then
img_crop_type=4
return true
elseif (img_size..'>' == value) then
img_crop_type=5
return true
elseif (img_size..'$' == value) then
img_crop_type=6
img_size = img_width..'x'
return true
end
end
end
i=i+1
end
return false
end

-- 拼接gm命令
local function generate_gm_command(img_crop_type,img_original_path,img_size,img_thumbnail_path)
local cmd = c.gm_path .. ' convert ' .. img_original_path
if (img_crop_type == 1) then
cmd = cmd .. ' -thumbnail ' .. img_size .. ' -background ' .. c.img_background_color .. ' -gravity center -extent ' .. img_size
elseif (img_crop_type == 2) then
cmd = cmd .. ' -thumbnail "' .. img_size .. '" +profile "*"'
elseif (img_crop_type == 3) then
cmd = cmd .. ' -thumbnail "' .. img_size .. '!" -extent ' .. img_size
elseif (img_crop_type == 4) then
cmd = cmd .. ' -thumbnail "' .. img_size .. '^" -extent ' .. img_size
elseif (img_crop_type == 5 or img_crop_type == 6) then
cmd = cmd .. ' -thumbnail "' .. img_size .. '>" +profile "*"'
else
lua_log('img_crop_type error:'..img_crop_type,ngx.ERR)
ngx.exit(404)
end
cmd = cmd .. ' ' .. img_thumbnail_path
return cmd
end

lua_log("ngx_thumbnail_root======="..ngx_thumbnail_root)
--对照配置文件规定的图片尺寸
if not table.contains(c.cfg, uri) then
lua_log(uri..' is not match!',ngx.ERR)
ngx.exit(404)
else
lua_log(uri..' is match!',ngx.ERR)
local img_original_uri = string.gsub(uri, cur_uri_reg, '')
lua_log('img_original_uri_old===' .. uri,ngx.ERR)
lua_log('cur_uri_reg===' .. cur_uri_reg,ngx.ERR)
lua_log('img_original_uri_new===' .. img_original_uri,ngx.ERR)
local img_exist=io.open(ngx_img_root .. img_original_uri)
if not img_exist then
if not c.enabled_default_img then
lua_log(img_original_uri..' is not exist!')
ngx.exit(404)
else
img_exist=io.open(ngx_img_root .. c.default_img_uri)
lua_log('img_exist='..img_exist,ngx.ERR)
if img_exist then
lua_log(img_original_uri .. ' is not exist! crop image with default image')
img_original_uri = c.default_img_uri
else
lua_log(img_original_uri..' is not exist!')
ngx.exit(404)
end
end
end

local img_original_path = ngx_img_root .. img_original_uri
local img_thumbnail_path = ngx_thumbnail_root .. uri
local gm_command = generate_gm_command(img_crop_type,img_original_path,img_size,img_thumbnail_path)

if (gm_command) then
lua_log('gm_command======'..gm_command)
_,_,img_thumbnail_dir,img__thumbnail_filename=string.find(img_thumbnail_path,'(.-)([^/]*)$')
os.execute('mkdir -p '..img_thumbnail_dir)
os.execute(gm_command)
end
ngx.req.set_uri('/thumbnail'..uri)
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值