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

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

 更新時間:2024年06月19日 16:39:39   作者:星華先生  
這篇文章主要介紹了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; #虛擬主機網站根目錄
    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服務器的一些必要配置

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

    Nginx對網段內ip的連接數(shù)限流配置詳解

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

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

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

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

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

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

    利用虛擬主機,不用為每個要運行的網站提供一臺單獨的Nginx服務器或單獨運行一組Nginx進程,本文主要介紹了Nginx虛擬主機的六種配置,具有一定的參考價值,感興趣的可以了解下
    2023-08-08
  • Nginx實現(xiàn)防盜鏈的多種方式

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

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

    nginx-rtmp-module構建流媒體直播服務器實戰(zhàn)指南

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

    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?made?to?access?a?socket?in...)

    解決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
  • Nginx Rewrite模塊應用的幾種場景

    Nginx Rewrite模塊應用的幾種場景

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

最新評論