首頁 運維 Nginx nginx+lua建構檔案上傳下載服務問題怎麼解決

nginx+lua建構檔案上傳下載服務問題怎麼解決

May 11, 2023 pm 08:52 PM
lua nginx

主要邏輯

nginx+lua建構檔案上傳下載服務問題怎麼解決

上傳

前端請求nginx 服務, nginx 呼叫upload 腳本,腳本透過尋找配置,找到對應的邏輯儲存路徑和實體儲存機器的agent 的ip 和端口,透過tcp 發包到對應agent ,部署在對應機器的agent 接受數據,並寫入本地檔案。

下載

http下載請求nginx , nginx 呼叫download 腳本,腳本解析連結參數,根據參數找到對應的agent 位址,請求返回檔案二進位內容,腳本接受到agent 傳回的數據,回傳給請求端。

設定nginx+lua

接下來主要講一下nginx 安裝設定(這裡包括lua的二進位流處理lpack, md5計算,mysql 操作, json 操作)

1、安裝nginx

下載

解壓縮tar -xvf nginx-1.10.3.tar.gz

2、安裝luajit(輕量級lua)

修改makefile 裡面的安裝路徑export prefix= /usr/local/luajit

##然後安裝make &make install

3、安裝nginx_lua_module

下載


解壓縮

4、 安裝ngx_devel_kit(ndk提供函數和巨集處理一些基本任務,減輕第三方模組開發的程式碼量)

下載

5、 安裝編譯,導入

export luajit_lib=/usr/local/luajit/lib 
export luajit_inc=/usr/local/luajit/include/luajit-2.0 
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/home/oicq/jeffzhuang/ngx_devel_kit-0.3.0 --add-module=/home/oicq/jeffzhuang/lua-nginx-module-0.10.
make -j2 
make install
登入後複製

#啟動/usr/local/nginx/sbin/nginx 重啟指令` usr/local/nginx/sbin/nginx -s reload v

如果報錯找不到luajit庫ln -s /usr/local/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

測試nginx直接開啟瀏覽器就可以了http:10.x.x.x:8080就可以看到歡迎介面了

6 、設定conf/nginx.conf運行lua 腳本

增加lua庫的查找路徑lua_package_path,lua_package_cpath

nginx+lua建構檔案上傳下載服務問題怎麼解決

7、增加mysql.lua下載 拷貝到lua_package_path 目錄下就可以了

8、增加csjon

#修改makefile 裡面的prefix=/usr/local/luajit就是luajit 的安裝路徑,make後將產生的cjson.so拷貝到

lua_package_cpath目錄下

9、安裝lpack 可以用現成的lpack.lua拷貝到lua_package_path 或用https://github.com/luadist/lpack 編譯產生lpack.so拷貝到lua_package_cpath 64位元需要增加編譯命令-fpic

10、upload.lua下載

11、md5下載

主要程式碼

1、前端上傳頁面程式碼

<!doctype html>
<html>
 <head>
  <title>file upload example</title>
 </head>
 <body>
  <form action="emer_upload/order_system_storage" method="post" enctype="multipart/form-data">
  <input type="file" name="testfilename"/>
  <input type="submit" name="upload" value="upload" />
  </form>
 </body>
</html>
登入後複製

2、upload上傳程式碼,該模組在解析檔案上傳請求的過程中,主要採用了簡單的類似有限狀態機的演算法來實現的,在不同的狀態由對應的handler 處理。

