Nginx靜態(tài)資源或者路徑鑒權(quán)方式
更新時間:2024年06月19日 10:55:06 作者:longxiaobai_WJ
這篇文章主要介紹了Nginx靜態(tài)資源或者路徑鑒權(quán)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
1. http_auth_request_module
# 版本說明 nginx 1.20.2, 默認自帶 http_auth_request_module # 模塊查看驗證 nginx -V (nginx -V| grep 'http_auth_request_module')
2. 鑒權(quán)接口準備
本文后端接口是通過Egg.js
來實現(xiàn)的
實現(xiàn)代碼取決于項目中使用何種后端語言
'use strict'; const Controller = require('egg').Controller; class AuthController extends Controller { async index() { const { ctx } = this; ctx.body = 'hi, AuthController'; } async login() { const { ctx, service } = this; const secrect = Date.now(); const jwt_token = await service.actionToken.apply(secrect); ctx.helper.success(); // ctx.app.jwt.verify(jwt_token, this.app.config.jwt.secret); ctx.body = { status: 200, secret: jwt_token, }; } async authorize() { // 獲取 POST 傳遞的參數(shù) console.log(this.ctx.params); // 獲取 GET 傳遞的參數(shù) console.log(this.ctx.query); // 獲取通過 cookie 傳遞的參數(shù) const isAuthToken = this.ctx.cookies.get('authorize', { signed: false, }); console.log('isAuthToken log', isAuthToken); // 該代碼為兼容 4-2 配置代碼 console.log('x-original-uri', this.ctx.headers['x-original-uri']); if (isAuthToken) { this.ctx.body = { status: 200, secret: 'jwt_token', }; } else { this.ctx.status = 403; } } } module.exports = AuthController;
3. 修改Nginx配置
server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location = / { root index; index index.html index.htm; } location /eggjs { if ($http_cookie) { set $code_cookie $http_cookie; } if ($arg_code) { set $code_cookie "authorize=$arg_code; Path=/"; } # 指定auth_request auth_request /auth; proxy_pass http://127.0.0.1:3000/static/index.html; } # 驗證配置 location /jwt { proxy_pass http://localhost:3000/authorize; } location /scripts { proxy_pass http://127.0.0.1:3000/static/scripts/; } location = /auth { internal; # $http_cookie proxy_set_header Cookie "$code_cookie"; # 鑒權(quán)服務(wù)器的地址 proxy_pass http://localhost:3000/authorize; # proxy_pass_request_body off; # proxy_set_header Content-Length ""; # proxy_set_header X-Original-URI $query_string; # proxy_set_header X-Original-URI $request_uri; } #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; } }
4. 配置說明
指定auth_request
auth_request /auth;
傳遞完整的原始請求URI
- 由于身份驗證子請求將丟棄請求體,可以通過以下配置傳遞信息(本文未使用該方式,故皆已注釋);
# proxy_pass_request_body off; # proxy_set_header Content-Length ""; # proxy_set_header X-Original-URI $query_string; # proxy_set_header X-Original-URI $request_uri;
傳遞校驗信息
# URL: http://localhost:8080/eggjs?code=202304 # 設(shè)置變量 $code_cookie,以便后續(xù)使用,此處配置兼容 cookie 與 query 兩種寫法 if ($http_cookie) { set $code_cookie $http_cookie; } if ($arg_code) { # $arg_code 與 URL上的參數(shù)code相對應(yīng) # 即 secrect=202304 應(yīng)對應(yīng) $arg_secrect set $code_cookie "authorize=$arg_code; Path=/"; } # 在此處使用變量 $code_cookie proxy_set_header Cookie "$code_cookie";
Extra Config Notes
以下配置,本文尚未驗證;
location /eggjs { auth_request /auth; auth_request_set $auth_status $upstream_status; if ($auth_status = "403") { return 403; } proxy_pass http://127.0.0.1:3000/static/index.html; }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
nginx支持.htaccess文件實現(xiàn)偽靜態(tài)的方法分享
這篇文章主要介紹了nginx支持.htaccess文件實現(xiàn)偽靜態(tài)的方法分享,需要的朋友可以參考下2015-01-01nginx中的兩個模塊的proxy_pass的區(qū)別解析
在nginx中配置proxy_pass代理轉(zhuǎn)發(fā)時,如果在proxy_pass后面的url加/,表示絕對根路徑;如果沒有/,表示相對路徑,把匹配的路徑部分也給代理走。本文給大家介紹nginx中的兩個模塊的proxy_pass的區(qū)別,感興趣的朋友一起看看吧2021-11-11Nginx 代理轉(zhuǎn)發(fā)阿里云OSS上傳的實現(xiàn)代碼
這篇文章主要介紹了Nginx 代理轉(zhuǎn)發(fā)阿里云OSS上傳的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09Nginx服務(wù)器中配置非80端口的端口轉(zhuǎn)發(fā)方法詳解
這篇文章主要介紹了Nginx服務(wù)器中配置非80端口的端口轉(zhuǎn)發(fā)方法詳解,文中使用到了Nginx中的proxy_pass配置項,需要的朋友可以參考下2016-04-04