基於openresty用lua搭建短網址服務

目標及讀者

目標

:能夠基於openresty用lua+redis搭建短網址服務。

讀者

:要求有一定的web開發基礎。

準備工作

硬體環境

PC或筆記本:1臺

雲伺服器:1臺

軟體工具

OS: windows10-64bit,CentOS7-64bit

web平臺:openresty

快取:redis

IDE:Vscode

PS: 工具名稱連結到官網,本文所需資源網盤下載地址(提取碼:skmq)。

本地(windows)開發環境搭建

web平臺(openresty)

下載及安裝

從官網或網盤下載zip解壓安裝包(如:openresty-1。15。8。3-win32。zip),然後解壓到某個碟符下(如:D:\)。PS: 因luarocks目前僅有32bit的編譯釋出包。

雙擊安裝目錄(如: D:\openresty-1。15。8。3-win32)下“nignx。exe”執行,將會出現如下介面,說明安裝正常。PS: 若有必要請安裝perl。

基於openresty用lua搭建短網址服務

啟停

編寫啟停指令碼存放在安裝目錄下。

啟動指令碼start.bat

::start。batnginx。exe -c conf/nginx。conf

停止指令碼stop.bat

::stop。batnginx -s stop taskkill /F /IM nginx。exe > nul

雙擊start。bat啟動,雙擊stop。bat停止。

配置

預設是使用安裝目錄下的conf/nginx。conf,若有必要,按實際需求進行修改即可。每次修改配置後,請重啟程式或重載入配置(nginx -s reload)。 如: 釋出f:/dwz網站

location / {    root f:/git/dwz;    index index。html index。htm; }

安裝gcc(mingw)、cmake環境安裝

下載及安裝

: mingw-w64|MinGW cmake 。下載後直接解壓到d:\或安裝並配置系統環境:

MINGW32_HOME=d:\mingw-w64\mingw32 MINGW_HOME=%MINGW32_HOME% CMAKE_HOME32=d:\cmake-3。17。1-win32-x86 CMAKE_HOME64=d:\cmake-3。17。1-win64-x64 CMAKE_HOME=%CMAKE_HOME32% path=%MINGW_HOME%\bin;%CMAKE_HOME%\bin;%path%

安裝luarocks及依賴庫

下載及安裝

下載all-in-one包 latest win32。zip file here,然後解壓到d:\,然後執行install進行安裝:

cd /d d:\luarocks-3。3。1-win32\ set PREFIX=d:\openresty-1。15。8。3-win32 install /P %PREFIX%\luarocks /SELFCONTAINED /INC %PREFIX%\include\luajit-2。1 /LIB %PREFIX% /BIN %PREFIX% /MW

配置系統環境變數

OPENRESTY_HOME=D:\openresty-1。15。8。3-win32 ##luajit。exeRESTY_LUAROCKS_SYSTREE=D:\openresty-1。15。8。3-win32/luarocks/systreeLUA_VERSION=5。1LUA_PATH=%OPENRESTY_HOME%\lua\?。lua;%OPENRESTY_HOME%\lualib\?。lua;%RESTY_LUAROCKS_SYSTREE%\share\lua\%LUA_VERSION%\?。lua LUA_CPATH=?。dll;?/?。dll;%OPENRESTY_HOME%\lualib\?。dll;%OPENRESTY_HOME%\lualib\?。so;%RESTY_LUAROCKS_SYSTREE%\lib\lua\%LUA_VERSION%\?。dll

安裝依賴lua庫

##啟動cmd視窗cd /d %OPENRESTY_HOME%\luarocksD:\openresty-1。15。8。3-win32\luarocks>luarocks install bit32D:\openresty-1。15。8。3-win32\luarocks>luarocks install structD:\openresty-1。15。8。3-win32\luarocks>luarocks install lua-cjsonD:\openresty-1。15。8。3-win32\luarocks>luarocks listRocks installed for Lua 5。1——————————————-bit32   5。3。0-1 (installed) - d:/openresty-1。15。8。3-win32/luarocks/systree/lib/luarocks/rocks-5。1lua-cjson   2。1。0。6-1 (installed) - d:/openresty-1。15。8。3-win32/luarocks/systree/lib/luarocks/rocks-5。1struct   1。4-1 (installed) - d:/openresty-1。15。8。3-win32/luarocks/systree/lib/luarocks/rocks-5。1

快取(redis)

下載及安裝

從redis微軟釋出官網或網盤下載zip解壓安裝包(如:Redis-x64-3。2。100。zip),然後解壓到某個碟符下(如:D:\),雙擊安裝目錄(如: D:\Redis-x64-3。2。100)下“redis-server。exe”執行,將會出現如下介面,說明安裝正常。

基於openresty用lua搭建短網址服務

啟停

編寫啟停指令碼存放在安裝目錄下。

啟動指令碼start.bat

::start。batredis-server。exe redis。windows。conf

停止指令碼stop.bat

::stop。battaskkill /F /IM redis-server。exe > nul

雙擊start。bat啟動,雙擊stop。bat停止。

配置

預設是使用安裝目錄下的redis。windows。conf,若有必要,按實際需求進行修改即可。每次修改配置後,請重啟程式。如: 僅本機且密碼訪問

bind 127。0。0。1requirepass %FSIh$qcnoYwGQvN

IDE(Vscode)

下載及安裝

從官網或網盤下載zip解壓安裝包(如:VSCode-win32-x64-1。44。1。zip),然後解壓到某個碟符下(如:D:\VSCode-win32-x64),雙擊安裝目錄(如: D:\VSCode-win32-x64)下“code。exe”執行。

