欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證過程

 更新時(shí)間:2024年06月19日 16:39:39   作者:星華先生  
這篇文章主要介紹了nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證過程,

nginx實(shí)現(xiàn)靜態(tài)文件的token認(rèn)證

說下思路

  • 1.用戶請(qǐng)求攜帶token請(qǐng)求nginx
  • 2.nginx反問后臺(tái)服務(wù)token是否有效
  • 3.token有效就返回靜態(tài)資源 無效就返回權(quán)限不夠

普通的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請(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)資源訪問添加認(rèn)證

由于nginx上存放了一些私密靜態(tài)文件,未防止被其他人獲取下載地址后私自下載,nginx可針對(duì)特定文件目錄進(jìn)行安全認(rèn)證,輸入用戶名和密碼通過后才能訪問,以下為設(shè)置過程:

1.安裝httpd

httpd里面有一個(gè)htpassword工具,用來創(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.瀏覽器訪問

http://192.168.11.20/qwert/

如圖:

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 把ImageMagic庫(kù)編譯進(jìn)nginx服務(wù)器的一些必要配置

    把ImageMagic庫(kù)編譯進(jìn)nginx服務(wù)器的一些必要配置

    這篇文章主要介紹了把ImageMagic庫(kù)編譯進(jìn)nginx服務(wù)器的一些必要配置,本文給出了操作步驟和配置參數(shù)示例,需要的朋友可以參考下
    2015-06-06
  • Nginx對(duì)網(wǎng)段內(nèi)ip的連接數(shù)限流配置詳解

    Nginx對(duì)網(wǎng)段內(nèi)ip的連接數(shù)限流配置詳解

    這篇文章主要介紹了Nginx對(duì)網(wǎng)段內(nèi)ip的連接數(shù)限流配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Nginx基于漏桶算法配置限流詳解

    Nginx基于漏桶算法配置限流詳解

    這篇文章主要為大家介紹了Nginx基于漏桶算法配置限流詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • nginx反向代理踩坑實(shí)戰(zhàn)記錄(容器方式)

    nginx反向代理踩坑實(shí)戰(zhàn)記錄(容器方式)

    Nginx是一個(gè)高性能的HTTP和反向代理web服務(wù)器,同時(shí)也提供了IMAP/POP3/SMTP服務(wù),下面這篇文章主要給大家介紹了關(guān)于nginx反向代理踩坑(容器方式)的相關(guān)資料,需要的朋友可以參考下
    2022-04-04
  • Nginx虛擬主機(jī)的六種配置(最全)

    Nginx虛擬主機(jī)的六種配置(最全)

    利用虛擬主機(jī),不用為每個(gè)要運(yùn)行的網(wǎng)站提供一臺(tái)單獨(dú)的Nginx服務(wù)器或單獨(dú)運(yùn)行一組Nginx進(jìn)程,本文主要介紹了Nginx虛擬主機(jī)的六種配置,具有一定的參考價(jià)值,感興趣的可以了解下
    2023-08-08
  • Nginx實(shí)現(xiàn)防盜鏈的多種方式

    Nginx實(shí)現(xiàn)防盜鏈的多種方式

    防盜鏈指的是防止其他網(wǎng)站未經(jīng)許可直接引用你的資源(如圖片、音視頻文件、文檔等),這樣做不僅能有效節(jié)約帶寬,還能防止未經(jīng)授權(quán)的內(nèi)容被濫用,本文給大家介紹了Nginx實(shí)現(xiàn)防盜鏈的多種方式,需要的朋友可以參考下
    2025-01-01
  • nginx-rtmp-module構(gòu)建流媒體直播服務(wù)器實(shí)戰(zhàn)指南

    nginx-rtmp-module構(gòu)建流媒體直播服務(wù)器實(shí)戰(zhàn)指南

    本文主要介紹了nginx-rtmp-module構(gòu)建流媒體直播服務(wù)器實(shí)戰(zhàn)指南,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • Linux centos7環(huán)境下Nginx安裝教程

    Linux centos7環(huán)境下Nginx安裝教程

    這篇文章主要為大家詳細(xì)介紹了Linux centos7環(huán)境下Nginx安裝教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • 解決nginx啟動(dòng)失敗(bind()?to?0.0.0.0:80?failed,An?attempt?was?made?to?access?a?socket?in...)

    解決nginx啟動(dòng)失敗(bind()?to?0.0.0.0:80?failed,An?attempt?was?

    這篇文章主要介紹了解決nginx啟動(dòng)失敗問題(bind()?to?0.0.0.0:80?failed,An?attempt?was?made?to?access?a?socket?in?...),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Nginx Rewrite模塊應(yīng)用的幾種場(chǎng)景

    Nginx Rewrite模塊應(yīng)用的幾種場(chǎng)景

    這篇文章主要介紹了Nginx Rewrite模塊應(yīng)用的幾種場(chǎng)景,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11

最新評(píng)論