--文件下载服务写到 saverootpath .."/" .. filename 下面 
function download()
 local chunk_size = 4096
 local form,err=upload:new(chunk_size)
 if not form then
  ngx.log(ngx.err, "failed to new upload: ", err)
  ngx.exit(ngx.http_internal_server_error)
 end 
 form:set_timeout(100000)
 while true do
 local typ,res,err=form:read()
 if not typ then
  errormsg="failed to read :"..err
  return 1
 end
 if typ =="header" then
  local key=res[1]
  local value=res[2]
  if key =="content-disposition" then
  local kvlist=string.split(value,&#39;;&#39;)
   for _, kv in ipairs(kvlist) do
   local seg = string.trim(kv)
   if seg:find("filename") then
   local kvfile = string.split(seg, "=")
   filename = string.sub(kvfile[2], 2, -2)
   if filename then
    --获取文件后缀名字
    fileextension=getextension(filename)
    local linuxtime=tostring(os.time())
    filepath=saverootpath .."/" ..linuxtime..filename
    filetosave,errmsg = io.open(filepath, "w+")
    --存储的文件路径   
    --ngx.say("failed to open file ", filepath)
    if not filetosave then
    --ngx.say("failed to open file ", filepath .. errmsg)
    errormsg="打开文件失败"..filepath .. errmsg
    return 1
    end
   else
    errormsg="请求参数找不到文件名字"
    return 1
   end
   --跳出循环
   break 
   end
   end
  end
 elseif typ =="body" then
  if filetosave then
  filetosave:write(res)
  filemd5:update(res)
  end
 elseif typ =="part_end" then
  if filetosave then
  local md5_sum=filemd5:final()
  --ngx.say("md5: ", str.to_hex(md5_sum))
  filemd532=str.to_hex(md5_sum)
  filetosave:close()
  filetosave = nil
  end  
 elseif typ =="eof" then
  break
 else
  ngx.log(ngx.info, "do other things")
 end
 end
 return 0
end
登入後複製

3、tcp接收二進位資料

-- 读取byte
function readint8(tcp)
 local next, val = string.unpack(tcp:receive(1), "b")
 return tonumber(val);
end
-- 读取int16
function readint16(tcp)
 local next, val = string.unpack(tcp:receive(2), "h");
 return tonumber(val);
end
-- 读取int32
function readint32(tcp)
 local next, val = string.unpack(tcp:receive(4), ">i");
 return tonumber(val);
end
-- 读取字符串
function readstring(tcp,len)
 return tostring(tcp:receive(len));
end
登入後複製

4、tcp寫二進位數據,這裡和agent 的通訊協定是:開始標誌位+包長度+json 字串+結束標誌位,所以對應pack 用的參數就是biab ,> 就是轉換成大端

jsondata["filename"]=filemd532 .. "." .. fileextension
jsondata["cmd"]="write"
jsondata["filesize"]=tostring(filelen)
jsondata["path"]=system.."/"..storagedate
local jsonstr=cjson.encode(jsondata)
local uilen=string.len(jsonstr)
senddata=bpack(">b1iab",startindex,uilen,jsonstr,endindex)
socket:send(senddata)
登入後複製
###5、下載錯誤的時候,使用了redirect 直接跳到錯誤頁面,方便輸出錯誤訊息,其實這裡還可以做用戶token 校驗###
local errorurl="/downloaderror.html"
errormsg="url 参数解析有问题 "..index
return ngx.redirect(errorurl.."?msg="..errormsg,``` ngx.http_moved_temporarily)
登入後複製

以上是nginx+lua建構檔案上傳下載服務問題怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

怎麼查看nginx是否啟動 怎麼查看nginx是否啟動 Apr 14, 2025 pm 01:03 PM

確認 Nginx 是否啟動的方法:1. 使用命令行:systemctl status nginx(Linux/Unix)、netstat -ano | findstr 80(Windows);2. 檢查端口 80 是否開放;3. 查看系統日誌中 Nginx 啟動消息;4. 使用第三方工具,如 Nagios、Zabbix、Icinga。

linux怎麼查看nginx是否啟動 linux怎麼查看nginx是否啟動 Apr 14, 2025 pm 12:48 PM

在 Linux 中,使用以下命令檢查 Nginx 是否已啟動:systemctl status nginx根據命令輸出進行判斷:如果顯示 "Active: active (running)",則 Nginx 已啟動。如果顯示 "Active: inactive (dead)",則 Nginx 已停止。

linux怎麼啟動nginx linux怎麼啟動nginx Apr 14, 2025 pm 12:51 PM

在 Linux 中啟動 Nginx 的步驟:檢查 Nginx 是否已安裝。使用 systemctl start nginx 啟動 Nginx 服務。使用 systemctl enable nginx 啟用在系統啟動時自動啟動 Nginx。使用 systemctl status nginx 驗證啟動是否成功。在 Web 瀏覽器中訪問 http://localhost 查看默認歡迎頁面。

nginx在windows中怎麼配置 nginx在windows中怎麼配置 Apr 14, 2025 pm 12:57 PM

如何在 Windows 中配置 Nginx?安裝 Nginx 並創建虛擬主機配置。修改主配置文件並包含虛擬主機配置。啟動或重新加載 Nginx。測試配置並查看網站。選擇性啟用 SSL 並配置 SSL 證書。選擇性設置防火牆允許 80 和 443 端口流量。

nginx403怎麼解決 nginx403怎麼解決 Apr 14, 2025 am 10:33 AM

如何解決 Nginx 403 Forbidden 錯誤?檢查文件或目錄權限;2. 檢查 .htaccess 文件;3. 檢查 Nginx 配置文件;4. 重啟 Nginx。其他可能原因還包括防火牆規則、SELinux 設置或應用程序問題。

nginx怎麼查看運行狀態 nginx怎麼查看運行狀態 Apr 14, 2025 am 11:48 AM

查看 Nginx 運行狀態的方法有:使用 ps 命令查看進程狀態;查看 Nginx 配置文件 /etc/nginx/nginx.conf;使用 Nginx 狀態模塊啟用狀態端點;使用 Prometheus、Zabbix 或 Nagios 等監控工具。

怎麼啟動nginx服務器 怎麼啟動nginx服務器 Apr 14, 2025 pm 12:27 PM

啟動 Nginx 服務器需要按照不同操作系統採取不同的步驟:Linux/Unix 系統:安裝 Nginx 軟件包(例如使用 apt-get 或 yum)。使用 systemctl 啟動 Nginx 服務(例如 sudo systemctl start nginx)。 Windows 系統:下載並安裝 Windows 二進製文件。使用 nginx.exe 可執行文件啟動 Nginx(例如 nginx.exe -c conf\nginx.conf)。無論使用哪種操作系統,您都可以通過訪問服務器 IP

怎麼解決nginx跨域問題 怎麼解決nginx跨域問題 Apr 14, 2025 am 10:15 AM

解決 Nginx 跨域問題有兩種方法:修改跨域響應頭:添加指令以允許跨域請求,指定允許的方法和頭,以及設置緩存時間。使用 CORS 模塊:啟用模塊並配置 CORS 規則,允許跨域請求、方法、頭和設置緩存時間。

See all articles