前言:實(shí)際開發(fā)中,用戶上傳的圖片凄敢,我們需要對圖片做保護(hù)措施碌冶,為了不讓圖片直接暴露在外網(wǎng)無差別的訪問,圖片服務(wù)器鏈接進(jìn)行鑒權(quán)這個就顯得尤為重要了
大致上分為三步驟涝缝,如果第一步已經(jīng)配置扑庞,則可以忽略
1.配置yum源譬重,方便安裝openresty
2.安裝openresty
3.編寫鑒權(quán)l(xiāng)ua腳本
1.配置yum源
1.1 將yum下載的rpm緩存下來(已備以后其他環(huán)境直接使用)
命令:vim /etc/yum.conf
cachedir
rpm包存放位置(更改為希望保存的地址)
keepcache
下載完成后是否進(jìn)行清理(更改為1)
debuglevel
debug等級
logfile
日志文件存放位置
1.2 配置yum源文件
進(jìn)入/etc/yum.reps.d/
目錄下,創(chuàng)建一個以 .repo
結(jié)尾的文件
我這邊創(chuàng)建一個163的yum源,把下邊文件放入到163yum源文件截圖對應(yīng)的目錄下即可
CentOS7-Base-163.repo文件下載 提取密碼:
esjb
刷新配置:
yum clean all
清空本地的yum源緩存yum repolist
重新生成元數(shù)據(jù)
2.安裝openresty
2.1通過yum形式安裝openresty
#執(zhí)行命令
yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty
期間有可能yum源安裝失敗嫩挤,提示無法解析163yum源域名害幅,此時需要配置linux的dns域名服務(wù)器
命令編輯:vim /etc/resolv.conf
添加內(nèi)容:
nameserver 168.95.1.1
nameserver 221.130.33.52
保存退出
2.2安裝成功后,則可以在/home/yumopenresty
目錄下岂昭,查看到openresty
文件
2.3我這邊安裝成功后以现,做了一下備份,方便其他同學(xué)下載使用
openresty.tar文件下載 提取密碼:cbva
2.4后續(xù)lua腳本會用到http網(wǎng)絡(luò)請求函數(shù)约啊,所以在/usr/local/openresty/lualib/resty
默認(rèn)路徑下邑遏,添加http工具類,以供后期調(diào)用http函數(shù)
httptool文件下載 提取密碼:
pc70
3.編寫鑒權(quán)l(xiāng)ua腳本
3.1 touch
命令新增touch getimage.lua
文件,編寫lua腳本
--getimage.lua文件內(nèi)容
--從header中獲取token值
local headers = ngx.req.get_headers()
local token = headers["token"]
--判斷token是否為空恰矩,為空則直接400
if not token
then
ngx.exit(400)
end
local http = require "resty.http"
local httpc = http.new()
local url = "http://換成你自己請求的地址?Authorization="..token
local resStr="false" --響應(yīng)結(jié)果
local res, err = httpc:request_uri(url, {
method = "GET",
headers = {
["Content-Type"] = "application/json",
}
})
--判斷接口返回結(jié)果狀態(tài)
if not res then
ngx.log(ngx.WARN,"failed to request: ", err)
ngx.exit(401)
end
--請求之后记盒,狀態(tài)碼
ngx.status = res.status
if ngx.status ~= 200 then
ngx.log(ngx.WARN,"非200狀態(tài),ngx.status:"..ngx.status)
ngx.exit(402)
end
--響應(yīng)的內(nèi)容 如果結(jié)果用戶id存在外傅,則重定向圖片服務(wù)器
resStr = res.body
if resStr ~= "false"
then
-- ngx.log(ngx.ERR,"failed to request: ")
return
else
ngx.exit(403)
end
3.2 配置nginx
/etc/nginx/lua/getimage.lua
lua文件地址纪吮,nginx觸發(fā)轉(zhuǎn)發(fā)前,去lua腳本文件鑒權(quán)萎胰,通過成功碾盟,則轉(zhuǎn)發(fā) proxy_pass http://127.0.0.1:7000/
; 地址
server {
listen 9000;
server_name localhost;
client_max_body_size 10m;
location /busvapp/image/ {
#日志地址
access_log /var/log/nginx/openrestyimage.access.log main;
#錯誤日志地址
error_log /var/log/nginx/openrestyimage.error.log error;
lua_code_cache off; #熱部署,每次修改lua文件技竟,不用重新加載部署
rewrite_by_lua_file /etc/nginx/lua/getimage.lua;
proxy_pass http://127.0.0.1:7000/;
}
}
3.3 啟動openresty
#啟動命令
usr/local/openresty/nginx/sbin/nginx -c /usr/local/openresty/nginx/conf/nginx.conf`
#建立軟鏈接冰肴,為openresty下的nginx建立軟鏈(非必需)
ln -s /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx
#如果此前有其他版本nginx已經(jīng)占用了軟鏈接,則我們可以修改刷新軟鏈接
語法:ln –snf [新的源文件或目錄] [目標(biāo)文件或目錄]
ln -snf /usr/local/openresty/nginx/sbin/nginx /usr/sbin/nginx
#建立完軟鏈接榔组,快速啟停
#啟動
service nginx start
#停止
service nginx stop
#重啟
service nginx restart
查看nginx版本信息是否是openresty熙尉,命令:nginx -V
至此,我們的圖片鑒權(quán)服務(wù)就做好了
后期 如果我們想把openresty服務(wù)拷貝到其他服務(wù)器上的話搓扯,很簡單检痰,只需要把
openresty.tar
包解壓,進(jìn)入到packages
文件夾
#命令安裝rpm包锨推,即可
rpm -ivh openresty-zlib-1.2.11-3.el6.x86_64.rpm
rpm -ivh openresty-pcre-8.44-1.el6.x86_64.rpm
rpm -ivh openresty-openssl111-1.1.1g-3.el6.x86_64.rpm
rpm -ivh openresty-1.17.8.2-1.el6.x86_64.rpm