nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證過(guò)程
nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證
說(shuō)下思路
- 1.用戶請(qǐng)求攜帶token請(qǐng)求nginx
- 2.nginx反問(wèn)后臺(tái)服務(wù)token是否有效
- 3.token有效就返回靜態(tài)資源 無(wú)效就返回權(quán)限不夠
普通的nginx無(wú)法編寫lua腳本
我們采用openresty版本可以編寫lua腳本
lua包需要下載lua-resty-http工具包,地址lua-resty-http,解壓后將.lua文件放到 lualib\resty目錄下就行。
編寫nginx的config的配置 server替換
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location / {
rewrite_by_lua_block {
-- local cjson = require "cjson"
-- local http = require "resty.http"
local httpc = http.new()
local ngx = ngx
local headers = ngx.req.get_headers()
-- get請(qǐng)求參數(shù)中T就是token
local token = headers["token"]
local request_method = ngx.var.request_method
local args = nil
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
end
token = args["token"];
if not token then
ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
ngx.status = ngx.HTTP_FORBIDDEN
ngx.say("You do not have permission to view the picture.")
ngx.exit(200)
end
-- 字符串拼接
-- 你要實(shí)現(xiàn)token鑒權(quán)的服務(wù),header和參數(shù)都給你實(shí)現(xiàn)了,根據(jù)實(shí)際需要選擇
local url = "http://127.0.0.1:8080/image/checkToken?token="..token;
local res, err = httpc:request_uri(url, {method="GET", headers={["token"]=token}})
if not res then
ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
ngx.say(cjson.encode({message = "Error getting response",status = ngx.HTTP_INTERNAL_SERVER_ERROR }));
ngx.exit(200)
end
if res.body == '0' then
ngx.header['Content-Type'] = 'text/plain; charset=utf-8';
ngx.say("You do not have permission to view the picture.");
ngx.exit(200)
end
}
root D:\\project;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}nginx對(duì)特定靜態(tài)資源訪問(wèn)添加認(rèn)證
由于nginx上存放了一些私密靜態(tài)文件,未防止被其他人獲取下載地址后私自下載,nginx可針對(duì)特定文件目錄進(jìn)行安全認(rèn)證,輸入用戶名和密碼通過(guò)后才能訪問(wèn),以下為設(shè)置過(guò)程:
1.安裝httpd
httpd里面有一個(gè)htpassword工具,用來(lái)創(chuàng)建認(rèn)證文件
yum -y install httpd
2.配置nginx
vim /etc/nginx/nginx.conf
添加如下配置:
location /qwert {
root /usr/share/nginx/html; #虛擬主機(jī)網(wǎng)站根目錄
index index.html index.htm; #虛擬主機(jī)首頁(yè)
auth_basic "secret"; #虛擬主機(jī)認(rèn)證命名
auth_basic_user_file /usr/local/nginx/passwd.db; #虛擬主機(jī)用戶名密碼認(rèn)證數(shù)據(jù)庫(kù)
}3.使用htpasswd命令生成用戶名及對(duì)應(yīng)密碼數(shù)據(jù)庫(kù)文件
htpasswd -c /usr/local/nginx/passwd.db admin // admin為認(rèn)證用戶名
4.重新加載nginx配置文件
nginx -s reload
5.瀏覽器訪問(wèn)
http://192.168.11.20/qwert/
如圖:

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過(guò)Nginx反向代理實(shí)現(xiàn)IP訪問(wèn)分流的示例代碼
本篇文章主要介紹了通過(guò)Nginx反向代理實(shí)現(xiàn)IP訪問(wèn)分流的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Nginx偽靜態(tài)配置和常用Rewrite偽靜態(tài)規(guī)則集錦
偽靜態(tài)是一種可以把文件后綴改成任何可能的一種方法,如果我想把php文件偽靜態(tài)成html文件,這種相當(dāng)簡(jiǎn)單的,下面我來(lái)介紹nginx 偽靜態(tài)配置方法有需要了解的朋友可參考。2014-06-06
Nginx實(shí)現(xiàn)自簽名SSL證書(shū)生成與配置實(shí)現(xiàn)
本文主要介紹了Nginx實(shí)現(xiàn)自簽名SSL證書(shū)生成與配置實(shí)現(xiàn),文章將詳細(xì)介紹生成自簽名SSL證書(shū)的步驟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09
一些可能是你極易忽略的Nginx知識(shí)點(diǎn)總結(jié)
無(wú)論是運(yùn)維、開(kāi)發(fā)、測(cè)試,Nginx技術(shù)棧的學(xué)習(xí)是必不可少的,只是不同的崗位掌握的深度與廣度不同而已,這篇文章主要介紹了一些可能是你極易忽略的Nginx知識(shí)點(diǎn),需要的朋友可以參考下2026-06-06
nginx?http?499錯(cuò)誤碼詳解以及解決辦法
HTTP狀態(tài)碼出現(xiàn)499錯(cuò)誤有多種情況,499錯(cuò)誤是什么?這篇文章主要給大家介紹了關(guān)于nginx?http?499錯(cuò)誤碼以及解決辦法的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
nginx通過(guò)nginx_upstream_check_module實(shí)現(xiàn)后端健康檢查
nginx的健康檢查有兩種,一種是被動(dòng)健康檢查,也就是nginx自帶健康檢查模塊ngx_http_upstream_module,另一種就是主動(dòng)健康檢查,使用第三方模塊nginx_upstream_check_module,下面就來(lái)介紹一下,感興趣的可以了解一下2024-08-08

