Nginx中使用Lua腳本與圖片的縮略圖處理的實現(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,這包如果實在裝不上,就多試一下其它版本的有奇效
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 文件 注意!?。?這個文件因為要切割圖片需要權(quán)限 # 加載外部 Lua 文件 注意?。。?這個文件因為要切割圖片需要權(quán)限 # 加載外部 Lua 文件 注意?。?! 這個文件因為要切割圖片需要權(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腳本與圖片的縮略圖處理的實現(xiàn)的文章就介紹到這了,更多相關(guān)Nginx使用Lua腳本處理縮略圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
國內(nèi)一些常用PHP的CMS的Nginx服務(wù)器的偽靜態(tài)規(guī)則整理
當(dāng)我們從apache服務(wù)器轉(zhuǎn)向Nginx服務(wù)器的時候,它們的偽靜態(tài)規(guī)則就不一樣了,所以你熟悉Nginx服務(wù)器的偽靜態(tài)規(guī)則,自己寫當(dāng)然也好2011-03-03關(guān)于nginx沒有跳轉(zhuǎn)到upstream地址的解決
這篇文章主要介紹了關(guān)于nginx沒有跳轉(zhuǎn)到upstream地址的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Ubuntu上安裝Nginx服務(wù)器程序及簡單的環(huán)境配置小結(jié)
Nginx是一款高性能的異步非阻塞服務(wù)器應(yīng)用程序,人氣相當(dāng)高,這里我們就來看一下在Ubuntu上安裝Nginx服務(wù)器程序及簡單的環(huán)境配置小結(jié):2016-07-07nginx配置ssl實現(xiàn)https訪問的步驟(適合新手)
這篇文章主要給大家介紹了關(guān)于nginx配置ssl實現(xiàn)https訪問的相關(guān)資料,這個教程非常適合新手小白,文中通過示例代碼將實現(xiàn)的方法一步步介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧2018-12-12深入探究Nginx體系化之虛擬主機(jī)分類及配置實現(xiàn)
Nginx,這款備受推崇的高性能 Web 服務(wù)器,以其強(qiáng)大的性能和靈活的配置而廣受歡迎,在實際應(yīng)用中,虛擬主機(jī)是一項重要的功能,允許我們在單個服務(wù)器上托管多個網(wǎng)站,本文將深入探討 Nginx 虛擬主機(jī)的分類和配置實現(xiàn),幫助您構(gòu)建一個高效多站點托管平臺2023-08-08SpringBoot項目整合FastDFS+Nginx實現(xiàn)圖片上傳功能
FastDFS是一個開源的輕量級分布式文件系統(tǒng),它對文件進(jìn)行管理,功能包括:文件存儲、文件同步、文件訪問(文件上傳、文件下載)等,解決了大容量存儲和負(fù)載均衡的問題,對SpringBoot整合FastDFS實現(xiàn)圖片上傳功能,感興趣的朋友一起看看吧2022-05-05