下載及安裝Lua外掛

啟動vscode後,按“Ctrl+Shift+X”,輸入lua, 選中“Lua 0。18。1”,點選右下角的Install完成安裝。如下圖:

基於openresty用lua搭建短網址服務

PS: 外掛Code Runner,方便除錯程式碼段。

短網址開發與除錯

設計思路

短網址的目的是將長網址轉換為較短的網址,從而方便記憶或使用。目前比較多的是t。cn和url。cn的短網址服務,功能比較完善且成熟,但都收費。故設計一個最簡單的

短網址服務

:僅提供長短轉換及短網址訪問功能。

最簡單的轉換方法就是對長網址做HASH、MD5、CRC等處理獲取唯一值,然後進行儲存,最後提供基於K的訪問短網址。

考慮到效率問題,經過收集資料甄比篩選,決定使用MurmurHash3+Redis來實現簡單的長短網址的轉換和儲存,openresty+lua來實現此功能。

主要編碼

dwz/murmurhash3.lua

: 實現hash演算法模組。具有一定的高效性、穩定性、防碰撞性。

dwz/base62.lua

: 實現10與62進位制的轉換模組。支援自定義62進位制字串序。

dwz/redis-mg.lua

: redis操作的簡單封裝。

dwz/init.lua

: 長短網址轉換和儲存模組。

access_by_redis.lua

: 基於訪問IP的限流和短網址服務功能。

本地除錯

修改openresty的配置檔案conf/nginx。conf,然後重啟或重載入配置:

error_log logs/error。log debug;   http {      # load lua modules env。LUA_{PATH,CPAHT}      lua_code_cache off;  ##only debug      lua_package_path        “f:/git/dwz/lua/?。lua;f:/git/dwz/?/?。lua;f:/git/dwz/lua/?/init。lua;;”;   }   server {      access_by_lua_file “f:/git/dwz/lua/access_by_redis。lua”;      location /dwz/ {         alias f:/git/dwz/;         index index。html         error_page   404 /dwz/404。html;     }   }

然後在瀏覽器(建議chrome)位址列輸入地址後回車:

長網址轉短網址

:localhost/dwz/${由encodeURIComponent編碼的長網址},將返回json資料(包括原長網址和目標短網址)

短網址訪問

:localhost/dwz/${hashkey} 若出現訪問異常,請檢視openresty的日誌檔案logs/error。log。

短網址部署釋出

釋出環境搭建

購買一臺雲伺服器,安裝CentOS7,然後yum安裝redis、openresty,再安裝luarocks及依賴庫且配置環境變數:

##配置openresty安裝域名源restyRepo(){cat << RESTY[openresty]name=Official OpenResty Open Source Repository for CentOSbaseurl=https://openresty。org/package/centos/$releasever/$basearchskip_if_unavailable=Falsegpgcheck=1repo_gpgcheck=0gpgkey=https://openresty。org/package/pubkey。gpgenabled=1enabled_metadata=1RESTY}restyRepo > /etc/yum。repos。d/openresty。repo##yum安裝redis openrestyyum makecacheyum install -y redis openresty##安裝luarocks及依賴庫#從 http://www。luarocks。org/en/Download 下載最新版本的 LuaRocks原始碼進行安裝cd /optwget http://luarocks。org/releases/luarocks-3。3。1。tar。gztar -xzvf luarocks-3。3。1。tar。gzcd luarocks-3。3。1OPENRESTY_HOME=/usr/local/openresty。/configure ——prefix=${OPENRESTY_HOME}/luarocks ——with-lua=${OPENRESTY_HOME}/luajit make && make install #設定lua搜尋路徑echo ‘##openresty && luarocks ’ >> /etc/profileecho ‘export OPENRESTY_HOME=/usr/local/openresty ’ >> /etc/profileecho ‘eval `${OPENRESTY_HOME}/luarocks/bin/luarocks path`’ >> /etc/profilesource /etc/profile#安裝lua庫 luarocks install structluarocks list

短網址部署釋出

將本地除錯好的程式打包(dwz。zip),上傳到雲伺服器上,並解壓到某個目錄下(如:/opt/myweb),再修改openresty的配置檔案,最後重啟openresty服務。

##部署釋出包dwz。zipcd /opt/myweb; unzip dwz。zip#修改openresty的配置cd /usr/local/openresty/nginx/conf/conf。dln -s /opt/myweb/dwz/nginx-dwz。conf dwz。conf#測試配置並重啟openrestyopenresty -tservice openresty restart

若一切正常,在瀏覽器中輸入:{雲伺服器外網IP}:8000/,出現介面如下:

基於openresty用lua搭建短網址服務

短網址服務用法

長網址轉短網址用法

在瀏覽器位址列中輸入: http://{IP}:8000/dwz/

${長網址}

後回車即可。

PS

${長網址}

必須由encodeURIComponent編碼。 如:https://www。wiqi100。cn/dwz/https%3A%2F%2Fwww。wiqi100。cn%2Fportal%2Findex。html%3Fp%3D中文%26p1%3Dv1%2Bv2%2B值20200615

短網址使用方法

將短網址直接複製到瀏覽器的位址列即可。 如:https://www。wiqi100。cn/dwz/uAi00n

結束語

基於openresty用lua搭建的短網址服務,為自己的網站提供一個可用的短網址功能,方便自己方便大家。

因本人水平有限,文中難免會有錯誤或不足之處,望各位看官不吝賜教。

參考資料

由衷感謝,基於各官網和網上搜索的一些相關資料,本文不再一一列出。