nginx實現(xiàn)靜態(tài)文件的token認證過程
nginx實現(xiàn)靜態(tài)文件的token認證
說下思路
- 1.用戶請求攜帶token請求nginx
- 2.nginx反問后臺服務token是否有效
- 3.token有效就返回靜態(tài)資源 無效就返回權限不夠
普通的nginx無法編寫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請求參數(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
-- 字符串拼接
-- 你要實現(xiàn)token鑒權的服務,header和參數(shù)都給你實現(xiàn)了,根據(jù)實際需要選擇
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對特定靜態(tài)資源訪問添加認證
由于nginx上存放了一些私密靜態(tài)文件,未防止被其他人獲取下載地址后私自下載,nginx可針對特定文件目錄進行安全認證,輸入用戶名和密碼通過后才能訪問,以下為設置過程:
1.安裝httpd
httpd里面有一個htpassword工具,用來創(chuàng)建認證文件
yum -y install httpd
2.配置nginx
vim /etc/nginx/nginx.conf
添加如下配置:
location /qwert {
root /usr/share/nginx/html; #虛擬主機網(wǎng)站根目錄
index index.html index.htm; #虛擬主機首頁
auth_basic "secret"; #虛擬主機認證命名
auth_basic_user_file /usr/local/nginx/passwd.db; #虛擬主機用戶名密碼認證數(shù)據(jù)庫
}3.使用htpasswd命令生成用戶名及對應密碼數(shù)據(jù)庫文件
htpasswd -c /usr/local/nginx/passwd.db admin // admin為認證用戶名
4.重新加載nginx配置文件
nginx -s reload
5.瀏覽器訪問
http://192.168.11.20/qwert/
如圖:

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
把ImageMagic庫編譯進nginx服務器的一些必要配置
這篇文章主要介紹了把ImageMagic庫編譯進nginx服務器的一些必要配置,本文給出了操作步驟和配置參數(shù)示例,需要的朋友可以參考下2015-06-06
Nginx對網(wǎng)段內ip的連接數(shù)限流配置詳解
這篇文章主要介紹了Nginx對網(wǎng)段內ip的連接數(shù)限流配置詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03
nginx-rtmp-module構建流媒體直播服務器實戰(zhàn)指南
本文主要介紹了nginx-rtmp-module構建流媒體直播服務器實戰(zhàn)指南,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2025-02-02
Linux centos7環(huán)境下Nginx安裝教程
這篇文章主要為大家詳細介紹了Linux centos7環(huán)境下Nginx安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-03-03
解決nginx啟動失敗(bind()?to?0.0.0.0:80?failed,An?attempt?was?
這篇文章主要介紹了解決nginx啟動失敗問題(bind()?to?0.0.0.0:80?failed,An?attempt?was?made?to?access?a?socket?in?...),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05

