Nginx中使用Lua脚本与图片的缩略图处理的实现


Posted in Servers onMarch 18, 2022

环境搭建

Ubuntu 16.04

安装环境的脚本

#!/bin/bash
apt-get update
apt-get install gcc g++ make wget openssl libssl-dev vim bzip2 -y
tar xzvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make install PREFIX=/usr/local/luajit
echo 'export LUAJIT_LIB=/usr/local/luajit/lib'>>/etc/bashrc
echo 'export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0'>>/etc/bashrc
source /etc/bashrc
cd /root/nginx_lua  # 注意你把文件文件放在哪里就去哪里!!!
tar xf LuaJIT-2.0.4.tar.gz
tar -xzvf v0.10.9rc7.tar.gz -C /usr/local/src   # lua-nginx-module-0.10.9rc7
tar -xzvf v0.3.0.tar.gz -C /usr/local/src
tar jxvf pcre-8.42.tar.bz2 -C /usr/local/src/
tar zxvf zlib-1.2.11.tar.gz -C /usr/local/src/
tar -xzvf nginx-1.16.1.tar.gz -C /usr/local/src
ln -s /usr/local/nginx/sbin/nginx   /usr/bin/nginx
cd  /usr/local/src/nginx-1.16.1
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.9rc7 --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_stub_status_module --without-http-cache --with-http_gzip_static_module
make -j2
make install
cp /usr/local/luajit/lib/libluajit-5.1.so.2 /usr/local/lib/
echo "/usr/local/lib"  >>/etc/ld.so.conf
/sbin/ldconfig
mv  /usr/local/nginx/conf/nginx.conf  /tmp
cd /root/nginx_lua   # 注意你把文件文件放在哪里就去哪里!!!
cp   nginx.conf     /usr/local/nginx/conf/
cp  -r  lua  /usr/local/nginx/conf/
nginx
#curl 127.0.0.1/hello
#curl 127.0.0.1/lua

nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /hello {
        default_type 'text/plain';
        content_by_lua 'ngx.say("hello,lua")';
        }
        location /lua {
        default_type 'text/html';
        content_by_lua_file conf/lua/test.lua;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

test.lua

ngx.say("hello lua");

启动nginx:

nginx

Nginx中使用Lua脚本与图片的缩略图处理的实现

Nginx中使用Lua脚本与图片的缩略图处理的实现

虽然都是hello lua ,但是我也不知道为什么字体显示的竟然不同

Ubuntu 18.04

在Ubuntu18.04中,其它的包不变,与上面16.04中一致,要变动 lua-nginx-module 包的版本,变动为lua-nginx-module-0.10.14rc7

...
tar -xzvf v0.10.14rc7.tar.gz -C /usr/local/src   # lua-nginx-module-0.10.14rc7
...
./configure --prefix=/usr/local/nginx --add-module=/usr/local/src/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.14rc7 --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.42 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_stub_status_module --without-http-cache --with-http_gzip_static_module

...

如果出现如下情况,那么就要替换 lua-nginx-module 的使用版本啦

devel_kit-0.3.0/src -I /home/usr/ngx_devel_kit-0.3.0/src -I /home/usr/ngx_devel_kit-0.3.0/objs -I objs/addon/ndk -I /usr/local/include/luajit-2.0 \
 -o objs/addon/src/ngx_http_lua_req_body.o \
 /home/usr/lua-nginx-module-0.10.8/src/ngx_http_lua_req_body.c
/home/usr/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c: In function 'ngx_http_lua_ngx_req_raw_header':
/home/usr/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c:151:15: error: incompatible types when assigning to type 'ngx_buf_t * {aka struct ngx_buf_s *}' from type 'ngx_chain_t {aka struct ngx_chain_s}'
             b = hc->busy[i];
               ^
/home/usr/lua-nginx-module-0.10.8/src/ngx_http_lua_headers.c:227:15: error: incompatible types when assigning to type 'ngx_buf_t * {aka struct ngx_buf_s *}' from type 'ngx_chain_t {aka struct ngx_chain_s}'
             b = hc->busy[i];
               ^
objs/Makefile:1465: recipe for target 'objs/addon/src/ngx_http_lua_headers.o' failed
make[1]: *** [objs/addon/src/ngx_http_lua_headers.o] Error 1
make[1]: *** Waiting for unfinished jobs....
make[1]: Leaving directory '/home/usr/nginx-1.17.10'
make: *** [build] Error 2
Makefile:8: recipe for target 'build' failed

Nginx中使用Lua脚本与图片的缩略图处理的实现

lua-nginx-module 各版本的下载地址,这个包的版本不同会导致各种错误,目前我调试啦16.04和18.04,这包如果实在装不上,就多试一下其它版本的有奇效

https://github.com/openresty/lua-nginx-module/tags

图片缩略图

图片缩略图的原理就是使用,操作系统的处理图片的convert命令
在调试图片缩略图前先在系统内测试一下如下命令

# /home/3.jpg  图片地址   						 # /home/3_100x100.jpg  切割后的图片存放地址
convert /home/3.jpg -resize 100x100 +profile "*" /home/3_100x100.jpg

一般是有的这个命令, 但是也有部分会出现 bash: convert: command not found
出现这个也不要慌,使用如下命令进行安装

apt-get install imagemagick -y

好啦进入正题, 开始先编写lua脚本

ImageResizer.lua

local ext =  ngx.var.ext
local filepath_i = ngx.var.request_filepath
local filepath = filepath_i .. "." .. ext

local width = ngx.var.width
local height = ngx.var.height
local command = "convert " .. filepath .. " -resize " .. width .. "x" .. height .. " +profile \"*\" " .. filepath_i .. "_" .. width .. "x" .. height .. "." .. ext;
os.execute(command);

-- ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓    
--这个方法是在lua脚本里面直接读取切好的缩略图,读取完通过 ngx.say 返回二进制
local filename = filepath_i .. "_" .. width .. "x" .. height .. "." .. ext;
local photo_size = io.open(filename,"rb") 
local photo_data_size = photo_size:read("*a");
photo_size:close();
ngx.say(photo_data_size);
-- ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑

-- 如果不想在lua中读取图片的话,使用 ngx.exec(ngx.var.request_uri);
-- 这个表示重新请求这个地址  /xxx/1_100x100.jpg
--ngx.exec(ngx.var.request_uri);

nginx.conf

user root;  
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /hello {
        default_type 'text/plain';
        content_by_lua 'ngx.say("hello,lua")';
        }
        location /lua {
        default_type 'text/html';
        content_by_lua_file conf/lua/test.lua;
        }
	location ~* /photo/(.*)_(\d+)x(\d+)\.(jpg|png|jpeg|gif|JPG|PNG|JPEG|GIF)$ {
		root /;
                if (!-f $request_filename) { 
                        lua_code_cache on; 
                        set $request_filepath /$1;
                        set $width $2;
                        set $height $3;
                        set $ext $4;
                        # 加载外部 Lua 文件   注意!!! 这个文件因为要切割图片需要权限
                        # 加载外部 Lua 文件   注意!!! 这个文件因为要切割图片需要权限
                        # 加载外部 Lua 文件   注意!!! 这个文件因为要切割图片需要权限
                        content_by_lua_file conf/lua/ImageResizer.lua;  #加载外部 Lua 文件   注意!!! 这个文件需要权限
                }
        }
        location /photo/ {
            alias /;
        }

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

编写完之后

重启nginx

nginx -s reload

访问

http://127.0.0.1/photo/home/3.jpg   # 原图
http://127.0.0.1/photo/home/3_100x100.jpg   # 缩略图

到此这篇关于Nginx中使用Lua脚本与图片的缩略图处理的实现的文章就介绍到这了,更多相关Nginx使用Lua脚本处理缩略图内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Servers 相关文章推荐
Nginx服务器添加Systemd自定义服务过程解析
Mar 31 Servers
Nginx域名转发https访问的实现
Mar 31 Servers
Nginx进程管理和重载原理详解
Apr 22 Servers
windows下快速安装nginx并配置开机自启动的方法
May 11 Servers
nginx配置文件使用环境变量的操作方法
Jun 02 Servers
Nginx反向代理学习实例教程
Oct 24 Servers
Windows Server 2008 修改远程登录端口以及配置防火墙
Apr 28 Servers
Nginx配置之禁止指定IP访问
May 02 Servers
基于docker安装zabbix的详细教程
Jun 05 Servers
设置IIS Express并发数
Jul 07 Servers
Linux安装Docker详细教程
Jul 07 Servers
Nginx如何配置多个服务域名解析共用80端口详解
Sep 23 Servers
nginx刷新页面出现404解决方案(亲测有效)
Mar 18 #Servers
Nginx+Windows搭建域名访问环境的操作方法
Mar 17 #Servers
nginx中封禁ip和允许内网ip访问的实现示例
Mar 17 #Servers
Apache Linkis 中间件架构及快速安装步骤
关于Nginx中虚拟主机的一些冷门知识小结
Mar 03 #Servers
nginx负载功能+nfs服务器功能解析
Apache Pulsar集群搭建部署详细过程
Feb 12 #Servers
You might like
PHP 彩色文字实现代码
2009/06/29 PHP
解决CodeIgniter伪静态失效
2014/06/09 PHP
PHP赋值的内部是如何跑的详解
2019/01/13 PHP
用JS实现的一个include函数
2007/07/21 Javascript
关于Ext中form移除textfield方法:hide(),setVisible(false),remove()
2010/12/02 Javascript
jQuery源码分析-05异步队列 Deferred 使用介绍
2011/11/14 Javascript
原生js实现半透明遮罩层效果具体代码
2013/06/06 Javascript
IE、FF、Chrome浏览器中的JS差异介绍
2013/08/13 Javascript
javascript字符串替换及字符串分割示例代码
2013/12/12 Javascript
深入理解JavaScript系列(19):求值策略(Evaluation strategy)详解
2015/03/05 Javascript
bootstrap表单按回车会自动刷新页面的解决办法
2017/03/08 Javascript
详解webpack解惑:require的五种用法
2017/06/09 Javascript
微信小程序使用wxParse解析html的方法教程
2018/07/06 Javascript
快速解决Vue项目在IE浏览器中显示空白的问题
2018/09/04 Javascript
浅谈微信页面入口文件被缓存解决方案
2018/09/29 Javascript
如何为你的JS项目添加智能提示与类型检查详解
2019/03/12 Javascript
node实现socket链接与GPRS进行通信的方法
2019/05/20 Javascript
vue使用代理解决请求跨域问题详解
2019/07/24 Javascript
[01:42]TI4西雅图DOTA2前线报道 第一顿早饭哦
2014/07/08 DOTA
Python实现的中国剩余定理算法示例
2017/08/05 Python
influx+grafana自定义python采集数据和一些坑的总结
2018/09/17 Python
python学生信息管理系统(完整版)
2020/04/05 Python
flask 使用 flask_apscheduler 做定时循环任务的实现
2019/12/10 Python
python+selenium+PhantomJS抓取网页动态加载内容
2020/02/25 Python
keras模型保存为tensorflow的二进制模型方式
2020/05/25 Python
Html5大文件断点续传实现方法
2015/12/05 HTML / CSS
澳大利亚领先的运动鞋商店:Hype DC
2018/03/31 全球购物
波兰快递服务:Globkurier.pl
2019/11/08 全球购物
Feelunique中文官网:欧洲最大化妆品零售电商
2020/07/10 全球购物
九年级家长会邀请函
2014/01/15 职场文书
委托代理人授权委托书范本
2014/09/24 职场文书
2015年度校学生会工作总结报告
2015/05/23 职场文书
人间正道是沧桑观后感
2015/06/15 职场文书
选调生挂职锻炼工作总结
2015/10/23 职场文书
Nginx解决403 forbidden的完整步骤
2021/04/01 Servers
Spring Cloud 中@FeignClient注解中的contextId属性详解
2021/09/25 Java/Android