7.8 KiB
7.8 KiB
Resty Function
前置安装
- 安装opm
sudo dnf -f install dnf -y install openresty-opm
- 安装 lua-resty-jit-uuid https://github.com/thibaultcha/lua-resty-jit-uuid
opm get thibaultcha/lua-resty-jit-uuid
- 通用命令 重启服务
$ sudo openresty -t
nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful
#首先要openresty nginx配置文件无误
$ sudo systemctl restart openresty
#重启openresty服务不报错
nginx根配置
- 配置openresty
# 配置文件路径
# /usr/local/openresty/nginx/conf/nginx.conf
# http contex 中添加lua脚本的路径
lua_package_path "/usr/local/openresty/resty_funcs/?.lua;;";
1.图片上传
- music-room配置
# 子域名配置文件路径
# /usr/local/openresty/nginx/conf.d/music-room.conf
# server contex 中添加文件存储路径变量
set $store_dir "/file_upload/"; # 文件存储路径
# server contex 中添加location
# Upload image file
location /upload_image {
content_by_lua_file /usr/local/openresty/resty_funcs/upload_image.lua;
}
- 添加lua脚本
将file_upload文件夹下的upload_image.lua 拷贝到如下路径
/usr/local/openresty/resty_funcs
.
└── upload_image.lua
- 测试curl命令
curl -v -F 'up_load_json=@./test_files/tile-0.jpg' -F 'up_load_json=@./test_files/tile-1.jpg' -F 'up_load_json=@./test_files/tile-2.jpg' https://music-room.alchemy-studio.cn/upload_image
⚠️ ./test_files/test0.jpg ./test_files/test1.jpg ./test_files/test2.jpg 为三张上传拼接的图片的路径和文件名
* Trying 152.136.103.69...
* TCP_NODELAY set
* Connected to music-room.alchemy-studio.cn (152.136.103.69) port 443 (#0)
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
* CAfile: /etc/ssl/cert.pem
CApath: none
* TLSv1.2 (OUT), TLS handshake, Client hello (1):
* TLSv1.2 (IN), TLS handshake, Server hello (2):
* TLSv1.2 (IN), TLS handshake, Certificate (11):
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
* TLSv1.2 (IN), TLS handshake, Server finished (14):
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (OUT), TLS handshake, Finished (20):
* TLSv1.2 (IN), TLS change cipher, Change cipher spec (1):
* TLSv1.2 (IN), TLS handshake, Finished (20):
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* ALPN, server accepted to use http/1.1
* Server certificate:
* subject: CN=music-room.alchemy-studio.cn
* start date: May 16 05:38:01 2021 GMT
* expire date: Aug 14 05:38:01 2021 GMT
* subjectAltName: host "music-room.alchemy-studio.cn" matched cert's "music-room.alchemy-studio.cn"
* issuer: C=US; O=Let's Encrypt; CN=R3
* SSL certificate verify ok.
> POST /upload_image HTTP/1.1
> Host: music-room.alchemy-studio.cn
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Length: 299590
> Content-Type: multipart/form-data; boundary=------------------------67148dd99aa78bdc
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
* We are completely uploaded and fine
< HTTP/1.1 200 OK
< Server: openresty/1.19.3.2
< Date: Thu, 22 Jul 2021 12:01:51 GMT
< Content-Type: application/octet-stream
< Transfer-Encoding: chunked
< Connection: keep-alive
<
-->> POST : Upload origin image is test0.jpg
Open file ok 82e3830b-c6ee-45cd-85c6-246682c087aa.jpg
Insert upload image table 1 with /file_upload/82e3830b-c6ee-45cd-85c6-246682c087aa.jpg
-->> POST : Upload origin image is test1.jpg
Open file ok f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg
Insert upload image table 2 with /file_upload/f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg
-->> POST : Upload origin image is test2.jpg
Open file ok 992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
Insert upload image table 3 with /file_upload/992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
Show all upload files
Save upload file is : /file_upload/82e3830b-c6ee-45cd-85c6-246682c087aa.jpg
Save upload file is : /file_upload/f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg
Save upload file is : /file_upload/992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
Input images list : /file_upload/82e3830b-c6ee-45cd-85c6-246682c087aa.jpg /file_upload/f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg /file_upload/992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
Output image is : /file_upload/553120d6-879d-4c9f-a74a-8319b7903642.jpg
/usr/local/ImageMagick/bin/magick convert -append /file_upload/82e3830b-c6ee-45cd-85c6-246682c087aa.jpg /file_upload/f67ece02-cb5c-48f8-9ff9-e56c69239b15.jpg /file_upload/992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg /file_upload/553120d6-879d-4c9f-a74a-8319b7903642.jpg
* Connection #0 to host music-room.alchemy-studio.cn left intact
* Closing connection 0
- 查看上传文件和拼接文件
$ pwd
/file_upload
$ ls -l
总用量 1440
-rw-rw-rw- 1 nobody nobody 99699 7月 21 22:25 08916bcf-93f4-4a2a-8d39-0e11174fdb90.jpg
-rw-rw-rw- 1 nobody nobody 35979 7月 22 11:46 52b2a24d-da6c-41e8-8cdb-2eab371eb56f.jpg
-rw-rw-rw- 1 nobody nobody 298648 7月 22 20:01 553120d6-879d-4c9f-a74a-8319b7903642.jpg
-rw-rw-rw- 1 nobody nobody 298648 7月 21 22:25 6143c43c-e2ab-4002-b391-9f82f6008931.jpg
-rw-rw-rw- 1 nobody nobody 99699 7月 22 20:01 82e3830b-c6ee-45cd-85c6-246682c087aa.jpg
-rw-rw-rw- 1 nobody nobody 99699 7月 21 22:25 8f2cd5d0-09f6-4be7-9829-3a12e92b81d3.jpg
-rw-rw-rw- 1 nobody nobody 99699 7月 22 20:01 992da1f2-9a9e-41e2-a14d-9d57aa07dcd2.jpg
-rw-rw-rw- 1 nobody nobody 6109 7月 22 11:46 9c831637-7de6-4255-8878-f2069b8f4f08.jpg
-rw-rw-rw- 1 nobody nobody 99699 7月 21 22:25 9ec1a383-9678-4c16-834f-57b4d7f87a1d.jpg
2.图片拼接
- music-room配置
# 子域名配置文件路径
# /usr/local/openresty/nginx/conf.d/music-room.conf
# server contex 中添加文件存储路径变量
set $store_dir "/file_upload/"; # 文件存储路径
# server contex 中添加location
#Image append
location /image_append {
content_by_lua_file /usr/local/openresty/nginx/conf/lua/image_append.lua;
}
- 添加lua脚本
/usr/local/openresty/resty_funcs
.
└── image_append.lua
3.图片下载
- music-room配置
# 子域名配置文件路径
# /usr/local/openresty/nginx/conf.d/music-room.conf
# server contex 中添加location
#Donwload image use ngx.req.set_uri
location /download_image {
rewrite_by_lua_block {
ngx.log(ngx.INFO , "location /download_image")
-- TODO: jwt decoder
--
-- Functions : String split
string.split = function(s, p)
local rt= {}
string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
return rt
end
-- Get url and split by '/' save into a table
local sub_url_table = string.split(ngx.var.uri, '/')
local sub_url_number = table.getn(sub_url_table)
for i = 1, sub_url_number do
ngx.log(ngx.INFO , "Sub url sub" .. i .. " : " .. sub_url_table[i])
end
local uri = "/internal_file_upload/"..sub_url_table[sub_url_number]..".jpg"
ngx.log(ngx.INFO , "uri : " .. uri)
return ngx.req.set_uri(uri , true)
}
}
#Static file server
location /internal_file_upload {
internal;
try_files $uri /file_upload/8493acab-0f7a-47e0-8fff-e96b843334dd.jpg;
}
- 添加lua脚本
/usr/local/openresty/resty_funcs
.
└── download_image.lua