Nginx中使用Lua腳本與圖片的縮略圖處理的實(shí)現(xiàn)
環(huán)境搭建
Ubuntu 16.04
安裝環(huán)境的腳本
#!/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


雖然都是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 ...
如果出現(xiàn)如下情況,那么就要替換 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

lua-nginx-module 各版本的下載地址,這個包的版本不同會導(dǎo)致各種錯誤,目前我調(diào)試?yán)?6.04和18.04,這包如果實(shí)在裝不上,就多試一下其它版本的有奇效
https://github.com/openresty/lua-nginx-module/tags
圖片縮略圖
圖片縮略圖的原理就是使用,操作系統(tǒng)的處理圖片的convert命令
在調(diào)試圖片縮略圖前先在系統(tǒng)內(nèi)測試一下如下命令
# /home/3.jpg 圖片地址 # /home/3_100x100.jpg 切割后的圖片存放地址 convert /home/3.jpg -resize 100x100 +profile "*" /home/3_100x100.jpg
一般是有的這個命令, 但是也有部分會出現(xiàn) bash: convert: command not found
出現(xiàn)這個也不要慌,使用如下命令進(jìn)行安裝
apt-get install imagemagick -y
好啦進(jìn)入正題, 開始先編寫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 返回二進(jìn)制
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 文件 注意?。。?這個文件因?yàn)橐懈顖D片需要權(quán)限
# 加載外部 Lua 文件 注意?。?! 這個文件因?yàn)橐懈顖D片需要權(quán)限
# 加載外部 Lua 文件 注意?。?! 這個文件因?yàn)橐懈顖D片需要權(quán)限
content_by_lua_file conf/lua/ImageResizer.lua; #加載外部 Lua 文件 注意?。。?這個文件需要權(quán)限
}
}
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_jb51.rar
到此這篇關(guān)于Nginx中使用Lua腳本與圖片的縮略圖處理的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx使用Lua腳本處理縮略圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
nginx搭建圖片服務(wù)器的過程詳解(root和alias的區(qū)別)
這篇文章主要介紹了nginx搭建圖片服務(wù)器(root和alias的區(qū)別)的過程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
永中文檔在線轉(zhuǎn)換預(yù)覽基于nginx配置部署方案
這篇文章主要為大家介紹了永中文檔在線轉(zhuǎn)換預(yù)覽基于nginx配置部署方案的實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
使用Nginx和Lua進(jìn)行JWT校驗(yàn)介紹
大家好,本篇文章主要講的是使用Nginx和Lua進(jìn)行JWT校驗(yàn)介紹,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2021-12-12
解決nginx+uwsgi部署Django的所有問題(小結(jié))
本篇文章主要介紹了解決nginx+uwsgi部署Django的所有問題(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
詳解Nginx反向代理實(shí)現(xiàn)會話(session)保持的兩種方式
這篇文章主要介紹了詳解Nginx反向代理實(shí)現(xiàn)會話(session)保持的兩種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Nginx+uwsgi+ssl配置https的詳細(xì)步驟
nginx是一個輕量級的web服務(wù)器,在處理靜態(tài)資源和高并發(fā)有優(yōu)勢,uwsgi是一個基于python的高效率的協(xié)議,處理后端和動態(tài)網(wǎng)頁有優(yōu)勢,我這里使用的是Ubuntu18.04版本,服務(wù)器在阿里云,感興趣的朋友跟隨小編一起看看吧2023-10-10
Nginx PHP-Fcgi中因PHP執(zhí)行時間導(dǎo)致504 Gateway Timeout錯誤解決記錄
這篇文章主要介紹了Nginx PHP-Fcgi中因PHP執(zhí)行時間導(dǎo)致504 Gateway Timeout錯誤解決記錄,本文的解決方法得來不易,需要的朋友可以參考下2014-09-09

