前言
1
同nginx配置server以后,我們可以很方便的直接訪問到文件服務器上的文件資源,但是某些情況下,文件資源可能是隱私圖片,比如客戶注冊時上傳的身份證照片等等,這時候我們需要對圖片訪問進行控制,必須登錄后才能查看到這些隱私圖片。
2
一般來說,我們都是通過后端controller對權(quán)限進行控制,但是nginx作為圖片服務器的情況下,再專門為此寫一個后端程序顯然比較笨重。所以根據(jù)實際情況,我們采用openresty平臺,用lua腳本+redis讀取token的方式即可完成對圖片訪問服務器的權(quán)限控制。
步驟
1.服務器文件目錄設置
正常來說我們的文件存放目錄是統(tǒng)一的,但是由于有些文件是開放訪問的,比如網(wǎng)頁圖片,有的文件是隱私的,比如注冊信息。所以此時我們將文件服務器目錄修改為兩個文件夾,public和private,public用以存放開發(fā)文件,private存放登陸后才可以訪問的文件。
2.openresty下載
進入官網(wǎng)下載頁:http://openresty.org/en/download.html
選擇windows版本的壓縮包進行下載
將壓縮包解壓到本地目錄,openresty是nginx和lua的結(jié)合,此時我們編寫lua腳本并修改nginx.conf,測試openresty。
在openresty目錄下的lua文件夾下新建txt文件,
ngx.say('hello lua!!!')
然后重命名txt為hello.lua
如圖所示
然后在penresty目錄下的conf文件夾下,修改nginx.conf
server {
listen 9000;
server_name localhost;
location / {
default_type 'text/html';
rewrite_by_lua_file lua/hello.lua;
}
}
修改完成后,啟動openresty下的nginx.exe
然后通過cmd運行
./nginx -s reload
啟動nginx
然后瀏覽器鍵入 http://127.0.0.1:9000/ 如圖所示即為成功
3 配置nignx權(quán)限控制
#反向代理進入文件查詢服務器
upstream download{
server 127.0.0.1:8282;
}
server {
listen 8282;
server_name localhost;
location /file_resources/private/ {
alias D:/society/file_resources/private/;
}
}
server {
listen 8082;
server_name localhost;
client_max_body_size 10000m;
location / {
index index.html;
}
#開放文件,直接進入文件目錄查找
location /file_resources/public/ {
alias D:/society/file_resources/public/;
}
#隱私文件,匹配下載請求前綴,進入token.lua, 進行token鑒權(quán),
location /file_resources/private/ {
default_type 'text/html';
rewrite_by_lua_file lua/token.lua;
proxy_pass http://download;
}
}
4. token.lua編寫
該處獲取請求的token為get請求參數(shù)獲取
如為cookie中, ngx.var.arg_token改為 ngx.var.cookie_token即可
-- 從cookie中獲取token值key為token)
local token = ngx.var.arg_token
--判斷token是否為空,為空返回登錄
if not token then
ngx.redirect("http://127.0.0.1:8086/login", 302)
--判斷token存在,則根據(jù)redis存儲格式拼寫Token, “..”為字符串拼接
else
token = "prefix_user_token_" .. ngx.var.arg_token
end
local function close_redis(red)
if not red then
return
end
local pool_max_idle_time = 30000 --毫秒
local pool_size = 50 --連接池大小
local ok, err = red:set_keepalive(pool_max_idle_time, pool_size)
if not ok then
ngx.say("close redis error : ",err);
end
end
-- 連接redis
local redis = require "resty.redis";
local red = redis:new();
red:set_timeout(2000)
local ok,err = red:connect("192.168.0.28", 6379)
if not ok then
ngx.say("failed to connect: ", err)
end
-- 根據(jù)自身redis是否有密碼開啟本部分
--local res, err = red:auth("password")
--if not res then
-- ngx.say("failed to authenticate: ", err)
--end
-- redis中若 key 存在返回 1 ,否則返回 0 。
local resp, err = red:exists(token)
if not resp then
ngx.say("get msg error : ", err)
return close_redis(red)
end
if resp == ngx.null then
resp = ''
end
if resp == 0 then
-- ngx.exit(ngx.HTTP_FORBIDDEN)
ngx.redirect("http://127.0.0.1:8086/login", 302)
end
close_redis(red)
完成后 執(zhí)行 nginx -s reload重啟nginx即可
5.結(jié)果展示
開開放圖片
隱私圖片訪問失敗
如圖通過開發(fā)工具可以看到訪問的是圖片地址,由于token為空,所以直接轉(zhuǎn)轉(zhuǎn)到了login頁面文章來源:http://www.zghlxwxcb.cn/news/detail-488775.html
隱私圖片訪問成功
帶上token,訪問成功文章來源地址http://www.zghlxwxcb.cn/news/detail-488775.html
到了這里,關于Windows 環(huán)境下nginx 靜態(tài)資源服務器(圖片,文件)權(quán)限控制(nginx/openresty/lua)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!