服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - Nginx中使用Lua脚本与图片的缩略图处理的实现

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

2022-07-29 09:38蓝小白1024 Nginx

本文主要介绍了Nginx中使用Lua脚本与图片的缩略图处理的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

环境搭建

Ubuntu 16.04

安装环境的脚本

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

?
1
ngx.say("hello lua");

启动nginx:

?
1
nginx

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

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

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

Ubuntu 18.04

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

?
1
2
3
4
5
6
...
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 的使用版本啦

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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命令
在调试图片缩略图前先在系统内测试一下如下命令

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

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

?
1
apt-get install imagemagick -y

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

ImageResizer.lua

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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

?
1
nginx -s reload

访问

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

文中使用的文件 nginx_lua.rar 

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

原文链接:https://blog.csdn.net/lxb_wyf/article/details/123548400

延伸 · 阅读

精彩推荐
  • Nginx详解如何修改nginx的默认端口

    详解如何修改nginx的默认端口

    这篇文章主要介绍了详解如何修改nginx的默认端口,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下...

    YLD107512021-05-06
  • NginxNginx配合php实现生成实时缩略图功能

    Nginx配合php实现生成实时缩略图功能

    这篇文章主要介绍了Nginx配合php实现生成实时缩略图功能,这在一些特殊场合可能会要用到,需要的朋友可以参考下 ...

    Nginx教程网3232019-10-24
  • NginxCentos 6.5 64位中Nginx详细安装部署教程

    Centos 6.5 64位中Nginx详细安装部署教程

    Nginx是一个web服务器也可以用来做负载均衡及反向代理使用,目前使用最多的就是负载均衡,具体简介我就不介绍了百度一下有很多,下面直接进入安装步...

    taiyonghai2972019-12-03
  • NginxNginx中禁止使用IP访问网站的配置实例

    Nginx中禁止使用IP访问网站的配置实例

    这篇文章主要介绍了Nginx中禁止使用IP访问网站的配置实例,一般在备案时可能需要这种设置,需要的朋友可以参考下 ...

    Nginx配置网5772019-10-22
  • Nginx为Nginx动态添加模块的方法

    为Nginx动态添加模块的方法

    这篇文章主要介绍了为Nginx动态添加模块的方法,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下...

    冰河团队7982020-07-22
  • NginxNginx开启Brotli压缩算法实现过程详解

    Nginx开启Brotli压缩算法实现过程详解

    这篇文章主要介绍了Nginx开启Brotli压缩算法实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可...

    -零4172021-01-06
  • Nginx部署Nginx+Apache动静分离的实例详解

    部署Nginx+Apache动静分离的实例详解

    Nginx不仅能作为Web服务器,还具有反向代理、负载均衡和缓存的功能,这篇文章主要介绍了部署Nginx+Apache动静分离的实例代码,需要的朋友可以参考下 ...

    丑叔叔7372020-03-19
  • Nginx详解Centos7.2安装Nginx实现负载平衡

    详解Centos7.2安装Nginx实现负载平衡

    本篇文章主要介绍了详解Centos7.2安装Nginx实现负载平衡,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    淅淅沥沥5682021-12-31