设为首页 收藏本站
查看: 322|回复: 0

[经验分享] nginx lua处理图片

[复制链接]
发表于 2016-12-25 10:18:51 | 显示全部楼层 |阅读模式
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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-319088-1-1.html 上篇帖子: nginx tomcat mem做负载session共享 下篇帖子: 三大Web Server分析(Apache、Lighttpd、Nginx)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表