2021-09-12 11:49:55 +08:00
2021-09-06 19:33:30 +08:00
2021-09-12 11:49:55 +08:00
2021-09-12 11:49:55 +08:00
2021-07-22 19:39:57 +08:00
2021-09-06 19:33:30 +08:00

Resty Function


前置安装

  • 安装opm
sudo dnf -f install dnf -y install openresty-opm
 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

#Static file server
    location /file_upload {
        default_type "";
        alias /file_upload/;
        autoindex on;
        autoindex_exact_size off;
        autoindex_localtime on;
    }
#Download image files
location /download_image {
  rewrite_by_lua_block{
    -- Get download image id
      local download_image_id = string.match(ngx.var.request_uri, ".*/(.*)$")
      ngx.log(ngx.INFO , "Download image id: " .. download_image_id)

      -- Set redirect uri
      --local uri = "/internal_file_upload/" .. download_image_id .. ".jpg"
      --return ngx.exec(uri)

      local uri = "/file_upload/" .. download_image_id .. ".jpg"
      return ngx.exec(uri, {a = download_image_id})
  }
}
  • wget测试
wget http://localhost:8080/download_image/{download_image_uuid}
S
Description
OpenResty configs and Lua scripts
Readme 469 KiB
Languages
Lua 76.4%
Shell 23.6%